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:
using UnityEngine;
using System.Collections;
public class HideWhenFalseAttribute : PropertyAttribute
{
public readonly string hideBoolean;
public HideWhenFalseAttribute (string booleanName)
{
this.hideBoolean = booleanName;
}
}
My property drawer:
using UnityEngine;
using UnityEditor;
using System.Collections;
[CustomPropertyDrawer(typeof(HideWhenFalseAttribute))]
public class HideWhenFalseDrawer : PropertyDrawer
{
public override void OnGUI ( Rect position, SerializedProperty property, GUIContent label)
{
HideWhenFalseAttribute hiddenAttribute = attribute as HideWhenFalseAttribute;
SerializedProperty boolProperty = property.serializedObject.FindProperty(hiddenAttribute.hideBoolean);
if(boolProperty.boolValue)
EditorGUI.PropertyField(position, property, label, true);
}
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
HideWhenFalseAttribute hiddenAttribute = attribute as HideWhenFalseAttribute;
SerializedProperty boolProperty = property.serializedObject.FindProperty(hiddenAttribute.hideBoolean);
if(!boolProperty.boolValue)
return 0f;
return EditorGUI.GetPropertyHeight(property);
}
}
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 ?
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.
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 ?
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?
[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.