Unity 3D Android Platform XML WRITE LOAD Question.

Asked 1 years ago, Updated 1 years ago, 119 views

string filename = "Quests";

Type[] questType = { typeof(QUEST) };

FileStream fs = new FileStream(Path.Combine(Application.persistentDataPath,filename+".txt"), FileMode.Create);

StreamWriter sw = new StreamWriter(fs, Encoding.UTF8);

XmlSerializer serializer = new XmlSerializer(typeof(QuestContainer), questType);

serializer.Serialize(sw, questContainer);

sw.Close();

The above is a method for WRITE to the specified file.

string filename = "Quests";

Type[] questType = { typeof(QUEST) };

XmlSerializer serializer = new XmlSerializer(typeof(QuestContainer), questType);

TextReader fs = new StreamReader(Application.persistentDataPath + filename + ".txt");

QuestContainer = (QuestContainer)serializer.Deserialize(fs);

fs.Close();

The above is a method for LOADing the specified file.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class QuestContainer
{
    private List<QUEST> confirmed = new List<QUEST>();
    private List<QUEST> complete = new List<QUEST>();
    private List<QUEST> possible = new List<QUEST>();
    private List<QUEST> im_possible = new List<QUEST>();

    public List<QUEST> Confirmed
    {
        get { return confirmed; }
        set { confirmed = value; }
    }

    public List<QUEST> Complete
    {
        get { return complete; }
        set { complete = value; }
    }

    public List<QUEST> Possible
    {
        get { return possible; }
        set { possible = value; }
    }

    public List<QUEST> im_Possible
    {
        get { return im_possible; }
        set { im_possible = value; }
    }
}

Above is the QuestContainer class.

public class QUEST
{
    public string Questname { get; set; }
    public string Description { get; set; }
    public string Class { get; set; }
    public int Level { get; set; }
    public int STR { get; set; }
    public int CON { get; set; }
    public int INT { get; set; }
    public int WIS { get; set; }
    public int DEX { get; set; }
    public string Title { get; set; }
    public string Item { get; set; }
    public QUEST_TYPE questType { get; set; }
    public DIFFICULTY difficulty { get; set; }
    public EXPEDIENT eXPEDIENT { get; set; }
    public QUEST_REWARD questReward { get; set; }
    public string target { get; set; }
    public int count { get; set; }
    public bool canClear { get; set; }

    public QUEST()
    {

    }

    public QUEST(string Questname,string Description,string Class,int level,int str,int con, int Int, int wis, int dex,string title,string item,  QUEST_TYPE QuestType, DIFFICULTY Difficulty, QUEST_REWARD quest_reward, EXPEDIENT EXPEDIENT, string Target, int Count, bool canclear)
    {
        this.Description = Description;
        this.Questname = Questname;
        this.questType = QuestType;
        this.difficulty = Difficulty;
        this.eXPEDIENT = EXPEDIENT;
        this.questReward = quest_reward;
        this.Class = Class;
        this.Level = level;
        this.STR = str;
        this.CON = con;
        this.INT = Int;
        this.WIS = wis;
        this.DEX = dex;
        this.Title = title;
        this.Item = item;
        this.target = Target;
        this.count = Count;
        this.canClear = canclear;
    }
 }

Above is the Quest class.

<?xml version="1.0" encoding="utf-8"?>
<QuestContainer xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Confirmed>
    <QUEST>
      <Questname>Start</Questname>
      <Description>It's just the beginning!</Description>
      <Class>ALL</Class>
      <Level>0</Level>
      <STR>0</STR>
      <CON>0</CON>
      <INT>0</INT>
      <WIS>0</WIS>
      <DEX>0</DEX>
      <Title />
      <Item />
      <questType>ROOP</questType>
      <difficulty>EASY</difficulty>
      <eXPEDIENT>MOVE</eXPEDIENT>
      <questReward>GOLD</questReward>
      <target>Bank</target>
      <count>10</count>
      <canClear>false</canClear>
    </QUEST>
  </Confirmed>
  <Complete />
  <Possible />
  <im_Possible />
</QuestContainer>

Above is the xml file. On the Android platform, I changed it to .txt and am using it.

According to the list in QuestContainer, it is divided into four processes and stored in an xml file.

Is the read working fine?

During the game, you change the contents of the list to QuestContainer and write it down.

There seems to be a problem with the write, but I don't know the reason because there is no change when I recall it again.

parsing android unity xml

2022-09-22 22:11

1 Answers

It works well on Android as follows. I loaded the contents of the file, changed the value, and then loaded it again and printed it out.

Get the path from pathForDocumentsFile. And if it fails to load the file, we used the method of loading the file in resources.

using UnityEngine;
using System.Collections;
using System.Xml;
using System.Collections.Generic;
using System.Xml.Serialization;
using System.IO;
using System.Text;
using System;


public class NewBehaviourScript : MonoBehaviour {
    QuestContainer container;
    void Start () {
        loadXML ();
        Debug.Log ("###before###");
        foreach (QUEST quest in container.Confirmed) {
            Debug.Log (quest.Description);
            quest.Description="!!!"+quest.Description;
        }

        writeXML ();
        loadXML ();
        Debug.Log ("###after###");
        foreach (QUEST quest in container.Confirmed) {
            Debug.Log (quest.Description);
        }


    }

    void loadXML(){
        try{
            TextReader fs = new StreamReader(pathForDocumentsFile("Catalog.txt"));
            var serializer = new XmlSerializer(typeof(QuestContainer));
            container = serializer.Deserialize(fs) as QuestContainer;
            fs.Close();
        }
        catch(Exception e){
            TextAsset textAsset = (TextAsset) Resources.Load("Catalog");  
            var serializer = new XmlSerializer(typeof(QuestContainer));
            StringReader stream = new StringReader (textAsset.ToString ());
            container = serializer.Deserialize(stream) as QuestContainer;
            stream.Close();
        }
    }

    void writeXML(){
        var serializer = new XmlSerializer (typeof(QuestContainer));
        var stream = new FileStream(pathForDocumentsFile("Catalog.txt"), FileMode.Create);
        StreamWriter sw = new StreamWriter (stream, Encoding.UTF8);
        serializer.Serialize(sw, container);
        stream.Close();
    }

    public string pathForDocumentsFile( string filename ) 
    {
        if (Application.platform == RuntimePlatform.IPhonePlayer)
        {
            string path = Application.dataPath.Substring( 0, Application.dataPath.Length - 5 );
            path = path.Substring( 0, path.LastIndexOf( '/' ) );
            return Path.Combine( Path.Combine( path, "Documents" ), filename );
        }
        else if(Application.platform == RuntimePlatform.Android)
        {
            string path = Application.persistentDataPath; 
            path = path.Substring(0, path.LastIndexOf( '/' ) ); 
            return Path.Combine (path, filename);
        } 
        else 
        {
            string path = Application.dataPath; 
            path = path.Substring(0, path.LastIndexOf( '/' ) );
            return Path.Combine (path, filename);
        }
    }
}

In order to use serializer, each class must have the following annotation.

using System.Xml.Serialization;
using System.Collections.Generic;

[XmlRoot("QuestContainer")]
public class QuestContainer{
    [XmlArray("Confirmed")]
    [XmlArrayItem("QUEST")]
    public List<QUEST> Confirmed = new List<QUEST> ();
}


public class QUEST{
    public string Questname;
    public string Description;
    public int Level;
}


2022-09-22 22:11

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.