I have what I thought would be a simple situation, but I can’t seem to get a custom editor to work.
I have a fairly complex ScriptableObject in my real project, but for testing I created a very simple stand-in:
public class TestData : ScriptableObject {
public string s1;
public float f1 = 3.14159f;
}
An instance of this class is a public property of my component class. Again, I have made a simplified version for testing:
public class TestComponent : MonoBehaviour {
[SerializeField]
public TestData tData = TestData.CreateInstance<TestData>();
}
I have a custom editor for the inner class:
[CustomEditor(typeof(TestData))]
[CanEditMultipleObjects]
public class TestDataEditor : Editor {
SerializedProperty s1;
SerializedProperty f1;
public override void OnInspectorGUI() {
Debug.Log("TestDataEditor.OnInspGUI");
base.OnInspectorGUI();
EditorGUILayout.LabelField("TestDataEditor begins");
s1 = serializedObject.FindProperty("s1");
f1 = serializedObject.FindProperty("f1");
EditorGUILayout.PropertyField(s1);
EditorGUILayout.PropertyField(f1);
EditorGUILayout.LabelField("TestDataEdito ends");
}
}
Finally, there is a custom editor for the outer class:
[CustomEditor(typeof(TestComponent))]
[CanEditMultipleObjects]
public class TestEditor : Editor {
Editor tDataEditor;
SerializedProperty tData;
public override void OnInspectorGUI() {
tData = serializedObject.FindProperty("tDdata");
EditorGUILayout.LabelField("This is TestEditor");
if (tData != null) {
if (tDataEditor == null) {
tDataEditor = Editor.CreateEditor((Object) tData.objectReferenceValue);
}
tDataEditor.OnInspectorGUI();
}
}
}
When I create a GameObject with the TestComponent attached, I see the LabelField and its message from the outer editor, but the FindProperty(“tData”) is always returning null. This means the rest of the OnInspectorGUI() logic is skipped.
is there a “right way” to do this, other than what I am trying to do? I also am not clear on what parameter I should be passing to Editor.CreateEditor(), so I would appreciate any advice on that.
I have been all over the forums and search engines, and so far every solution I’ve seen posted for something akin to this has not worked with current Unity versions (I’m running 5.3.4). I’ve spent many hours trying to solve this, and I’m finally admitting I’m stumped.
For what it’s worth, if I turn the real version of my ScriptableObject into a MonoBehaviour, I have a working custom editor for that more complex situation that is doing just fine. What I am attempting to do is to take my MonoBehaviour and refactor it as a ScriptableObject to allow it to be used away from a GameObject context.
Thanks in advance to anyone who can help.