Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Buttons inline with Properties in custom PropertyDrawer

Discussion in 'Immediate Mode GUI (IMGUI)' started by HoloGuy, Feb 6, 2017.

  1. HoloGuy

    HoloGuy

    Joined:
    Oct 4, 2016
    Posts:
    11
    Hey guys!

    I'm looking for a method to display buttons inline (or better in the next line) of my custom PropertyDrawer. But I was not able to figure out yet how to do it.

    gesture.PNG

    As you can see, the buttons belonging to a single property, are drawn below the last property and the GUILayout.Space is also applied here. For my understanding, an issue could be that EditorGUI.BeginProperty(...) / .EndProperty is not compatible with GUILayout. But I don't see another option to add buttons to the PropertyDrawer...

    The code:

    Code (CSharp):
    1. using UnityEditor;
    2. using UnityEngine;
    3.  
    4. [CustomPropertyDrawer(typeof(TrainingGestureData))]
    5. public class TrainingGestureDataDrawer : PropertyDrawer
    6. {
    7.     public enum placeholder
    8.     {
    9.         oneHanded,
    10.         twoHanded
    11.     }
    12.  
    13.     private placeholder handiness;
    14.  
    15.     public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    16.     {
    17.         SerializedProperty gestureId = property.FindPropertyRelative("GestureID");
    18.         SerializedProperty gestureName = property.FindPropertyRelative("GestureName");
    19.         SerializedProperty inputData = property.FindPropertyRelative("InputData");
    20.  
    21.  
    22.         EditorGUI.BeginProperty(position, label, property);
    23.  
    24.         int ident = EditorGUI.indentLevel;
    25.         EditorGUI.indentLevel = 0;
    26.  
    27.         // Calculate rects
    28.         Rect labelRect = new Rect(position.x, position.y, 20, position.height);
    29.         Rect nameRect = new Rect(position.x + 25, position.y, 150, position.height);
    30.         Rect handinessRect = new Rect(position.x + 180, position.y, 100, position.height);
    31.         Rect numberRect = new Rect(position.x + 285, position.y, 120, position.height);
    32.  
    33.         // Draw fields (use GUIContent.none to disable label)
    34.         EditorGUI.LabelField(labelRect, gestureId.intValue.ToString());
    35.         EditorGUI.PropertyField(nameRect, gestureName, GUIContent.none);
    36.         handiness = (placeholder)EditorGUI.EnumPopup(handinessRect, handiness);
    37.         EditorGUI.LabelField(numberRect, "Recorded inputs: " + inputData.arraySize);
    38.  
    39.         Rect buttonsRect = EditorGUILayout.BeginHorizontal();
    40.  
    41.         // Get object
    42.         object trainingGestureObj = fieldInfo.GetValue(property.serializedObject.targetObject);
    43.  
    44.         if (GUILayout.Button("Train Gesture"))
    45.         {
    46.             Debug.Log(gestureId.intValue);
    47.         }
    48.         GUILayout.Button("Remove Last Input");
    49.         GUILayout.Button("Clear All Inputs");
    50.  
    51.         EditorGUILayout.EndHorizontal();
    52.  
    53.         EditorGUI.indentLevel = ident;
    54.  
    55.         EditorGUI.EndProperty();
    56.  
    57.         GUILayout.Space(10);
    58.     }
    59. }
    Btw: Is it correct, that GUIEditor, etc. is just supported as legacy code? If yes, what's the "new way" to create PropertyDrawers then?
     
  2. PsyKaw

    PsyKaw

    Joined:
    Aug 16, 2012
    Posts:
    102
    You can't use GUILayout or EditorGUILayout in Property Drawers, you have to calculate rects for the buttons and use GUI.Button(rect, label);

    GUIEditor, ect is supported as legacy code for make ingame UI but not for editor.