Unity3d – Attributes

- Attributes are one type of markers in Unity.

- Attributes can be placed above a Class, Property or Function in a script, that specially indicate for any special behavior on it.

- We can decorate our Classes, Properties or Metadata using attributes.

- C# contains attribute names within square brackets.

1) Header:
- Header Attribute useful to add Header like a Title above some fields in the Inspector.

[Header("Player Data")]
public string playerName;
public int playerHealth;

[Header("Enemy Data")]
public string enemyHealth;
public int enemyPower;

2) Hide InInspector:
- This attribute makes a variable not show up in the Inspector but be serialized.

public string playerName;
public int playerHealth;

3) Range:
- This attribute used to make a float or int variable in a script be restricted to a specific range.
- When this attribute is used, the float or int will be shown as a slider in the Inspector instead of the default number field.
- Parameters: Range(Min Value, Max Value)

[Range(0, 100)]
public int playerHealth;

[Range(20, 50)]
public float enemyPower;

4) Space:
- This attribute make a space between two sections.

public string playerName;
public int playerHealth;
public Transform targetEnemy;
public float attackSpeed;
public string enemyHealth;
public int enemyPower;

5) Multiline/TextArea:
- This attribute helps to make multiple lines for a string in Inspector.
- TextArea attribute is used to display multiple lines in Inspector for a string. Displaying is only change in Inspector.
- TextArea renders a flexible textbox with a scrollbar.

public string objectName;
public string objectMultiline;
public string objectTextArea;

6) Tooltip:
- Display tooltip when hovering of that field in Inspector window.

[Range(0, 100)]
[Tooltip("Player Health value between 0 and 100")]
public int playerHealth;

7) ContextMenu:
- The ContextMenu attribute allows you to add commands to the context menu.
- In the inspector of the attached script. When the user selects the context menu, the function will be executed.
- This is most useful for automatically setting up Scene data from the script. The function has to be non-static.

public class AttributesExample : MonoBehaviour
    private void TestContextMenu()
        Debug.Log("Hey! I am Debug for ContextMenu Testing.");

8) UnityEditor:
- All UnityEditor components can used using this attribute.

[UnityEditor.MenuItem("MyShortcuts/Clear AllPrefrences")]
public static void ClearAllPrefrences()
    Debug.Log("Clear All Prefrences");

9) HelpURL:
- This attribute provide custom documentation URL for a class.

public class MyComponentClass

10) DisallowMultipleComponent:
- Prevents MonoBehaviour of same type (or subtype) to be added more than once to a GameObject.

public class MyComponentClass

11) ExecuteInEditMode:
- Makes all instances of a script execute in Edit Mode.
- By default, MonoBehaviours are only executed in Play Mode. By adding this attribute, any instance of the MonoBehaviour will have its callback functions executed while the Editor is in Edit Mode too.

public class MyClass : MonoBehaviour
    void Awake()
        Debug.Log("Editor causes this Awake");

12) SerializedField:
- Force Unity to serialize a private field.
- When Unity serializes your scripts, it will only serialize public fields. If in addition to that you also want Unity to serialize one of your private fields you can add the SerializeField attribute to the field.
- The serialization system used can do the following:
  • CAN serialize public nonstatic fields (of serializable types)
  • CAN serialize nonpublic nonstatic fields marked with the SerializeField attribute.
  • CANNOT serialize static fields.
  • CANNOT serialize properties.
- All classes inheriting from UnityEngine.Object, for example GameObject, Component, MonoBehaviour, Texture2D, AnimationClip.
  • All basic data types like int, string, float, bool.
  • Some built-in types like Vector2, Vector3, Vector4, Quaternion, Matrix4x4, Color, Rect, LayerMask.
  • Arrays of a serializable type
  • List of a serializable type
  • Enums
  • Structs
Note: if you put one element in a list (or array) twice when the list gets serialized, you'll get two copies of that element, instead of one copy being in the new list twice.

public class AttributeExample : MonoBehaviour
    //This field gets serialized because it is public.
    public string playerName = "SudhirKotila";

    //This field does not get serialized because it is private.
    private int age = 28;

    //This field gets serialized even though it is private
    //because it has the SerializeField attribute applied.
    private bool hasHealthPotion = true;

    void Start()
        if (hasHealthPotion)
            Debug.Log("Person's name: " + playerName + " Person's age: " + age);


Post a Comment

Popular posts from this blog

GDLC [Game Development Life Cycle]

Data Encryption - Decryption [Unity3d - C#]


Unity Tiny | Tiny Mode | Instant Games | Playable Ads


DOTS - Unity3d


Unity's Back-end System