Unity characteristics

Menu Menu

AddComponentMenuConstructor

Script to add settings from under the Component menu (path can be set))

AddComponentMenu - AddComponentMenu - Unity Script API

using UnityEngine;

[AddComponentMenu("Test")]
public class Test : MonoBehaviour
{
    
    
}

insert image description here


CreateAssetMenuAttribute/CreateAssetMenu

Implement a path in Assets>Create, create ScriptableObject files and store them in the project (need to inherit from ScriptableObject)
UnityEngine.CreateAssetMenuAttribute - Unity Scripting API

using UnityEngine;

[CreateAssetMenu(fileName="Test",menuName = "Test",order=1)]
public class Test : ScriptableObject
{
    
    
    public string playerBiography = "";
}

insert image description here

inspection panel

Context Menu

On the Inspector panel, execute the method function bound to this feature. (this function must be non-static)

UnityEngine.ContextMenu - Unity Screenplay API

using UnityEngine;

public class Test : MonoBehaviour
{
    
    
    [ContextMenu("Do Something")]
    void DoSomething()
    {
    
    
        Debug.Log("Perform operation");
    }
}

insert image description here


HeaderAttribute

Give script variables a title in the Inspector panel

UnityEngine.HeaderAttribute - Unity Screenplay API

using UnityEngine;

public class Test : MonoBehaviour
{
    
    
    [Header("Health Settings")]
    public int health = 0;
}

insert image description here


HideInInspector

Causes the variable not to show up in the Inspector, but to be serialized.

UnityEngine.HideInInspector - Unity Screenplay API

using UnityEngine;

public class Test : MonoBehaviour
{
    
    
    [HideInInspector]
    public int health = 0;
}

InspectorNameAttribute

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

UnityEngine.InspectorNameAttribute - Unity Screenplay API

using UnityEngine;

public class Test : MonoBehaviour
{
    
    
    public Mode Modes;
}

public enum Mode
{
    
    
    Auto = 0,
    [InspectorName("竖排")]
    SP = 1,
    [InspectorName("横排")]
    HP = 2,
}

insert image description here


MinAttribute

Attribute to constrain float or int variables in scripts to a certain minimum value.

UnityEngine.MinAttribute - Unity Screenplay API

using UnityEngine;

public class Test : MonoBehaviour
{
    
    
    [Min(0)]
    public float minFloat;
}


MultilineAttribute

Multiple text display in editor

UnityEngine.MultilineAttribute - Unity Screenplay API

using UnityEngine;

public class Test : MonoBehaviour
{
    
    
    [Multiline(10)]//默认为3行
    public string minFloat;
}

insert image description here


NonReorderableAttribute

Arrays or lists with this property cannot be rearranged in the editor

UnityEngine.NonReorderableAttribute - Unity Screenplay API

using UnityEngine;

public class Test : MonoBehaviour
{
    
    
    [NonReorderableAttribute]
    public int[] array;
}

insert image description here


RangeAttribute

Attribute to restrict float or int variables in scripts to a specific range.

When using this property, float or int will be displayed as a slider in the Inspector instead of the default number field.

UnityEngine.RangeAttribute - Unity Screenplay API

using UnityEngine;

public class Test : MonoBehaviour
{
    
    
    [Range(1, 6)]
    public int integerRange;
}

insert image description here


RequireComponent

The RequireComponent attribute automatically adds the required component as a dependency. (When binding the script, the set dependencies will be added automatically, and the dependencies cannot be deleted)

UnityEngine.RequireComponent - Unity Screenplay API

using UnityEngine;

[RequireComponent(typeof(Rigidbody))]
public class Test : MonoBehaviour
{
    
    
}

insert image description here


SpaceAttribute

Add spacing in the Inspector

UnityEngine.SpaceAttribute - Unity Screenplay API

using UnityEngine;

public class Test : MonoBehaviour
{
    
    
    public int maxHealth = 100;
    
    [Space(10)]//中间十个间距
    
    public int shield = 0;
}

insert image description here


TextAreaAttribute

A minimum and maximum number of lines can be specified for the TextArea, and the field will expand according to the size of the text. If the text is larger than the available area, scroll bars are displayed.

UnityEngine.TextAreaAttribute - Unity Screenplay API

using UnityEngine;

public class Test : MonoBehaviour
{
    
    
    [TextArea(0,10)]
    public string MyTextArea;
}

insert image description here


TooltipAttribute

Specifies tooltips for fields in the Inspector window.

UnityEngine.TooltipAttribute - Unity Screenplay API

using UnityEngine;

public class Test : MonoBehaviour
{
    
    
    [Tooltip("提示")]
    int health = 0;
}

insert image description here

variable

ColorUsageAttribute/ColorUsage

Assign attributes to the color (1. You can set whether to display the Alpha channel, 2. You can set whether it is an HDR color)

UnityEngine.ColorUsageAttribute - Unity Screenplay API

using UnityEngine;

public class Test : MonoBehaviour
{
    
    
    [ColorUsage(true,true)]//参数1:alpha开关。参数2:hdr开关。
    public Color testColor;
}

insert image description here


ContextMenuItemAttribute/ContextMenuItem

Right click on the current variable to set an event and call the method.

UnityEngine.ContextMenuItemAttribute - Unity 脚本 API

using UnityEngine;

public class Test : MonoBehaviour
{
    
    
    [Multiline(8)]    
    [ContextMenuItem("Do Something", "ResetBiography")]
    public string playerBiography = "";

    void ResetBiography()
    {
    
    
        playerBiography = "Set";
    }
}

insert image description here


DelayedAttribute

Attribute to cause float, int, or string variables in scripts to be deferred.

When using this property, the field does not return a new value until the user presses the Enter key or moves focus away from the float, int, or text field.

UnityEngine.DelayedAttribute - Unity Screenplay API

using UnityEngine;

public class Test : MonoBehaviour
{
    
    
    [Delayed]
    public string playerBiography = "";

}

screenplay

DisallowMultipleComponent

Prevents a MonoBehaviour of the same type (or subtype) from being added to a GameObject multiple times.

UnityEngine.DisallowMultipleComponent - Unity Screenplay API

Hanging on more than one script will report an error
insert image description here


ExecuteAlways

Add this property in play mode, edit mode, prefab mode, any instance of MonoBehaviour will always execute its callback function.

UnityEngine.ExecuteAlways - Unity Screenplay API

using UnityEngine;

[ExecuteAlways]
public class Test : MonoBehaviour
{
    
    
    void Start()
    {
    
    
        if (Application.IsPlaying(gameObject))
        {
    
    
            // Play logic
        }
        else
        {
    
    
            // Editor logic
        }
    }
}

ExecuteInEditMode

Execute callback function in editor mode (recommended to use ExecuteAlways)


HelpURLAttribute

Add a custom document for this script

UnityEngine.HelpURLAttribute - Unity 脚本 API

using UnityEngine;

[HelpURL("http://www.baidu.com")]
public class Test : MonoBehaviour
{
    
    
}

insert image description here


PropertyAttribute

Base class for deriving custom property attributes. This can be used to create properties for script variables.

A custom property can be connected with a custom PropertyDrawer class to control how script variables with that property are displayed in the Inspector.

UnityEngine.PropertyAttribute - Unity Screenplay API


RuntimeInitializeOnLoadMethodAttribute

Allows a runtime class method to be initialized without user action when loading the game at runtime. (you can not mount it)

When the game loads, the method [RuntimeInitializeOnLoadMethod]marked . This is done after calling Awakethe method .

UnityEngine.RuntimeInitializeOnLoadMethodAttribute - Unity 脚本 API

class MyClass
{
    
    
    [RuntimeInitializeOnLoadMethod]
    static void OnRuntimeMethodLoad()
    {
    
    
        Debug.Log("After Scene is loaded and game is running");
    }

    [RuntimeInitializeOnLoadMethod]
    static void OnSecondRuntimeMethodLoad()
    {
    
    
        Debug.Log("SecondMethod After Scene is loaded and game is running.");
    }
}

SerializeField

Forces Unity to serialize private fields.

When Unity serializes scripts, only public fields are serialized. If you also want Unity to serialize private fields, you can add the SerializeField property to those fields.

UnityEngine.SerializeField - Unity Screenplay API

using UnityEngine;

public class Test : MonoBehaviour
{
    
    
    [SerializeField]
    private string test;
}

SerializeReference

serialized reference type

UnityEngine.SerializeReference - Unity Screenplay API

special

SelectionBaseAttribute

In the Scene window, when the script bound to this feature is on the bound GameObject, when the child object of the object is clicked for the first time, the object bound to the script will be selected.

UnityEngine.SelectionBaseAttribute - Unity Screenplay API

using UnityEngine;

[SelectionBaseAttribute]
public class Test : MonoBehaviour
{
    
    
}

unbound scriptinsert image description here

binding scriptinsert image description here

Not summarized

AssemblyIsEditorAssembly

BeforeRenderOrderAttribute

CustomGridBrushAttribute

ExcludeFromObjectFactoryAttribute

ExcludeFromPresetAttribute

GradientUsageAttribute

GUITargetAttribute

IconAttribute

ImageEffectAfterScale

ImageEffectAllowedInSceneView

ImageEffectOpaque

ImageEffectTransformsToLDR

ImageEffectUsesCommandBuffer

PreferBinarySerialization

SharedBetweenAnimatorsAttribute

UnityAPICompatibilityVersionAttribute

Guess you like

Origin blog.csdn.net/SodasZ/article/details/128386375