
Conditional Attributes
Some of our public variables logically depends on the value of others. When some variable is unchecked (false), other variable has no meaning. For example: Player class containing canJump boolean with jumpHeight float. We don’t have to display jumpHeight when Player canJump is false etc…
Even if these variables can be grouped by Header, they are still there and visible.
Imagine example script:
1 2 3 4 5 6 |
public class Enemy : MonoBehaviour { public bool canDealDamage = false; public float damage = 10.0f; } |
In the example script, the damage variable is relevant only when canDealDamage is true.
Therefore, there is no need to display the damage variable in the Inspector window, when canDealDamage is false.
To solve this, we can make our custom Property attributes and hook them with Property drawer.
Property attribute allows us to use our custom attribute in the Unity script. We will call this attribute ShowWhenAttribute.
Then we will make ShowWhenAttributeDrawer, and in the OnGUI function we will draw the property only if the referenced property is visible.
ShowWhenAttribute
This is our custom attribute, that contains 3 basic values:
- name of the referenced variable
- condition (optional)
- value (optional)
simple usage:
1 2 3 4 5 6 7 |
public class Enemy : MonoBehaviour { public bool canDealDamage = false; [ShowWhen("canDealDamage")] public float damage = 10.0f; } |
usage with condition and value:
1 2 3 4 5 6 7 8 9 10 |
public class Enemy : MonoBehaviour { public bool canDealDamage = false; [ShowWhen("canDealDamage")] public float damage = 10.0f; [ShowWhen("damage", Condition.Greater, 5.0f)] public bool showsWhenDamageGreater5 = false; } |
We can use 4 conditions:
- Condition.Equals
- Condition.NotEqual
- Condition.Greater
- Condition.Less
The variable is shown, when: <referenced_variable_value> <condition> <value>. In the provided example, the showsWhenDamageGreater5 is shown when damage is greater than 5.
[ShowWhen(“name”)] is the same as [ShowWhen(“name”, Condition.Equals, true)]
How its done
- the custom Property drawer (ConditionalPropertyDrawer) finds SerializedProperty of the ShowWhenAttribute
- from the SerializedProperty, the target object Type is found using: property.serializedObject.targetObject.GetType();
- using C# reflection, the FieldInfo[] of given Type is found
- then the SerializedProperty boolValue/floatValue/intValue/… is checked with ShowWhenAttribute values using condition for each field
- when the condition is not met, the OnGUI function returns immediatelly and does not draw anythnig
Supported types
- bool
- float
- int
- string
- enum
- object reference value
Full source code with example scene:
Edit 2022: I’ve found that my BitBucket repo has been deleted. Moved the source to the GitHub…
Available here: https://github.com/ssincak/conditional_attributes
2 thoughts on “Conditional Attributes”
Comments are closed.
I was just about to implement a ConditionalAttribute! Thankfully I found this, thanks a ton – it’s awesome!
cccam server
Hello,nice share.