Search Unity

How to combine Conditional Property Hide and other custom Property Drawers?

Discussion in 'Immediate Mode GUI (IMGUI)' started by laurentlavigne, Jul 1, 2016.

  1. laurentlavigne

    laurentlavigne

    Joined:
    Aug 16, 2012
    Posts:
    6,363
    When I add [ConditionalHide] on a HalfRange HalfRange property drawer no longer draws the struct.
    Also [ConditionalHide("AIOn",true)] above [Header("Moods")] won't hide the header when AIOn if false
    What's the way to make these work together?

    Code (CSharp):
    1. [System.Serializable]
    2. public struct HalfRange
    3. {
    4.     public float maximum;
    5.     public float value;
    6.     public HalfRange(float value, float max){
    7.         this.maximum = max;
    8.         this.value = value;
    9.     }
    10. }
    property drawer for the half range.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3.  
    4. [CustomPropertyDrawer(typeof(HalfRange))]
    5. public class RangeDrawer : PropertyDrawer
    6. {
    7.     private const float FieldWidth = 30.0f;
    8.  
    9.     public override void OnGUI(Rect pos, SerializedProperty property, GUIContent label)
    10.     {
    11.         EditorGUI.BeginProperty(pos, label, property);
    12.         {
    13.             // Label
    14.             pos = EditorGUI.PrefixLabel(pos, label);
    15.  
    16.             // Child objects shouldn't be indented
    17.             int indent = EditorGUI.indentLevel;
    18.             EditorGUI.indentLevel = 0;
    19.  
    20.             // Create rects
    21. //            Rect minRect = new Rect(pos.x, pos.y, FieldWidth, pos.height);
    22.             Rect sliderRect = new Rect(pos.x -30 , pos.y, Mathf.Max(0.0f, pos.width - FieldWidth+30), pos.height);
    23.             Rect maxRect = new Rect(sliderRect.xMax, pos.y, FieldWidth, pos.height);
    24.  
    25. //            SerializedProperty spMin = property.FindPropertyRelative("minimum");
    26.             SerializedProperty spMax = property.FindPropertyRelative("maximum");
    27.             SerializedProperty spValue = property.FindPropertyRelative("value");
    28.  
    29.             // Minimum
    30. //            spMin.floatValue = EditorGUI.FloatField(minRect, spMin.floatValue);
    31. //            spMin.floatValue = Mathf.Min(spMin.floatValue, spMax.floatValue);
    32.  
    33.             // Maximum
    34.             spMax.floatValue = EditorGUI.FloatField(maxRect, spMax.floatValue);
    35.             spMax.floatValue = Mathf.Max(spMax.floatValue, 0);
    36.  
    37.             // Value slider          
    38.             spValue.floatValue = EditorGUI.Slider(sliderRect, spValue.floatValue, 0, spMax.floatValue);
    39.  
    40.             // Reset indenting
    41.             EditorGUI.indentLevel = indent;
    42.         }
    43.         EditorGUI.EndProperty();
    44.     }
    45. }
    conditional hide attribute

    Code (CSharp):
    1. using UnityEngine;
    2. using System;
    3. using System.Collections;
    4.  
    5. //Original version of the ConditionalHideAttribute created by Brecht Lecluyse (www.brechtos.com)
    6. //Modified by: -
    7.  
    8. [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property |
    9.     AttributeTargets.Class | AttributeTargets.Struct, Inherited = true)]
    10. public class ConditionalHide : PropertyAttribute
    11. {
    12.     public string ConditionalSourceField = "";
    13.     public string ConditionalSourceField2 = "";
    14.     public bool HideInInspector = false;
    15.     public bool Inverse = false;
    16.  
    17.     // Use this for initialization
    18.     public ConditionalHide(string conditionalSourceField)
    19.     {
    20.         this.ConditionalSourceField = conditionalSourceField;
    21.         this.HideInInspector = false;
    22.         this.Inverse = false;
    23.     }
    24.  
    25.     public ConditionalHide(string conditionalSourceField, bool hideInInspector)
    26.     {
    27.         this.ConditionalSourceField = conditionalSourceField;
    28.         this.HideInInspector = hideInInspector;
    29.         this.Inverse = false;
    30.     }
    31.  
    32.     public ConditionalHide(string conditionalSourceField, bool hideInInspector, bool inverse)
    33.     {
    34.         this.ConditionalSourceField = conditionalSourceField;
    35.         this.HideInInspector = hideInInspector;
    36.         this.Inverse = inverse;
    37.     }
    38.  
    39. }
    conditional hide property drawer

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3.  
    4. //Original version of the ConditionalHideAttribute created by Brecht Lecluyse (www.brechtos.com)
    5. //Modified by: -
    6.  
    7. [CustomPropertyDrawer(typeof(ConditionalHide))]
    8. public class ConditionalHidePropertyDrawer : PropertyDrawer
    9. {
    10.     public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    11.     {
    12.        
    13.         ConditionalHide condHAtt = (ConditionalHide)attribute;
    14.         bool enabled = GetConditionalHideAttributeResult(condHAtt, property);      
    15.  
    16.         bool wasEnabled = GUI.enabled;
    17.         GUI.enabled = enabled;
    18.         if (!condHAtt.HideInInspector || enabled)
    19.         {
    20.             EditorGUI.PropertyField(position, property, label, true);
    21.         }      
    22.  
    23.         GUI.enabled = wasEnabled;
    24.     }
    25.  
    26.     public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
    27.     {
    28.         ConditionalHide condHAtt = (ConditionalHide)attribute;
    29.         bool enabled = GetConditionalHideAttributeResult(condHAtt, property);
    30.  
    31.         if (!condHAtt.HideInInspector || enabled)
    32.         {
    33.         return EditorGUI.GetPropertyHeight(property, label);
    34.         }
    35.         else
    36.         {
    37.             //The property is not being drawn
    38.             //We want to undo the spacing added before and after the property
    39.             return -EditorGUIUtility.standardVerticalSpacing;
    40.             //return 0.0f;
    41.         }
    42.  
    43.  
    44.         /*
    45.         //Get the base height when not expanded
    46.         var height = base.GetPropertyHeight(property, label);
    47.  
    48.         // if the property is expanded go thru all its children and get their height
    49.         if (property.isExpanded)
    50.         {
    51.             var propEnum = property.GetEnumerator();
    52.             while (propEnum.MoveNext())
    53.                 height += EditorGUI.GetPropertyHeight((SerializedProperty)propEnum.Current, GUIContent.none, true);
    54.         }
    55.         return height;*/
    56.     }
    57.  
    58.     private bool GetConditionalHideAttributeResult(ConditionalHide condHAtt, SerializedProperty property)
    59.     {
    60.         bool enabled = true;
    61.         SerializedProperty sourcePropertyValue = property.serializedObject.FindProperty(condHAtt.ConditionalSourceField);
    62.         if (sourcePropertyValue != null)
    63.         {
    64.             enabled = CheckPropertyType(sourcePropertyValue);              
    65.         }
    66.         else
    67.         {
    68.             Debug.LogWarning("Attempting to use a ConditionalHideAttribute but no matching SourcePropertyValue found in object: " + condHAtt.ConditionalSourceField);
    69.         }
    70.  
    71.         SerializedProperty sourcePropertyValue2 = property.serializedObject.FindProperty(condHAtt.ConditionalSourceField2);
    72.         if (sourcePropertyValue2 != null)
    73.         {
    74.             enabled = enabled && CheckPropertyType(sourcePropertyValue2);
    75.         }
    76.         else
    77.         {
    78.             //Debug.LogWarning("Attempting to use a ConditionalHideAttribute but no matching SourcePropertyValue found in object: " + condHAtt.ConditionalSourceField);
    79.         }
    80.  
    81.         if (condHAtt.Inverse) enabled = !enabled;
    82.  
    83.         return enabled;
    84.     }
    85.  
    86.     private bool CheckPropertyType(SerializedProperty sourcePropertyValue)
    87.     {
    88.         switch (sourcePropertyValue.propertyType)
    89.         {              
    90.             case SerializedPropertyType.Boolean:
    91.                 return sourcePropertyValue.boolValue;              
    92.             case SerializedPropertyType.ObjectReference:
    93.                 return sourcePropertyValue.objectReferenceValue != null;              
    94.             default:
    95.                 Debug.LogError("Data type of the property used for conditional hiding [" + sourcePropertyValue.propertyType + "] is currently not supported");
    96.                 return true;
    97.         }
    98.     }
    99. }
    100.