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

CustomPropertDrawer noob : simple togle to show different fields

Discussion in 'Scripting' started by Pablomon, Mar 22, 2018.

  1. Pablomon

    Pablomon

    Joined:
    Mar 9, 2015
    Posts:
    47
    Hi, I'am trying to get a custom property drawer that has to show one flied or other depending on toogle button.

    The problems I am facing are:
    - the toogle button gets clicked when clicking anywhere inside the inspector property. The inspector property has a toogle and below it a field. When clicking in the field the toogle gets clicked too.
    - The property field is twice as high as it should.

    This is the result I am getting:
    upload_2018-3-22_19-1-23.png

    And this is the code I am using.
    Code (CSharp):
    1. [CustomPropertyDrawer(typeof(FloatReference))]
    2. public class FloatReferenceDrawer : PropertyDrawer
    3. {
    4.     public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    5.     {
    6.         SerializedProperty useConstant = property.FindPropertyRelative("UseConstant");
    7.         SerializedProperty constValue = property.FindPropertyRelative("ConstValue");
    8.         SerializedProperty variable = property.FindPropertyRelative("Variable");
    9.  
    10.         EditorGUI.BeginProperty(position, label, property);
    11.         // Draw name    
    12.         Rect r = EditorGUI.PrefixLabel(position, label); // returns the space is going the label takes          
    13.         // Draw toggle      
    14.         useConstant.boolValue = EditorGUI.ToggleLeft(r, "Use Constant", useConstant.boolValue);
    15.  
    16.         // Draw value
    17.         r.y += EditorGUIUtility.singleLineHeight; //move the rect down one line height
    18.         if (useConstant.boolValue)
    19.         {
    20.             EditorGUI.FloatField(r, constValue.floatValue);
    21.         }
    22.         else
    23.         {
    24.             Type t = typeof(FloatVariable);
    25.             EditorGUI.ObjectField(r, variable, t);        
    26.         }                                          
    27.  
    28.         EditorGUI.EndProperty();    
    29.     }
    30.  
    31.     public override float GetPropertyHeight(SerializedProperty prop, GUIContent label)
    32.     {    
    33.         return EditorGUIUtility.singleLineHeight *2;
    34.     }
    35. }
    36.  
    Thank you for your time.
     
    Last edited by a moderator: Mar 27, 2018
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I'm very bad at these things, but I manage to get this together.. (modified slightly from yours)
    Code (csharp):
    1.  
    2. public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    3.     {
    4.  
    5.         SerializedProperty useConstant = property.FindPropertyRelative("UseConstant");
    6.  
    7.         SerializedProperty constValue = property.FindPropertyRelative("ConstValue");
    8.  
    9.         SerializedProperty variable = property.FindPropertyRelative("Variable");
    10.  
    11.  
    12.  
    13.         EditorGUI.BeginProperty(position, label, property);
    14.  
    15.         // Draw name      
    16.         float rx = position.x;
    17.         Rect r = EditorGUI.PrefixLabel(position, label); // returns the space is going the label takes            
    18.  
    19.  
    20.         useConstant.boolValue = EditorGUI.ToggleLeft(r, "Use Constant", useConstant.boolValue);
    21.        
    22.         r.x = rx;
    23.         r.y += 18;
    24.         r.height = 18;
    25.      
    26.         if (useConstant.boolValue)
    27.             EditorGUI.PropertyField(r, constValue);
    28.         else EditorGUI.PropertyField(r, variable);
    29.  
    30.         EditorGUI.EndProperty();
    31.  
    32.     }
    33.     public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
    34.     {
    35.         return 18*2;
    36.     }
    I have no idea if that's what you wanted. ;) Maybe it's helpful.
     
  3. Pablomon

    Pablomon

    Joined:
    Mar 9, 2015
    Posts:
    47
    Thanks for the reply!

    r.height did it and the field no longer overlaps the next property, thanks.

    The problem of the toggle remains though. Wherever I click in the height of the property operates the toogle button.

    upload_2018-3-23_13-34-56.png

    Clicking where the arrow points operates the toogle so I can't ever change the numeric value...

    Bonus question:
    For the float field I use EditorGUI.FloatField(r, constValue.floatValue) and it displays only the value.
    But for the variable field I tried EditorGUI.ObjectField(r, variable) and EditorGUI.PropertyField(r, variable) and both draw the name of the property. Is there a way I can get rid of the "variable" text?

    upload_2018-3-23_13-41-26.png
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Sorry, I'm pretty clueless about that stuff. :) Kinda just got half lucky helping you out there. heh

    I don't think mine was forcing the toggle being clicked, but you have 2 input areas.. and I had one -- I thought you only wanted 1, toggled?

    Someday I'd like to get better at the editor scripting stuff.. could be someday soon, or far away.. who knows :)
     
  5. Pablomon

    Pablomon

    Joined:
    Mar 9, 2015
    Posts:
    47
    Hey methos5k got it working!



    So I found the problem with the toogle being clicked anywhere was the rect it was given.
    If I give it a rect with the exact width it works. Same idea you pointed me too before.

    r.width = 110;
    useConstant.boolValue = EditorGUI.ToggleLeft(r, "Use Constant", useConstant.boolValue);

    So now the float field can be changed.
    upload_2018-3-23_14-27-44.png

    As for the bonus question I used the method ObjectField(Rect position, string label, Object obj, Type objType, bool allowSceneObjects) with a label equal to "". Now it looks like this:
    upload_2018-3-23_14-37-8.png

    Since this drawer works for a trendy pattern I am pasting the whole code for others to use.

    Cheers!

    Code (CSharp):
    1. using UnityEditor;
    2. using UnityEngine;
    3.  
    4. [CustomPropertyDrawer(typeof(FloatReference))]
    5. public class FloatReferenceDrawer : PropertyDrawer
    6. {
    7.  
    8.     public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    9.     {
    10.  
    11.         SerializedProperty useConstant = property.FindPropertyRelative("UseConstant");
    12.  
    13.         SerializedProperty constValue = property.FindPropertyRelative("ConstValue");
    14.  
    15.         SerializedProperty variable = property.FindPropertyRelative("Variable");
    16.  
    17.  
    18.  
    19.         EditorGUI.BeginProperty(position, label, property);
    20.  
    21.         Rect r;
    22.         // Draw name        
    23.         r = EditorGUI.PrefixLabel(position, label); // returns the space is going the label takes      
    24.  
    25.         r.width = 90;
    26.         useConstant.boolValue = EditorGUI.ToggleLeft(r, "Use Constant", useConstant.boolValue);
    27.  
    28.         r.xMin += 95;
    29.         r.xMax = position.xMax;
    30.         r.height = EditorGUIUtility.singleLineHeight;
    31.  
    32.         if (useConstant.boolValue)
    33.         {
    34.             constValue.floatValue = EditorGUI.FloatField(r, constValue.floatValue);
    35.         }
    36.         else
    37.         {
    38.             //EditorGUI.ObjectField(r, variable);
    39.             variable.objectReferenceValue = EditorGUI.ObjectField(r, "", variable.objectReferenceValue, typeof(FloatVariable), false);
    40.         }
    41.  
    42.         EditorGUI.EndProperty();
    43.  
    44.     }
    45.  
    46.     public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
    47.     {
    48.         return EditorGUIUtility.singleLineHeight ;
    49.     }
    50. }
     
    Last edited: Mar 26, 2018
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
  7. Pablomon

    Pablomon

    Joined:
    Mar 9, 2015
    Posts:
    47