简体   繁体   English

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

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

I am trying to save a list into XML and later load the XML back into that list. 我试图将列表保存为XML,然后再将 XML 加载回该列表。 Most of it appears to work well with the exception being the actual Save method. 除实际的Save方法外,大多数方法看起来都可以正常工作。

Here is my code. 这是我的代码。 The problem is that SaveInformation.xml is not getting created. 问题是没有创建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();
        }
    }
}

Here are the Load and Save methods: 这是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");
    }
}

Here is the Class that should be passing XML stat and PlayerData 这是应该传递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>();
}

And here is the Data itself 这是数据本身

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;
}

You shouldn't create a new instance of your class if you call save. 如果调用save,则不应创建类的新实例。 Make sure you only have one instance that gets Loaded and Saved... 确保只有一个实例已被加载并保存...

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