- Home /
Unable to draw propertyfield in inspector for some classes, findProperty returns null.
I am trying to create an editor inspector for a custom class. However, I'm having trouble drawing some of the property fields. This is the object class I need to display in the inspector:
using UnityEngine;
using System.Collections;
public class TestScript : MonoBehaviour
{
public GUIStyle style = new GUIStyle();
public GUIContent content = new GUIContent();
public int number = 5;
public TestScript(){}
}
Using this editor script: using UnityEngine; using UnityEditor; using System.Collections.Generic;
[CustomEditor(typeof(TestScript))]
public class TestEditor : Editor
{
public override void OnInspectorGUI()
{
// DrawDefaultInspector(); <- Works as expected.
//These don't work, however (only the number property field).
EditorGUILayout.PropertyField(serializedObject.FindProperty("number"),new GUILayoutOption[]{});
EditorGUILayout.PropertyField(serializedObject.FindProperty("style"),new GUILayoutOption[]{});
EditorGUILayout.PropertyField(serializedObject.FindProperty("content"),new GUILayoutOption[]{});
}
}
FindProperty for the GUIStyle and GUIContent returns null. Why? How does the default inspector draw them correctly?
Interesting. I quit working with SerializedProperties ages ago, but if I remember correctly, you can't get it to give you back a direct reference to a System.Object (GUIContent/GUIStyle don't inherit UnityEngine.Object) even if the class was Serializable (which GUIStyle/GUIContent are) - I think the default editor does this recursive drawing which handles the drawing:
serializedObject.Update();
SerializedProperty iterator = serializedObject.GetIterator();
bool enterChildren = true;
while (iterator.NextVisible(enterChildren))
{
EditorGUILayout.PropertyField(iterator, true, new GUILayoutOption[0]);
enterChildren = false;
}
serializedObject.Apply$$anonymous$$odifiedProperties();