Unity序列化方式汇总

117 阅读3分钟

1.XML文件

XML文件是一种通用的数据格式,可以适用于各种类型的数据。
在Unity中,我们可以使用XmlSerializer类来将对象序列化为XML格式的数据或将XML格式的数据反序列化为对象。
使用XML文件作为序列化配表可以使数据结构更清晰,易于修改和维护。
xml使用XmlDocument类来保存和加载xml文档,静态方法提供创建、保存、读取、添加、删除xml文件,命名为System.xml;

XML文件格式:

1698114342312.png

1.1 对象模型

public class Save
    {
        public int m_posX;
        public int m_posY;
        public int m_posZ;
        public Save(int posX, int posY, int posZ)
        {
            m_posX = posX;
            m_posY = posY;
            m_posZ = posZ;
        }
    }

1.2 写xml

private void SaveByXML1()
    {
    //创建对象模型
        Save save = new Save(1,2,3);
        XmlDocument xmlDoc = new XmlDocument();
        //创建根节点
        XmlElement root=xmlDoc.CreateElement("Save");
        root.SetAttribute("FileName", "File_01"); //可选

        XmlElement posXElement = xmlDoc.CreateElement("posX");
        posXElement.InnerText=save.m_posX.ToString();
        root.AppendChild(posXElement);

        XmlElement posYElement = xmlDoc.CreateElement("posY");
        posYElement.InnerText=save.m_posY.ToString();
        root.AppendChild(posYElement);

        XmlElement posZElement = xmlDoc.CreateElement("posZ");
        posZElement.InnerText=save.m_posZ.ToString();
        root.AppendChild(posZElement);

        xmlDoc.AppendChild(root);
        xmlDoc.Save(Application.streamingAssetsPath + "/DataXml.text");
        Debug.Log("------Save------");  
    }

1.3 读xml

 private void LoadByXML()
    {
        if (File.Exists(Application.streamingAssetsPath + "/DataXML.text"))
        {
            Save save = new Save(0,0,0);
            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.Load(Application.streamingAssetsPath + "/DataXML.text");
            //通过Tag标签访问元素中具体的数值,返回的是List列表,第一个是posX[0],第二个是posX[1];
            XmlNodeList posX = xmlDocument.GetElementsByTagName("posX");
            int pos_X = int.Parse(posX[0].InnerText);
            save.m_posX = pos_X;

            XmlNodeList posY = xmlDocument.GetElementsByTagName("posY");
            int pos_Y = int.Parse(posY[0].InnerText);
            save.m_posY = pos_Y;

            XmlNodeList posZ = xmlDocument.GetElementsByTagName("posZ");
            int pos_Z = int.Parse(posZ[0].InnerText);
            save.m_posZ = pos_Z;
            gameObject.transform.position = new Vector3(save.m_posX, save.m_posY,save.m_posZ);
            Debug.Log("------Load-------");
        }
        else
        {
            Debug.Log("NOT FOUND THIS FILE!");
        }
    }

1.4序列化Save

ed3097f9fae59ae899bd1e3d51a4cf6.png

List<Save> saves = new List<Save>() 
        { 
            new Save() {PosX = 10,PosY = 10,PosZ = 10},
            new Save() {PosX = 11,PosY=11,PosZ=11},
            new Save() {PosX = 12,PosY=12,PosZ=12}
        };

        //创建包含Save集合的对象
        SaveCollection saveCollection = new SaveCollection {Saves=saves};

        //将对象集合序列化为XML格式数据

        XmlSerializer xmlSerializer = new XmlSerializer(typeof(SaveCollection));

        using (TextWriter writer = new StreamWriter(Application.streamingAssetsPath + "/savesList.xml"))
        {
            xmlSerializer.Serialize(writer, saveCollection);
        }

1.5反序列化Save集合

  // 将XML格式数据反序列化为对象集合
        using (TextReader reader = new StreamReader(Application.streamingAssetsPath+ "/savesList.xml"))
        {
            SaveCollection deserializedCollection = (SaveCollection)xmlSerializer.Deserialize(reader);
            List<Save> deserializedPlayers = deserializedCollection.Saves;

            foreach (Save player in deserializedPlayers)
            {
                Debug.LogFormat($"posX: {player.PosX}, posY: {player.PosY},posZ:{player.PosZ}");
            }
        }

2.JSON文件

JSON文件是一种轻量级的数据交换格式,具有良好的可读性和可扩展性。
在Unity中,我们可以使用JsonUtility类将对象序列化为JSON格式的数据或将JSON格式的数据反序列化为对象。
使用JSON文件作为序列化配表可以使数据格式更紧凑,易于传输和存储。

3.ScriptableObject

ScriptableObject是Unity中的一种特殊类型,可以用于存储数据和对象,具有良好的可扩展性和可重用性Unity中,我们可以创建一个ScriptableObject对象,将需要序列化的数据存储在其中,
然后将ScriptableObject对象保存为.asset文件
使用ScriptableObject作为序列化配表可以使数据结构更灵活,易于修改和扩展

4.CSV文件

CSV文件是一种简单的文本格式,可以用于存储表格数据。
在Unity中,我们可以使用第三方插件或自己编写代码来读取和写入CSV文件。
使用CSV文件作为序列化配表可以使数据结构更简单,易于编辑和查看