EloiStree / HelloSharpForUnity3D

Here we are going to check all the basic need to work in Unity3D with C#
0 stars 0 forks source link

Topic : All Attributes in Unity3D #429

Open EloiStree opened 2 months ago

EloiStree commented 2 months ago

Board: https://github.com/users/EloiStree/projects/17/views/1


🤖

Unity Attributes and Examples

1. RuntimeInitialize

Documentation
Used to specify methods that should be called when the game starts.

[RuntimeInitializeOnLoadMethod]
static void OnRuntimeInitialize()
{
    Debug.Log("This method runs when the game starts.");
}

2. ContextMenuItem

Documentation
Allows you to add custom options to the right-click context menu in the Unity Editor.

public class ExampleContextMenu : MonoBehaviour
{
    [ContextMenu("Reset Position")]
    void ResetPosition()
    {
        transform.position = Vector3.zero;
    }
}

3. SelectionBaseAttribute

Documentation
Use the SelectionBaseAttribute to mark a GameObject as a selection base object, allowing it to be selected directly in the Scene View when clicking on it.

using UnityEngine;

[SelectionBase]
public class ExampleSelectionBase : MonoBehaviour
{
    // This GameObject will be treated as a selection base in the Scene View
}

4. MenuItem

Documentation
Creates a menu item in the Unity Editor, allowing quick access to custom functions.

public class ExampleMenuItem
{
    [MenuItem("Tools/Custom Menu Item")]
    static void CustomMenuFunction()
    {
        Debug.Log("Custom menu item clicked!");
    }
}

5. Serializable

Documentation
Marks a class as serializable, allowing its instances to be saved and loaded.

[System.Serializable]
public class ExampleSerializable
{
    public int id;
    public string name;
}

6. CreateAssetMenu

Documentation
Enables you to create new ScriptableObjects from the Unity Editor menu.

[CreateAssetMenu(fileName = "NewItem", menuName = "Inventory/Item")]
public class Item : ScriptableObject
{
    public string itemName;
    public int itemID;
}

7. RequireComponent

Documentation
Ensures that a GameObject has a specific component attached before this script can work.

[RequireComponent(typeof(Rigidbody))]
public class ExampleRequireComponent : MonoBehaviour
{
    private Rigidbody rb;

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

8. Header

Documentation
Adds a header label above a field in the Inspector for better organization.

public class ExampleHeader : MonoBehaviour
{
    [Header("Player Settings")]
    public float speed;
    public float jumpHeight;
}

9. ContextMenu

Documentation
Adds a method to the context menu for easy access from the Unity Inspector.

public class ExampleContextMenuUsage : MonoBehaviour
{
    [ContextMenu("Print Message")]
    void PrintMessage()
    {
        Debug.Log("This is a message from the context menu!");
    }
}

10. ToolTip

Documentation
Displays a tooltip with information about a field when hovering in the Inspector.

public class ExampleToolTip : MonoBehaviour
{
    [Tooltip("This is a player speed multiplier.")]
    public float speedMultiplier;
}

11. TextArea

Documentation
Creates a multi-line text area in the Inspector for easier text input.

public class ExampleTextArea : MonoBehaviour
{
    [TextArea(3, 10)]
    public string description;
}

12. Range

Documentation
Restricts a numeric field to a specified range in the Inspector.

public class ExampleRange : MonoBehaviour
{
    [Range(0, 100)]
    public int health;
}

13. InitializedOnLoad

Documentation
Executes a static constructor when the editor is loaded, useful for setup.

[InitializeOnLoad]
public static class ExampleInitializedOnLoad
{
    static ExampleInitializedOnLoad()
    {
        Debug.Log("This runs when the editor loads.");
    }
}

14. RuntimeInitializeddonLoad

Documentation
Specifies methods that should be called when the game is initialized at runtime.

[RuntimeInitializeOnLoadMethod]
static void OnRuntimeInitialized()
{
    Debug.Log("Called at runtime initialization.");
}

15. HelpUrl

Documentation
Provides a link to documentation related to the script for easier access.

[HelpURL("https://docs.unity3d.com/ScriptReference/HelpURLAttribute.html")]
public class ExampleHelpUrl : MonoBehaviour
{
    // This script has a help URL associated with it.
}

16. SerializedField

Documentation
Allows private fields to be serialized and visible in the Inspector.

public class ExampleSerializedField : MonoBehaviour
{
    [SerializeField]
    private int score;
}

17. Multiline

Documentation
Creates a multi-line text input for a string field in the Inspector.

public class ExampleMultiline : MonoBehaviour
{
    [Multiline]
    public string multiLineText;
}

18. Space

Documentation
Adds space between fields in the Inspector for better organization.

public class ExampleSpace : MonoBehaviour
{
    [Space(10)]
    public float floatValue1;

    [Space(20)]
    public float floatValue2;
}

19. CustomEditor

Documentation
Creates a custom Inspector for a specified class, allowing for personalized layouts and buttons.

[CustomEditor(typeof(ExampleCustomEditor))]
public class ExampleCustomEditor : Editor
{
    public override void OnInspectorGUI()
    {
        DrawDefaultInspector();
        if (GUILayout.Button("Custom Button"))
        {
            Debug.Log("Button pressed!");
        }
    }
}

20. ExecuteInEditorMode

Documentation
*Allows a script to run even when the game is not playing

, useful for editor tools.*

[ExecuteInEditMode]
public class ExampleExecuteInEditMode : MonoBehaviour
{
    void Update()
    {
        // This code runs in editor mode as well.
        Debug.Log("This is running in edit mode.");
    }
}