ShowIf

  06. Attributes No Comments

Attribute that can be used to make class member that follows the attribute only be shown in the inspector when a predicate statement is true.

ShowIf(string classMemberName, object requiredValue)

When this constructor is used, the target is only shown if the class member with the given name has the specified value.

ShowIf(string classMemberName, Is comparison, object requiredValue)

When this constructor is used, the target is only shown if the class member with the given name passes the comparison check against the specified value.

ShowIf(string classMemberName, string comparison, object requiredValue)

When this constructor is used, the target is only shown if the class member with the given name passes the comparison check against the specified value.
Accepted comparison values are: ==, !=, <, >, <= and >=. They mean equal, not equal, smaller than, larger than, smaller than or equal to and larger than or equal to respectively.

ShowIf(string booleanExpression)

When this constructor is used, the target is only shown if the boolean expression is true.
With this constructor it is possible to evaluate expressions containing multiple parts and containing conjunctions and parentheses.
Supported conjunctions are & (and) and | (or).

Attribute Target

Field, property, method or indexer.

Example 1

using UnityEngine;
using Sisus.Attributes;

public class HideAndSeek : MonoBehaviour
{
	public bool foundYou;
		
	[ShowIf("foundYou", true)]
	private void Hide()
	{
		foundYou = false;
	}
}

Example 1 Result

 

Example 2

using UnityEngine;
using Sisus.Attributes;

public class NothingToSeeHere : MonoBehaviour
{
	public bool f;
	public bool u;
	public bool n;
	public bool c;
	public bool k;
	public bool y;
	public bool h;
	public bool o;
	public bool v;
	public bool e;
	public bool r;

	[ReadOnly, ShowIf("f|u|n|c|k|y|h|o|v|e|r")]
	private string selectedLetters;

	[ReadOnly, ShowIf("u & n & c & o & v & e & r & !f & !k & !y & !h")]
	private void SecretButton()
	{
		Debug.Log("Congratulations! You found the secret button!");
	}

	private void OnValidate()
	{
		selectedLetters = "";
		foreach(var field in GetType().GetFields())
		{
			if((bool)field.GetValue(this))
			{
				selectedLetters += char.ToUpper(field.Name[0]);
			}
		}
	}
}

Example 2 Result