繁体   English   中英

无法使用Unity和C#编写XML文件

[英]XML file not getting written using Unity and C#

我试图将列表保存为XML,然后再将 XML 加载回该列表。 除实际的Save方法外,大多数方法看起来都可以正常工作。

这是我的代码。 问题是没有创建SaveInformation.xml

[XmlRoot("SaveInformation")]
public class SaveInformation {

    [XmlArray("stat")]
    [XmlArrayItem("PlayerData")]
    public PlayerData[] stat;

    public void Save(string path){
        var serializer = new XmlSerializer(typeof(SaveInformation));
        using(var stream = new FileStream(path,FileMode.Create)){
            serializer.Serialize (stream, this);
            vstream.Close();
        }
    }

    public static SaveInformation Load(string path){
        var serializer = new XmlSerializer(typeof(SaveInformation));
        using(var stream = new FileStream(path,FileMode.Open)){
            return serializer.Deserialize (stream) as SaveInformation;
            stream.Close();
        }
    }
}

这是LoadSave方法:

using UnityEngine;
using System.Collections;
using System.IO;

public class StoreData : MonoBehaviour {

    public static void Load(){
        var saveInformation = SaveInformation.Load(
            Path.Combine(Application.dataPath, "SaveInformation.xml"));

        Debug.Log("Loaded");
    }

    public static void Save(){
        SaveInformation obj = new SaveInformation();

        obj.Save(Path.Combine(Application.persistentDataPath,
            "SaveInformation.xml"));

        Debug.Log("Saved");
    }
}

这是应该传递XML statPlayerData的类

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

[XmlRoot("PlayerDatabaseCollection")]
public class PlayerDatabase : MonoBehaviour {
    [XmlArray("stat")]
    [XmlArrayItem("PlayerData")]
    public List<PlayerData> stat = new List<PlayerData>();
}

这是数据本身

using UnityEngine;
using System.Collections;
using System.Xml;
using System.Xml.Serialization;

[System.Serializable]
public class PlayerData {
    [XmlAttribute("name")]
    public string Name;
    [XmlAttribute("current")]
    public int Current;
    [XmlAttribute("max")]
    public int Max;
}

如果调用save,则不应创建类的新实例。 确保只有一个实例已被加载并保存...

public class StoreData : MonoBehaviour {

    // this will hold your data (but in the snippets you provided I 
    // can't see how you are accessing this, maybe this should be public)
    private static SaveInformation saveInformation = null; // keep one instance

    private static string savepath = Application.dataPath; // or Application.persistentDataPath ?
    private static string savePathFile = Path.Combine(savepath, "SaveInformation.xml");

    public SaveInformation Instance {
       get {
           return (saveInformation ?? new SaveInformation());
       }
    }

    public static void Load(){
        saveInformation = SaveInformation.Load(savePathFile);
        Debug.Log("Loaded");
    }

    public static void Save(){
        if (saveInformation != null) {
           saveInformation.Save(savePathFile);
           Debug.Log("Saved");
        } else {
            Debug.Log("No saveinformation yet");
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM