Useful Attributes

ConditionalHide

This attribute allows to make your class field visible or invisible in inspector depending on value of another field.

How to use:

Add ConditionalHide attribute in the next format:

[ConditionalHide("conditionFieldName", hideInInspector, neededFieldValue)]

The first parameter (conditionFieldName) is the name of class field that will be checked.

If second parameter is true, your conditional field will be hidden from inspector in case if checked field value equals to neededFieldValue. Otherwise, your field will be visible, but not editable in inspector.

Example #1:

public class MyClass : MonoBehaviour
{
    [SerializeField] private bool firstField = false;
    [ConditionalHide("firstField", true, true)]
    [SerializeField] private int secondField;
}

In this case, secondField will be visible in inspector only if firstField value will be true.

First field is false
First field is true

Example #2

Let's change the second parameter in ConditionalHide attribute and see what happens.

public class MyClass : MonoBehaviour
{
    [SerializeField] private bool firstField = false;
    //This time we just disable editing for secondField field
    [ConditionalHide("firstField", false, true)]
    [SerializeField] private int secondField;
}

As result, we can always see secondField in inspector, but it can be editable or not:

Second Field is locked for editing
Second Field is unlocked for editing

ReadonlyField

This attribute makes class field not editable, but visible in inspector.

How to use:

Add [ReadonlyField] attribute to your class field. Good job, that's all!

Example:

public class MyClass : MonoBehaviour
{
    //id field will be visible in inspector, but not editable
    [SerializeField] [ReadonlyField] private string id = "0123";
    [SerializeField] private int someNumber;
}
[ReadonlyField] attribute blocks field editing in inspector

Last updated