Search Unity

Attribute to hide variables based on another public bool

Discussion in 'Immediate Mode GUI (IMGUI)' started by _Adriaan, Nov 20, 2015.

  1. _Adriaan

    _Adriaan

    Joined:
    Nov 12, 2009
    Posts:
    481
    Hey there!

    I wrote an Attribute and PropertyDrawer to hide a property when a different boolean property is set to false. It works for regular properties, hooray! But it doesn't for other property types such as arrays or properties with an additional Range attribute. How can I modify my attribute & propertydrawer to hide all properties regardless of its type or other attributes?

    Giant thanks in advance!

    My attribute:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class HideWhenFalseAttribute : PropertyAttribute
    5. {
    6.     public readonly string hideBoolean;
    7.  
    8.     public HideWhenFalseAttribute (string booleanName)
    9.     {
    10.         this.hideBoolean = booleanName;
    11.     }
    12. }
    13.  
    My property drawer:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3. using System.Collections;
    4.  
    5. [CustomPropertyDrawer(typeof(HideWhenFalseAttribute))]
    6. public class HideWhenFalseDrawer : PropertyDrawer
    7. {
    8.  
    9.     public override void OnGUI ( Rect position, SerializedProperty property, GUIContent label)
    10.     {
    11.         HideWhenFalseAttribute hiddenAttribute = attribute as HideWhenFalseAttribute;
    12.         SerializedProperty boolProperty = property.serializedObject.FindProperty(hiddenAttribute.hideBoolean);
    13.  
    14.         if(boolProperty.boolValue)
    15.             EditorGUI.PropertyField(position, property, label, true);
    16.     }
    17.  
    18.     public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
    19.     {
    20.         HideWhenFalseAttribute hiddenAttribute = attribute as HideWhenFalseAttribute;
    21.         SerializedProperty boolProperty = property.serializedObject.FindProperty(hiddenAttribute.hideBoolean);
    22.      
    23.         if(!boolProperty.boolValue)
    24.             return 0f;
    25.  
    26.         return EditorGUI.GetPropertyHeight(property);
    27.     }
    28. }
     
  2. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    I don't think you can use multiple PropertyDrawers for a single field (e.g: using Range and another property drawer).

    Unity will pick up whatever last defined drawer it finds. Since the drawer for the RangeAttribute is found last (in UnityEditor.dll) it will be used, and not your own drawer.

    A "hack" workaround for this is to place your own property drawer inside a .DLL and name it something that is alphabetically sorted AFTER UnityEditor. This is a pure hack and may break in the future if they change how they initialize things...

    Regarding arrays - i think the property drawer gets called once PER ITEM in the array. what is the behaviour you're seeing exactly ?
     
  3. _Adriaan

    _Adriaan

    Joined:
    Nov 12, 2009
    Posts:
    481
    Okay, well, it's okay if I can't apply two attributes I guess. More annoying is the array: my hiding attribute just doesn't hide the array at all. No behaviour change; it doesn't disappear with my code.
     
  4. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    Can you verify that the array does use your drawer? e.g: by placing a breakpoint or putting some debug.log calls to make sure it's picking up when used on that field ?
     
  5. _Adriaan

    _Adriaan

    Joined:
    Nov 12, 2009
    Posts:
    481
    Ah! You were right about the individual parameters (how could I have overlooked this): the drawer draws each individual property of the list, but not the list header / size variable. So... how can I address those?
     
  6. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    I am not familiar with a good solution for this scenario using PropertyDrawers.

    You can implement an inspector (Editor), but that is kind of the opposite from doing a property drawer..
     
  7. _Adriaan

    _Adriaan

    Joined:
    Nov 12, 2009
    Posts:
    481
    The silly thing is that [HideInInspector] does hide arrays, so there is definitely a solution to my problem... I just don't know it yet ;)
     
  8. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    [HideInInspector] is not a Property Drawer... and i dont think it has a matching property drawer.
    It is somehow hard-wired into the way the inspector works. Not the same thing at all.