- Home /
Intractable CustomPropertyDrawer
Hi ( This is a UnityEditor question )
I am using CustomPropertyDrawer and need access to the class being drawn.
I use Reflection to get the instance from serializedObject's targetObject.
TestProperty testProperty = (TestProperty)fieldInfo.GetValue (property.serializedObject.targetObject);
Problem :
It doesn't work when it is a field within another serialized class, nor does it work in an array. This is because the serializedObject's targetObject is always the MonoBehaviour.
Question :
How do I get access to the instance of what the PropertyDrawer is drawing ?
Let's replicate the core of my code :
(1) Here is the class we'll be using for testing :
[System.Serializable]public class TestProperty{}
(2) For now, we'll displaying the type of the serialized property :
[CustomPropertyDrawer (typeof(TestProperty), true)]
public class CustomPropertyDrawer_PropertyField : PropertyDrawer
{
public override void OnGUI (Rect position, SerializedProperty property, GUIContent label)
{
position = EditorGUI.PrefixLabel (position, GUIUtility.GetControlID (FocusType.Passive), label);
// Display something useful
string labelValue = property.type.ToString ();
EditorGUI.LabelField (position, labelValue, EditorStyles.label);
}
}
(3) And then to test it, we place this in some random MonoBehaviour
public TestProperty testField;
public TestProperty[] testFieldArray = new TestProperty[3];
Your answer
Follow this Question
Related Questions
How to create bitmask flags per scene that are available in the editor? 0 Answers
HingeJoint2D anchor point disappered 0 Answers
Expanding a custom property drawer of a list item forces further items in the list to disappear. 2 Answers
A List of all Property Drawers 1 Answer
Unity UI not refresh. 0 Answers