#CustomPropertyDrawer fails to call OnGUI in some cases

1 messages · Page 1 of 1 (latest)

normal latch
#

Hey guys, I was messing around with custom property drawers and I was using one that, when serialized, selectively hides a particular property if another value is set to false (i.e. have a property to enable or disable a different property in the inspector). I've used this in two separate places in my project and for some reason, in one instance, the OnGUI method of the property drawer seems to never be called, causing the hiding mechanic of the custom attribute to completely fail.
I have seriously not the faintest clue why this would happen, and I was hoping I could receive some guidance here. Thanks.

#

Here are the relevant scripts:

// custom property drawer script
#if UNITY_EDITOR
using UnityEngine;
using UnityEditor;

public class ShowIfAttribute : PropertyAttribute
{
    public string conditionField;

    public ShowIfAttribute(string conditionField)
    {
        this.conditionField = conditionField;
    }
}
[CustomPropertyDrawer(typeof(ShowIfAttribute))]
public class ShowIfDrawer : PropertyDrawer
{
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        ShowIfAttribute showIf = (ShowIfAttribute)attribute;
        SerializedProperty condition = property.serializedObject.FindProperty(showIf.conditionField);
        Debug.Log(label.text); // only ever prints "Stats"; never "Recursions"
        bool shouldShow = true;

        if (condition != null)
        {
            if (condition.propertyType == SerializedPropertyType.Boolean)
                shouldShow = condition.boolValue;
            else
                Debug.LogWarning($"ShowIf works only with bool fields. '{showIf.conditionField}' is not a bool.");
        }

        if (shouldShow)
            EditorGUI.PropertyField(position, property, label, true);
    }
#
// instance where "showif" attribute works
public class Card : ScriptableObject
{

    [NonSerialized] public bool conditionPassed = false;
    public string cardName;
    public string desc;
    public int cost;
    public bool isConstruct;
    [Tooltip("Stats that apply to the card if it is a construct."), ShowIf("isConstruct")] // this is the custom attribute in question
    public ConstructStats stats;
    public CardEffect[] cardEffects;
}
// instance where "showif" doesnt work
[Serializable]
public class CardEffect 
{
    [CustomLabel("Event"), Tooltip("Event that triggers the card effect.")]
    public CardEvent eventRef;
    public List<Subscriber> cardEvent;
    [Tooltip("List of conditions that must evaluate to true for the effect to activate.")]
    public Condition[] conditions;
    [CustomLabel("Effect"), Tooltip("Function that is invoked upon card effect triggering.")]
    public EffectRef effectRef;
    public Action<ParamObject, FuncPointer> effect;
    public bool recursive;
    [Tooltip("How many times the effect and condition will run recursively."), Range(1, 16), ShowIf("recursive")] // here it doesnt work for some reason
    public byte recursions = 1;
    [Tooltip("Parameters that will be used for the card effect function if the Param Type for the Func Pointer is set to \"Func Params\"")]
    public int[] funcParams;
    public FuncPointer funcPointer;
}