【发布时间】:2021-12-27 23:11:31
【问题描述】:
我有一个 MonoBehaviour 的自定义编辑器来显示可重新排序的项目列表。
public class MyComponent : MonoBehaviour
{
public MyArrayElement[] myList;
}
public struct MyArrayElement
{
public string firstField;
public string secondField;
}
[CustomEditor(typeof(MyComponent))]
public class MyComponentEditor : Editor
{
private ReorderableList list;
private void OnEnable()
{
SerializedProperty property = this.serializedObject.FindProperty("myList");
this.list = new ReorderableList(this.serializedObject, property, true, true, true, true);
list.drawElementCallback = DrawListItems;
}
public override void OnInspectorGUI()
{
serializedObject.Update();
list.DoLayoutList();
serializedObject.ApplyModifiedProperties();
}
void DrawListItems(Rect rect, int index, bool isActive, bool isFocused)
{
EditorGUI.PropertyField(
new Rect(rect.x, rect.y, 100, EditorGUIUtility.singleLineHeight),
element.FindPropertyRelative("firstField"),
GUIContent.none);
EditorGUI.PropertyField(
new Rect(rect.x + 150, rect.y, 100, EditorGUIUtility.singleLineHeight),
element.FindPropertyRelative("secondField"),
GUIContent.none);
}
}
这可以正常工作。但是,我想让这个 MonoBehaviour 的每个实例的 Inspector 编辑相同的元素集,所以我创建了一个 ScriptableObject
public MyScriptableObject : ScriptableObject
{
public MyArrayElement[] myList;
}
然后将MyComponent.myList替换为MyScriptableObject的实例
public class MyComponent : MonoBehaviour
{
// Remove this
// public MyArrayElement[] myList;
// Add this
public MyScriptableObject myScriptableObject;
}
然后我想更新 MonoBehaviour 的自定义编辑器以显示 myScriptableObject.myList
我试过了,但是 Inspector 中的列表是空的,即使 ScriptableObject 的列表不为空
SerializedProperty property = this.serializedObject.FindProperty("myScriptableObject").FindPropertyRelative("myList");
this.list = new ReorderableList(this.serializedObject, property, true, true, true, true);
有没有办法让我的MonoBehaviours 编辑器让我编辑ScriptableObjects 数组?
【问题讨论】: