HideIf & ShowIf
Overview
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
.
Capabilities
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.
Syntax and Examples
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.
Basic Syntax
[SerializeField]
[HideIf("conditionName")]
private FieldType fieldName;
[SerializeField]
[ShowIf("conditionName")]
private FieldType fieldName;
Additional Constructor Syntax
// Using a method with parameters to control visibility
[HideIf("methodName", param1, param2, ...)]
Example 1: Hiding a Field Based on a Boolean Field
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.
Example 2: Showing a Field Based on a Method Condition
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
.
Example 3: Using Method Parameters for Conditional Visibility
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.
Example 4: Combining HideIf and ShowIf for Complex Conditions
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.