首页 > 代码库 > unity, 由scriptableObject创建.asset
unity, 由scriptableObject创建.asset
由继承自scriptableObject的类X创建.asset文件。
假设类X的定义为:
[System.Serializable]
public class X : ScriptableObject {
public float m_a;
}
方法一:写代码创建。
#if UNITY_EDITOR
string path = "Assets/Resources/x.asset";
X scriptableObj = ScriptableObject.CreateInstance<X> ();
UnityEditor.AssetDatabase.CreateAsset (scriptableObj, path);
UnityEditor.AssetDatabase.SaveAssets ();
UnityEditor.AssetDatabase.Refresh();
#endif
(补充:对于如上创建出来的x.asset,加载写法是:
若用assetDatabase加载:
#if UNITY_EDITOR
UnityEditor.AssetDatabase.LoadAssetAtPath("Assets/Resources/x.asset");
#endif
若用Resource.Load加载:
Resources.Load("x");
)
方法二:通过菜单创建。
在类X前添加CreateAssetMenu属性,会自动在系统create菜单中添加对应的菜单项。
[CreateAssetMenu(fileName="x",menuName="(custom) X")]
[System.Serializable]
public class X : ScriptableObject {
public float m_a;
}
参考:
http://baba-s.hatenablog.com/entry/2015/06/13/000000
http://answers.unity3d.com/questions/1079664/how-do-i-use-the-createassetmenu-attribute.html
unity, 由scriptableObject创建.asset