1.使用Asset序列化需要继承ScriptableObject,当继承以后,脚本中的元素不需要添加任何标签
2.创建一个实例并保存为.asset文件。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu(fileName ="AssetSerilize",menuName ="CreatAsset",order =0)]
public class AssetSerilize : ScriptableObject
{
public int id;
public string name;
public List<string> testList = new List<string>();
}
3.点击创建(Creat),选择自己的菜单栏
4.创建以后,填入我们所需要的数据
5.读取
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ReadAssetSerlilze : MonoBehaviour
{
void Start()
{
ReadAsset();
}
void ReadAsset()
{
AssetSerilize asset = UnityEditor.AssetDatabase.LoadAssetAtPath<AssetSerilize>("Assets/Scripts/AsseTest/AssetSerilizeTest.asset");
Debug.Log(asset.id);
Debug.Log(asset.name);
foreach (string str in asset.testList)
{
Debug.Log(str);
}
}
}