Skip to main content

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​

object→Attribute→PropertyAttribute→EnableIfAttribute

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​

NameTypeDescription
conditionNamestringName of the boolean property to evaluate.
orderintOptional 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​

NameTypeDescription
operatorTypeEConditionOperatorThe logical operator to use (AND/OR).
orderintOptional render order in the inspector.
conditionNamesstring[]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​

EConditionOperator

Methods​

GetCondition(SerializedProperty)​

Evaluates the condition(s) using the SerializedProperty context.

public bool GetCondition(SerializedProperty property)

Parameters​

NameTypeDescription
propertySerializedPropertyThe property being evaluated.

Returns​

bool — True if enabled; otherwise, false.