- Home /
Property Drawers confusion
I've been trying to adapt a snippet of code from the live training tutorials in the property drawers one to use it. This is what I'm using:
public static void OnGUI (Rect position, SerializedProperty property, GUIContent label) {
EditorGUI.BeginProperty (position, label, property);
position = EditorGUI.PrefixLabel (position, GUIUtility.GetControlID(FocusType.Passive), label);
var indent = EditorGUI.indentLevel;
EditorGUI.indentLevel = 0;
Rect amountRect = new Rect(position.x, position.y, 30, position.height);
Rect unitRect = new Rect(position.x + 35, position.y, 50, position.height);
Rect nameRect = new Rect(position.x + 90, position.y, position.width - 90, position.height);
EditorGUI.PropertyField (amountRect, property.FindPropertyRelative("count"), GUIContent.none);
EditorGUI.PropertyField (unitRect, property.FindPropertyRelative("rate"), GUIContent.none);
EditorGUI.PropertyField (nameRect, property.FindPropertyRelative("health"), GUIContent.none);
EditorGUI.indentLevel = indent;
EditorGUI.EndProperty ();
}
I basically want a somewhat array-looking thing but I'm having trouble understanding what the values for the OnGUI function in my snippet are and how they are getting assigned. In the video, it doesn't show that the rect position, the serialized property, or the guicontent are used. I'm not sure what it's asking for the serialized property.
Answer by VesuvianPrime · Feb 10, 2015 at 02:30 AM
The inputs match up to EditorGUI.PropertyField
http://docs.unity3d.com/ScriptReference/EditorGUI.PropertyField.html
Internally Unity uses reflection to determine that it will draw your PropertyAttribute with the associated PropertyDrawer.
Unless using custom editor, Unity will automatically pass a position rect the width of the inspector and a single line height, the field name as a GUIContent label, and the serialized field as a SerializedProperty.
Okay well I am using a custom editor (and so is Buckner in the tutorial) so how would I go about getting it to work?
I recommend following this tutorial: http://catlikecoding.com/unity/tutorials/editor/star/
SerializedProperty is a headache to learn to use at first because it goes against everything good in OOP.
That tutorial was the first thing I read that really made me understand SerializedProperty.
Your answer
Follow this Question
Related Questions
How to change inspector with non-Monobehaviour objects ? 1 Answer
Cannot convert serializedObject to float 1 Answer
Implementing a 2D array as a field on a ScriptableObject? 1 Answer
Creating a custom inspector utilizing a list of class instances with serialization? 1 Answer
Edit existing editor inspector? 1 Answer