Unity Attributes

Some attributes that are pretty often useful.

ColorUsage Attribute

Attribute used to configure the usage of the ColorField and Color Picker for a color.

  1. showAlpha defines whether the alpha color part is shown.
  2. hdr defines that the color is used as a HDR color.

There are more attributes: ColorUsageAttribute Constructor

public class ColorFieldTesting : MonoBehaviour
{
    // No alpha, but HDR.
    [ColorUsage(false, true)]
    public Color myRedHue;
}

ContextMenu

The ContextMenu attribute allows you to add commands to the context menu in the inspector of the attached script.

public class ContextTesting : MonoBehaviour
{
    /// Add a context menu named "Do Something" in the inspector
    /// of the attached script.
    [ContextMenu("Do Something")]
    void DoSomething()
    {
        Debug.Log("Perform operation");
    }
}

Delayed

Attribute used to make a float, int, or string variable in a script not return a new value until the user has pressed enter or focus is moved away from the field.

Use this PropertyAttribute to add a header above some fields in the Inspector.

using UnityEngine;

public class Example : MonoBehaviour
{
    [Header("Health Settings")]
    public int health = 0;
    public int maxHealth = 100;

    [Header("Shield Settings")]
    public int shield = 0;
    public int maxShield = 0;
}

HelpURL

Provide a custom documentation URL for a class.

using UnityEngine;
using UnityEditor;

[HelpURL("http://manuals.clawjelly.net/Unity/UnityAttributes/")]
public class MyComponent
{
}

HideInInspector

Makes a variable not show up in the inspector but be serialized.

using UnityEngine;

public class Example : MonoBehaviour
{
    // Make the variable p not show up in the inspector but be serialized.
    [HideInInspector]
    int p = 5;
}

InspectorName

Use this attribute on enum value declarations to change the display name shown in the Inspector.

using UnityEngine;

public enum ModelImporterIndexFormat
{
    Auto = 0,
    [InspectorName("16 bits")]
    UInt16 = 1,
    [InspectorName("32 bits")]
    UInt32 = 2,
}

Use this to place a custom command in the main menu of the Unity Editor.

[MenuItem("MyMenu/MyCommand")]
static void MyCommand()
{
    Debug.Log("This is my command!");
}

Range

Attribute used to make a float or int variable in a script be restricted to a specific range.

using UnityEngine;

public enum ModelImporterIndexFormat
{
    [Range(0.0f, 100.0f)]
    int Size = 1,
}

RequireComponent

The RequireComponent attribute automatically adds required components as dependencies.

using UnityEngine;

// PlayerScript requires the GameObject to have a Rigidbody component
[RequireComponent(typeof(Rigidbody))]
public class PlayerScript : MonoBehaviour
{
    Rigidbody rb;

    void Start()
    {
        rb = GetComponent<Rigidbody>();
    }

    void FixedUpdate()
    {
        rb.AddForce(Vector3.up);
    }
}

Space

Use this PropertyAttribute to add some spacing in the Inspector.

using UnityEngine;

public class Example : MonoBehaviour
{
    int health = 0;
    int maxHealth = 100;

    [Space(10)] // 10 pixels of spacing here.

    int shield = 0;
    int maxShield = 0;
}

SerializeField

Makes private variables editable in the Editor

using UnityEngine;

public class Example : MonoBehaviour
{
    private int health = 0;
    // next one is private, but can be edited.
    [SerializeField]
    private int maxHealth = 100;
}

System.Serializeable

Makes a whole class editable in the Editor

using UnityEngine;

[System.Serializable]
public class PlayerStats
{
    public float health=100f;
    public int level=4;
    public string "Johnny";
}

public class MyHero: MonoBehaviour
{
    public PlayerStats playerStats;
}

Tooltip

tooltip

In the script a tooltip s added.

using UnityEngine;

public class Example : MonoBehaviour
{
    [Tooltip("Health value between 0 and 100.")]
    int health = 0;
}