Class EnableIfAttribute
Attribute used to conditionally enable a property in the Unity Inspector. Supports single or multiple boolean condition fields using AND/OR logic.
public class EnableIfAttribute : PropertyAttribute
Inheritance
Derived
Examples
Enable a field only when "IsEnabled" is true:
public bool IsEnabled;
[EnableIf(nameof(IsEnabled))]
public int EnabledOnlyField;
Enable a field when both "A" and "B" are true:
public bool A, B;
[EnableIf(EConditionOperator.And, 0, nameof(A), nameof(B))]
public int EnabledWhenAB;
Remarks
Use this attribute to enable a field only when one or more conditions are met. It evaluates the condition(s) by looking up boolean fields in the same serialized object.
Constructors
EnableIfAttribute(string, int)
Creates an EnableIfAttribute using a single condition name.
public EnableIfAttribute(string conditionName, int order = 0)
Parameters
| Name | Type | Description |
|---|---|---|
conditionName | string | Name of the boolean property to evaluate. |
order | int | Optional render order in the inspector. |
EnableIfAttribute(EConditionOperator, int, params string[])
Creates an EnableIfAttribute using multiple condition names and an operator.
public EnableIfAttribute(EConditionOperator operatorType, int order = 0, params string[] conditionNames)
Parameters
| Name | Type | Description |
|---|---|---|
operatorType | EConditionOperator | The logical operator to use (AND/OR). |
order | int | Optional render order in the inspector. |
conditionNames | string[] | Names of the boolean properties to evaluate. |
Fields
ConditionNames
The names of the boolean fields used to determine if the property should be enabled.
public string[] ConditionNames
Field Value
string[]
Operator
The operator to apply when evaluating multiple conditions.
public EConditionOperator Operator
Field Value
Methods
GetCondition(SerializedProperty)
Evaluates the condition(s) using the SerializedProperty context.
public bool GetCondition(SerializedProperty property)
Parameters
| Name | Type | Description |
|---|---|---|
property | SerializedProperty | The property being evaluated. |
Returns
bool — True if enabled; otherwise, false.