zoukankan      html  css  js  c++  java
  • Unity 序列化与反序列化

    • 使用 ScriptableObject 保存到硬盘后在编辑器中可以二次编辑
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEditor;
    using System.IO;
    
    public class TestScriptableObject : Editor {
    
        [MenuItem("Tools/Serialize")]
        private static void Serialize () {
            Rect rect = ScriptableObject.CreateInstance<Rect>();
            rect.width = 10;
            rect.height = 20;
    
            // *注意 AssetDatabase.CreateAsset 方法使用的是相对路径
            string path = "Assets/Data";
            if (!Directory.Exists(path)) {
                Directory.CreateDirectory(path);
            }
    
            // Assets/Data/Rect.asset
            path += $"/Rect.asset";
            AssetDatabase.CreateAsset(rect, path);
        }
    
        [MenuItem("Tools/Deserialize")]
        private static void Deserialize () {
            Rect rect = AssetDatabase.LoadAssetAtPath<Rect>("Assets/Data/Rect.asset");
            Debug.Log(rect.width);  // 10
            Debug.Log(rect.height); // 20
        }
    
    
        public class Rect : ScriptableObject {
            public int width;
            public int height;
        }
    }
    
    • 二进制序列化与反序列化
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEditor;
    using System.IO;
    using System.Runtime.Serialization.Formatters.Binary;
    
    public class TestBinary : Editor {
    
        [MenuItem("Tools/BinarySerialize")]
        private static void BinarySerialize () {
            Rect rect = new Rect();
            rect.width = 10;
            rect.height = 20;
    
            string path = Application.dataPath + "/Data/Rect.bytes";
            FileStream fileStream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
            BinaryFormatter binaryFormatter = new BinaryFormatter();
            binaryFormatter.Serialize(fileStream,rect);
            fileStream.Close();
    
            AssetDatabase.Refresh(); // 保存到 Assets 文件夹下需要刷新才能看到
        }
    
        [MenuItem("Tools/BinaryDeserialize")]
        private static void BinaryDeserialize () {
            // 非 Assets 文件夹下时,使用 byte[] bytes = File.ReadAllBytes(path);
            TextAsset textAsset = AssetDatabase.LoadAssetAtPath<TextAsset>("Assets/Data/Rect.bytes");
    		
            MemoryStream memoryStream = new MemoryStream(textAsset.bytes);
            BinaryFormatter binaryFormatter = new BinaryFormatter();
    		
            Rect rect = (Rect)binaryFormatter.Deserialize(memoryStream);
            memoryStream.Close();
    		
            Debug.Log(rect.width);  // 10
            Debug.Log(rect.height); // 20
        }
    
        // * 注意必须标记可序列化
        [System.Serializable]
        public class Rect{
            public int width;
            public int height;
        }
    }
    
    • XML 序列化与反序列化
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEditor;
    using System.IO;
    using System.Xml.Serialization;
    
    public class TestXml : Editor {
    
        [MenuItem("Tools/XmlSerialize")]
        private static void XmlSerialize () {
            Rect rect = new Rect();
            rect.width = 10;
            rect.height = 20;
    
            string path = Application.dataPath + "/Data/Rect.xml";
            FileStream fileStream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
            StreamWriter streamWriter = new StreamWriter(fileStream, System.Text.Encoding.UTF8);
            XmlSerializer xmlSerializer = new XmlSerializer(rect.GetType());
            xmlSerializer.Serialize(streamWriter, rect);
            streamWriter.Close();
            fileStream.Close();
    
            AssetDatabase.Refresh(); // 保存到 Assets 文件夹下需要刷新才能看到
        }
    
        [MenuItem("Tools/XmlDeserialize")]
        private static void XmlDeserialize () {
            string path = Application.dataPath + "/Data/Rect.xml";
            FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(Rect));
            Rect rect = (Rect)xmlSerializer.Deserialize(fileStream);
            fileStream.Close();
    
            Debug.Log(rect.width);  // 10
            Debug.Log(rect.height); // 20
        }
    
        // * 注意必须标记可序列化
        [System.Serializable]
        public class Rect {
            public int width;
            public int height;
        }
    }
    
    • JSON 序列化与反序列化
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEditor;
    using System.IO;
    
    public class TestJson : Editor {
    
        [MenuItem("Tools/JsonSerialize")]
        private static void JsonSerialize () {
            Rect rect = new Rect();
            rect.width = 10;
            rect.height = 20;
    
            string jsonString = JsonUtility.ToJson(rect);
            string path = Application.dataPath + "/Data/Rect.json";
            File.WriteAllText(path, jsonString);
    
            AssetDatabase.Refresh(); // 保存到 Assets 文件夹下需要刷新才能看到
        }
    
        [MenuItem("Tools/JsonDeserialize")]
        private static void JsonDeserialize () {
            string path = Application.dataPath + "/Data/Rect.json";
            StreamReader streamReader = File.OpenText(path);
            string jsonString = streamReader.ReadToEnd();
            streamReader.Close();
    
            Rect rect = JsonUtility.FromJson<Rect>(jsonString);
            Debug.Log(rect.width);  // 10
            Debug.Log(rect.height); // 20
        }
    
        public class Rect {
            public int width;
            public int height;
        }
    }
    
  • 相关阅读:
    设计模式之单例模式实践
    有关集合的foreach循环里的add/remove
    项目中常用的MySQL优化方法--壹拾玖条
    Solr
    Lucene补充
    Lucene
    一千行 MySQL 学习笔记
    Servlet
    CSS未知宽高元素水平垂直居中
    深拷贝和浅拷贝
  • 原文地址:https://www.cnblogs.com/kingBook/p/15147940.html
Copyright © 2011-2022 走看看