美文网首页
Unity asset序列化

Unity asset序列化

作者: ___________6a1d | 来源:发表于2021-06-17 08:55 被阅读0次

    需要继承 ScriptableObject
    字典的序列化需要注意,可以使用两个list来帮助字典序列化。

    using System.Collections.Generic;
    using UnityEngine;
     
    /// <summary>
    /// 生成文件名   菜单中名字   菜单中顺序
    /// </summary>
    [CreateAssetMenu(fileName ="TestAssets",menuName = "CreateAsset", order =0)]
    public class AssetsSerilize : ScriptableObject
    {
        public int ID;
        public string Name;
        public List<int> GradeList;
      
    }
    

    在project面板右击create—CreateAsset就可以得到:


    image.png
    image.png

    反序列化

    using System.Runtime.Serialization.Formatters.Binary;
     
    public class Test : MonoBehaviour
    {
        void Start()
        {
            //SerilizeTest();
            //DeSerilizeTest();
     
            //BinarySerilizeTest();
            //BinaryDeserilizeTest();
     
            ReadTestAssets();
        }
     
        void ReadTestAssets()
        {
            AssetsSerilize assetsSerilize = UnityEditor.AssetDatabase.LoadAssetAtPath<AssetsSerilize>("Assets/Scripts/Serilize/TestAssets.asset");
     
            Debug.Log("ID====" + assetsSerilize.ID);
            Debug.Log("Name====" + assetsSerilize.Name);
            foreach (int item in assetsSerilize.GradeList)
            {
                Debug.Log("Grade===" + item);
            }
        }
    

    扩展教程https://blog.csdn.net/jjiss318/article/details/7195052

    相关文章

      网友评论

          本文标题:Unity asset序列化

          本文链接:https://www.haomeiwen.com/subject/hppzeltx.html