I have a custom editor for a MonoBehaviour
to display a reorderable list of items.
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);
}
}
This works correctly. However, I would like to have the Inspector for every instance of this MonoBehaviour
edit the same set of elements, so I created a ScriptableObject
public MyScriptableObject : ScriptableObject
{
public MyArrayElement[] myList;
}
And then replace MyComponent.myList
with an instance of MyScriptableObject
public class MyComponent : MonoBehaviour
{
// Remove this
// public MyArrayElement[] myList;
// Add this
public MyScriptableObject myScriptableObject;
}
Then I want to update my custom editor for the MonoBehaviour
to show myScriptableObject.myList
I tried this, but the list is empty in the Inspector, even if the ScriptableObject
's list is not empty
SerializedProperty property = this.serializedObject.FindProperty("myScriptableObject").FindPropertyRelative("myList");
this.list = new ReorderableList(this.serializedObject, property, true, true, true, true);
Is there a way to get my MonoBehaviour
s editor to let me edit the ScriptableObject
s array?