ScriptableObject 【脚本化对象】
可以作为我们存储资源数据的有效方案。同时此资源可以作为我们AB包的有效资源!
优点
- 不需要绑定到物体对象。
- 存放于编辑器或者作为一种资源存储。
- 操作方便,可视化动态修改。
- 读取数据方便,ScriptableObject已经是可序列化的数据。
- 可以在项目之间很好的复用而不会丢失数据。
代码实现
using System.IO;
using UnityEditor;
using UnityEngine;
public class Item : ScriptableObject
{
public ulong itemId;
public Texture itemIco;
public string info;
[MenuItem("Assets/Create/Items")]
public static void Execute()
{
Items sd = ScriptableObject.CreateInstance();
string path = AssetDatabase.GetAssetPath(Selection.activeObject);
if(File.Exists(path))
{
path = Path.GetDirectoryName(path);
}
path = GetName(path, 0);
AssetDatabase.CreateAsset(sd, path);
Selection.activeObject = sd;
EditorGUIUtility.PingObject(sd);
}
static string GetName(string path,int index)
{
string name = path+ "/New Items.asset";
while (true)
{
if (File.Exists(name))
{
name = path + "/New Items " + (++index) + ".asset";
}
else
{
return name;
}
}
}
}
效果展示