本地资源_Asset
数据
using System.Collections.Generic; using UnityEngine; public enum Enum_Test { A, B, C, } [System.Serializable] public class Data_Test : ScriptableObject { public Enum_Test state; [Range(0, 10)] public float value; public List<int> numbers; public List<string> infomations; public string description; //可挂载资源 public GameObject obj; }
处理
using System.IO; using UnityEditor; using UnityEngine; public class AssetBuilder { public static void CreateAsset<T>() where T : ScriptableObject { ScriptableObject data = ScriptableObject.CreateInstance<T>(); LoadData(data); string path = Application.dataPath + "/Data"; if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } path = string.Format("Assets/Data/{0}.asset", (typeof(T).ToString())); AssetDatabase.CreateAsset(data, path); } public static T LoadAsset<T>() where T : ScriptableObject { string name = typeof(T).ToString(); string path = string.Format("Assets/Data/{0}.asset", name); T data = AssetDatabase.LoadAssetAtPath<T>(path); return data; } public static void LoadData(ScriptableObject data) { //可以去load表 if (data.GetType() == typeof(Data_Test)) { Data_Test dt = (Data_Test)data; dt.description = "测试"; } } }
测试
using UnityEngine; public class LoadAssetData : MonoBehaviour { void Start() { AssetBuilder.CreateAsset<Data_Test>(); Data_Test dt = AssetBuilder.LoadAsset<Data_Test>(); Debug.LogError(dt.description); } }