Unity Attributes 自帶特性

AddComponentMenu

AddComponentMenu屬性允許您將腳本放置在“組件”菜單中的任何位置,而不僅僅是“組件 - >腳本”菜單。
您可以使用它來更好地組織“組件”菜單,這樣可以在添加腳本時改進工作流程。重要提示:您需要重新啟動。

  • componentOrder 組件菜單中的組件順序(低于頂部)。
  • AddComponentMenu 在“組件”菜單中添加一個項目。
using UnityEngine;

[AddComponentMenu("Transform/Follow Transform")]
public class FollowTransform : MonoBehaviour
{
}

效果如下

AssemblyIsEditorAssembly

裝配級屬性。具有此屬性的程序集中的任何類將被視為編輯器類。
構造函數(shù)
AssemblyIsEditorAssembly

ContextMenu

ContextMenu屬性允許您向上下文菜單添加命令。在所附腳本的督察中。當用戶選擇上下文菜單時,將執(zhí)行該功能。這對于自動從腳本設置場景數(shù)據(jù)是最有用的。該功能必須是非靜態(tài)的。

using UnityEngine;

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");
    }
}
  • ContextMenu 將功能添加到組件的上下文菜單。

效果如下

ContextMenuItemAttribute

使用此屬性將上下文菜單添加到調(diào)用命名方法的字段。

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour
{
    [ContextMenuItem("Reset", "ResetBiography")]
    [Multiline(8)]
    public string playerBiography = "";
    void ResetBiography()
    {
        playerBiography = "";
    }
}

效果如下

CreateAssetMenu

fileName
新創(chuàng)建的此類實例使用的默認文件名。(創(chuàng)建文件必須以 .asset 結尾)
menuName
此類型顯示的名稱顯示在“資產(chǎn)/創(chuàng)建”菜單中。
order
菜單項在資產(chǎn)/創(chuàng)建菜單中的位置。

using UnityEngine;

[CreateAssetMenu(fileName = "自定義資源.asset", menuName = "菜單/子項0")]
public class CreateAsset : ScriptableObject
{
    public string Name = "自定義資源";

    public Vector3[] Pos = new Vector3[10];
}

效果如下

DelayedAttribute

用于在腳本中使float,int或string變量的屬性被延遲。

當使用此屬性時,float,int或text字段將不會返回一個新的值,直到用戶按下enter或焦點離開該字段。

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

public class DelayedAttributeExample : MonoBehaviour
{
    [DelayedAttribute()]
    public string content;

}

效果如下

DisallowMultipleComponent

防止將相同類型(或子類型)的MonoBehaviour多次添加到GameObject。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[DisallowMultipleComponent]
public class Disallow : MonoBehaviour
{

}

效果如下

ExecuteInEditMode

使腳本的所有實例在編輯模式下執(zhí)行。

默認情況下,MonoBehaviours只能在播放模式下執(zhí)行。
通過添加此屬性,MonoBehaviour的任何實例將在編輯器不處于播放模式時執(zhí)行其回調(diào)函數(shù)。

這些功能不會像播放模式一樣被調(diào)用。

更新僅在場景中的某些內(nèi)容更改時才調(diào)用。當游戲視圖接收到一個事件時,OnGUI被調(diào)用。OnRenderObject和其他渲染回調(diào)函數(shù)在場景視圖或游戲視圖的每次重繪時都被調(diào)用。
另請參見:runInEditMode

using UnityEngine;

[ExecuteInEditMode]
public class PrintAwake : MonoBehaviour
{
    void Awake()
    {
        Debug.Log("Editor causes this Awake");
    }

    void Update()
    {
        Debug.Log("Editor causes this Update");
    }
}

效果如下

GUITargetAttribute

控制對應的OnGUI在那個Display上顯示

using UnityEngine;
public class ExampleClass1 : MonoBehaviour
{
    // Label will appear on display 0 and 1 only
    [GUITarget(0, 1)]
    void OnGUI()
    {
        GUI.Label(new Rect(10, 10, 300, 100), "Visible on TV and Wii U GamePad only");
    }
}

效果如下

HeaderAttribute

在Inspector 中顯示屬性對應屬性的注釋
使用此
PropertyAttribute在檢查器中的某些字段上方添加標題。
標題是使用DecoratorDrawer完成的。

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour
{
    [Header("生命 Settings")]
    public int health = 0;
    public int maxHealth = 100;
    [Header("防御 Settings")]
    public int shield = 0;
    public int maxShield = 0;
}

效果如下

HelpURLAttribute

幫助圖標對應跳轉(zhuǎn)的URL(注意此類需要繼承MonoBehaviour,不繼承在2017測試跳轉(zhuǎn)地址無效)

using UnityEngine;
using UnityEditor;

[HelpURL("http://www.itdecent.cn/u/84e03bc5c4a6")]
public class MyComponent:MonoBehaviour
{
}

效果如下

HideInInspector

隱藏在Inspector面板中顯示的Public屬性

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour
{
    [HideInInspector]
    public int Hide = 0;

    [Header("生命 Settings")]
    public int health = 0;
    public int maxHealth = 100;
    [Header("防御 Settings")]
    public int shield = 0;
    public int maxShield = 0;
}

效果如下

ImageEffectAllowedInSceneView

具有此屬性的任何圖像效果可以渲染到場景視圖相機中。
如果您希望將圖像效果應用于場景視圖相機,則會添加此屬性。效果將應用于相同的位置,并且具有與相機效果相同的值。(5.4開始新加的特性,目前不知道怎么用)

MultilineAttribute

用于使字符串值的屬性顯示在多行文本區(qū)域中。

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

public class Example1 : MonoBehaviour
{
    [MultilineAttribute(10)]
    public string str = "";

}

效果如下

PreferBinarySerialization

這對于包含大量數(shù)據(jù)的自定義資產(chǎn)類型很有用。始終保持存儲為二進制可以提高讀/寫性能,并在磁盤上生成更緊湊的表示。(說白了就是一二進制形式保存提高性能)

using UnityEngine;


[CreateAssetMenu]
[PreferBinarySerialization]
public class CustomData : ScriptableObject
{
    public float[] lotsOfFloatData = new[] { 1f, 2f, 3f };
    public byte[] lotsOfByteData = new byte[] { 4, 5, 6 };
}

PropertyAttribute

派生自定義屬性屬性的基類。
使用它來創(chuàng)建腳本變量的自定義屬性。

自定義屬性可以與自定義的
[PropertyDrawer(https://docs.unity3d.com/2017.2/Documentation/ScriptReference/PropertyDrawer.html)類掛鉤,以控制如何在Inspector中顯示具有該屬性的腳本變量。
另請參見:PropertyDrawer類。

RangeAttribute

用于在腳本中創(chuàng)建一個float或int變量的屬性被限制在一個特定的范圍內(nèi)。
當使用此屬性時,float或int將在Inspector中顯示為滑塊,而不是默認數(shù)字字段

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

public class Example1 : MonoBehaviour
{
    [RangeAttribute(0,1000f)]
    public int speed;
}

效果如下

RequireComponent

RequireComponent屬性自動將所需組件添加為依賴關系。
當您將一個使用RequireComponent的腳本添加到GameObject時,所需的組件將自動添加到GameObject中。這有助于避免安裝錯誤。例如,腳本可能需要將Rigidbody總是添加到同一個GameObject中。使用RequireComponent這將自動完成,因此您永遠不會得到設置錯誤。請注意,RequireComponent僅在組件添加到GameObject的時刻檢查缺少的依賴關系。GameObject缺少新依賴關系的組件的現(xiàn)有實例將不會自動添加這些依賴關系。(自動添加指定組建,有組件的不添加,沒有的添加)

using UnityEngine;

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

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

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

效果如下

RuntimeInitializeOnLoadMethodAttribute

允許運行時類方法在運行時加載游戲時被初始化,而不需要用戶的操作。
標記[RuntimeInitializeOnLoadMethod]的方法在游戲加載后被調(diào)用。這是在Awake方法被調(diào)用之后。
注意:[RuntimeInitializeOnLoadMethod]不保證標記的方法的執(zhí)行順序。調(diào)用的方法需靜態(tài)

場景加載前調(diào)用

[RuntimeInitializeOnLoadMethodAttribute(RuntimeInitializeLoadType.BeforeSceneLoad)]

場景加載后調(diào)用

[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterSceneLoad)]
// Create a non-MonoBehaviour class which displays
// messages when a game is loaded.
using UnityEngine;

class MyClass
{
    [RuntimeInitializeOnLoadMethod]
    static void OnRuntimeMethodLoad()
    {
        Debug.Log("場景加載和游戲運行后");
    }

    [RuntimeInitializeOnLoadMethod]
    static void OnSecondRuntimeMethodLoad()
    {
        Debug.Log("SecondMethod場景加載和游戲運行后");
    }
}

效果如下

SelectionBaseAttribute

將此屬性添加到腳本類以將其GameObject標記為用于場景視圖挑選的選擇基礎對象。
在Unity Scene View中,單擊選擇對象時,Unity將嘗試找出最適合您選擇的對象。如果單擊作為預制體一部分的對象,則選擇預制根的根,因為預制根被視為選擇庫。您也可以使其他對象也被視為選擇庫。您需要使用SelectionBase屬性創(chuàng)建一個腳本類,然后您需要將該腳本添加到GameObject中。

using UnityEngine;

[SelectionBase]
public class PlayerScript : MonoBehaviour
{

}

效果如下

SerializeField

在Inspector可以看到標記為private的屬性

using UnityEngine;

public class SomePerson : MonoBehaviour
{
    //This field gets serialized because it is public.
    public string firstName = "John";

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

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

    void Start()
    {
        if (hasHealthPotion)
            Debug.Log("Person's first name: " + firstName + " Person's age: " + age);
    }
}

效果如下

SharedBetweenAnimatorsAttribute

SharedBetweenAnimatorsAttribute是一個屬性,指定該StateMachineBehaviour應僅實例化一次,并在所有Animator實例之間共享。
此屬性減少每個控制器實例的內(nèi)存占用。

程序員可以選擇哪個StateMachineBehaviour可以使用此屬性。
請注意,如果您的StateMachineBehaviour更改一些成員變量,它將影響使用它的所有其他Animator實例。
另請參見:StateMachineBehaviour類。

using UnityEngine;

[SharedBetweenAnimators]
public class AttackBehaviour : StateMachineBehaviour
{
    public override void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        Debug.Log("OnStateEnter");
    }
}

SpaceAttribute

使用此
PropertyAttribute在檢查器中添加一些間距。
間距使用DecoratorDrawer完成。

using UnityEngine;
using System.Collections;

public class ExampleClass2 : MonoBehaviour
{
    public int health = 0;
    public int maxHealth = 100;
  [Space(10)]
    public int shield = 0;
    public int maxShield = 0;
}

效果如下

TextAreaAttribute

使用高度靈活和可滾動的文本區(qū)域編輯字符串的屬性。
您可以指定TextArea的最小和最大行,并且字段將根據(jù)文本的大小進行擴展。如果文本大于可用區(qū)域,則會顯示一個滾動條。

using UnityEngine;

public class TextAreaExample : MonoBehaviour
{
    [TextArea()]
    public string MyTextArea;
}

效果如下

TooltipAttribute

在“檢查器”窗口中指定一個字段的工具提示。

using UnityEngine;
using System.Collections;

public class ExampleClass3 : MonoBehaviour
{
    [Tooltip("Health 值 從 0 到 100.")]
    public int health = 0;
}

效果如下

UnityAPICompatibilityVersionAttribute

聲明一個程序集與特定的Unity API兼容(API明智)。由內(nèi)部工具使用,以避免處理程序集,以確定程序集是否可能使用舊的Unity API。
(目前沒弄明白怎么用)

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容