需要继承 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);
}
}
网友评论