-
Notifications
You must be signed in to change notification settings - Fork 0
HideIf & ShowIf Attributes
The [HideIf]
and [ShowIf]
attributes conditionally hide or show fields in the Unity Inspector based on specific conditions. They are useful for creating dynamic Inspector layouts, allowing certain fields to be visible only when specific conditions are met.
-
HideIf: Hides a field when the specified condition is
true
. -
ShowIf: Shows a field when the specified condition is
true
.
The [HideIf]
and [ShowIf]
attributes support the following capabilities:
-
Boolean Field Conditions: Conditions based on a
bool
field in the class. -
Method Conditions: Supports conditions based on a method that returns a
bool
. - Method Parameters: Allows passing parameters to methods referenced in the attribute.
Note: These examples use Unity’s
[SerializeField]
onprivate
fields to promote encapsulation. This approach can help protect internal data from unintended access by other scripts and maintain clean, organized code. For more details, see Unity’s documentation on SerializeField.
Applying [HideIf]
or [ShowIf]
is straightforward. Specify the name of a bool
field or a method that returns bool
in the attribute.
[SerializeField]
[HideIf("conditionName")]
private FieldType fieldName;
[SerializeField]
[ShowIf("conditionName")]
private FieldType fieldName;
// Using a method with parameters to control visibility
[HideIf("methodName", param1, param2, ...)]
In this example, hiddenField
will be hidden in the Inspector when the shouldHide
boolean field is true
.
[SerializeField]
private bool shouldHide;
[SerializeField]
[HideIf("shouldHide")]
private int hiddenField = 20;
Result: When shouldHide
is true
, hiddenField
will be hidden in the Inspector. When shouldHide
is false
, hiddenField
will be visible.
Here, showFieldMethod
is shown based on the result of a method, ReturnShouldShow
, which returns a boolean value.
[SerializeField]
private int number = 10;
[SerializeField]
[ShowIf("ReturnShouldShow")]
private string showFieldMethod = "Visible when condition is met";
private bool ReturnShouldShow() {
return number >= 5;
}
Result: showFieldMethod
will be shown when ReturnShouldShow
returns true
and hidden when it returns false
.
In this example, conditionalField
is hidden based on the result of a method, CheckCondition
, that takes a bool
parameter.
[SerializeField]
private bool shouldHide;
[SerializeField]
[HideIf("CheckCondition", "shouldHide")]
private int conditionalField = 30;
private bool CheckCondition(bool parameter)
{
return parameter;
}
Result: conditionalField
will be hidden if CheckCondition
returns true
based on the value of shouldHide
. When shouldHide
is false
, conditionalField
will be visible.
This example combines [HideIf]
and [ShowIf]
on combinedField
to control its visibility based on both shouldHide
and shouldShow
conditions. The field will only be visible if shouldShow
is true
and shouldHide
is false
.
[SerializeField]
private bool shouldHide;
[SerializeField]
private bool shouldShow;
[SerializeField]
[HideIf("shouldHide"), ShowIf("shouldShow")]
private string combinedField = "Conditionally Visible";
Result: combinedField
will be visible only when shouldShow
is true
and shouldHide
is false
. If shouldHide
is true
or shouldShow
is false
, the field will be hidden.