1 回答
TA贡献1808条经验 获得超4个赞
使用 XMLSerializer,我可以随时从“.xml”文件中保存和获取数据。
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;
using UnityEngine;
[DisallowMultipleComponent]
internal sealed class XMLManager : MonoBehaviour
{
public ItemDatabase ItemDatabase = new ItemDatabase();
public void SaveItem()
{
var xmlSerializer = new XmlSerializer(typeof(ItemDatabase));
var fileStream = new FileStream(Application.dataPath + "/StreamingFiles/XML/Items.xml", FileMode.Create);
xmlSerializer.Serialize(fileStream, ItemDatabase);
fileStream.Close();
}
public void LoadItem()
{
var xmlSerializer = new XmlSerializer(typeof(ItemDatabase));
var fileStream = new FileStream(Application.dataPath + "/StreamingFiles/XML/Items.xml", FileMode.Open);
ItemDatabase = xmlSerializer.Deserialize(fileStream) as ItemDatabase;
fileStream.Close();
}
}
[System.Serializable]
public sealed class Item
{
public string Name;
public string Description;
public int Damage;
public Source Element;
public enum Source { Fire, Water, Air };
}
[System.Serializable]
public sealed class ItemDatabase
{
public List<Item> items = new List<Item>();
}
这是适合我的代码。
希望这能帮助那里的人。
- 1 回答
- 0 关注
- 140 浏览
添加回答
举报