0.创建有层级的资源
1.子资源代码
using UnityEngine;
public class ChildScriptableObject : ScriptableObject
{
[SerializeField]
string str;
void OnEnable()
{
name = "New ChildScriptableObject";
}
}
2.父资源代码
创建父子资源、删除子资源
using UnityEngine;
using UnityEditor;
public class ParentScriptableObject : ScriptableObject
{
//--------------父资源内容
[SerializeField]
ChildScriptableObject child;
//------------------------
const string PATH = "Assets/Editor/04/New ParentScriptableObject.asset";
/// <summary>
/// 创建父资源、子资源,并建立父子关系
/// </summary>
[MenuItem("Assets/Create ScriptableObject")]
static void CreateScriptableObject()
{
//父物体实例化
var parent = ScriptableObject.CreateInstance<ParentScriptableObject>();
//子物体实例化
parent.child = ScriptableObject.CreateInstance<ChildScriptableObject>();
//隐藏子物体展开箭头,必要时使用
//parent.child.hideFlags = HideFlags.HideInHierarchy;
//创建父子关系
AssetDatabase.AddObjectToAsset(parent.child, PATH);
//保存资源到本地
AssetDatabase.CreateAsset(parent, PATH);
//更新
AssetDatabase.ImportAsset(PATH);
}
/// <summary>
/// 删除子资源
/// </summary>
[MenuItem("Assets/Remove ChildScriptableObject")]
static void Remove()
{
var parent = AssetDatabase.LoadAssetAtPath<ParentScriptableObject>(PATH);
//アセットの CarentScriptableObject を破棄
Object.DestroyImmediate(parent.child, true);
//破棄したら Missing 状態になるので null を代入
parent.child = null;
//再インポートして最新の情報に更新
AssetDatabase.ImportAsset(PATH);
}
}