Attributes in Unity 3D

What is an Attribute:
Attributes provide a powerful method of associating metadata, or declarative information, with code (assemblies, types, methods, properties, and so forth). -Taken from .NET.

The Script:
I have compiled some example code to help you learn how to use C# Attributes in Unity. Below is an example of what I believe are the most useful attributes. Everything is fully commented and is ready to add to a GameObject in your Unity game.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

//[RequireComponent(typeof(OtherClass))]      // This makes is so this component requires another component. (unity will auto add it for you)
[DisallowMultipleComponent]                 // This makes it so you cannot add the same component twice.
[HelpURL("https://docs.unity3d.com/2018.2/Documentation/ScriptReference/AddComponentMenu.html")]// This will link out to a help doc through unity when clicking on the ? book in the inspector
public class LearnAttributesScript : MonoBehaviour
{
    [Header("Stats")]                       // This will be bold.
    public int health;
    public int armor;
    [Header("Character Info")]
    public string playerName;
    [Tooltip("This crates a paragraph like area of text")]  // This creates a mouse over tip. When you mouse over the text 'description' in the editor.
    [TextArea]
    public string description;
    [Space]                                 // This creates a space inbetween this and the lower item. 
    public int level;
    [Tooltip("This keeps something with in two numbers")]
    [Range(1, 100)]                         // This keeps something with in two numbers
    public int powerlvl;
    [HideInInspector]                       // This hides data or a class that needs to be pulbic but only altered through code, not the inspector. 
    public bool isPaused;

    /////////////////////////
    [Tooltip("Right click on this to see menu options")]
    [ContextMenuItem("One", "SelectOne")]   // This creates a menu system.
    [ContextMenuItem("Two", "SelectTwo")]
    [ContextMenuItem("Three", "SelectThree")]
    [Multiline(4)]                          // This creates an indented multiline string.
    public string a_number = "";
    void SelectOne()
    {
        a_number = "You selected 1!";
    }
    void SelectTwo()
    {
        a_number = "You selected 2!";
    }
    void SelectThree()
    {
        a_number = "You selected 3!";
    }
    /////////////////////////
    [ContextMenu("Do Something")]               // Creates selectable code to be accessed in the gear icon.
    void DoSomething()
    {
        Debug.Log("Perform operation");
    }
    /////////////////////////
    [UnityEditor.MenuItem("Tools/Do a thing")]  // Creates a menu with a selectable item that will run code.
    public static void DoAThing()
    {
        Debug.Log("a thing");
    }
}

Other Info on Attributes:
Another helpful Attribute is [Serializable] and [SerializeField]. These are a bit more complicated then a one sentence comment will allow but here and here is some basic info on them.

Here are the Unity Docs on Attributes which are somewhat useful.
Here is a really helpful video.

How do you use Attributes? Thank you for reading,
James

Comments

  1. This comment has been removed by the author.

    ReplyDelete
  2. I am very thankful to you for sharing this best knowledge with us. This information is helpful for everyone. So please always share this kind of knowledge with everyone. Thanks. Read more info about unity development company

    ReplyDelete

Post a Comment

Popular posts from this blog

How to create slowly revealed text in a Unity 3d game

Fade in and fade out Script for Unity 3d

How to do chain damage in a Unity 3D game using C#