Unity-Built-In-Attributes 内置属性

Unity-Built-In-Attributes

https://github.com/teebarjunk/Unity-Built-In-Attributes/blob/master/README.md#unity-built-in-attributes
A list of built in Unity Attributes.

Note: Attributes can be placed in a single set of square brackets:

[HideInInspector][SerializeField] int score;
// can be
[HideInInspector, SerializeField] int score;

These aren't all the attributes available, and a few of them are system attributes, not Unity ones.

Added July 3, 2018:

  • DidReloadScripts
  • PostProcessScene
  • PostProcessBuild
  • Preserve
  • RejectDragAndDropMaterial
  • CustomGridBrush
  • IconName

Property Inspector

HideInInspector: Stops the property from showing up in the inspector.

[HideInInspector] public bool reset = false;

Range: Limit the range of a float or int.

[Range(0, 100)] public float speed = 2f;

Multiline: Show more than one lines.

[Multiline(4)] public string description = "";

TextArea: Draw a flexible scrollable text area.

[TextArea] public string description = "";

ColorUsage: Allow alpha channel to be modified, and allow HDR mode.

[ColorUsage(true, true)] public Color color = Color.white;

Space: Add space between inspector elements.

public float item1 = 0f;
[Space(10)]
public float item2 = 0f;

Header: Shows a bold label in the inspector.

[Header("Stats")]
public int health = 100;
public float speed = 0f;
[Header("Items")]
public int ammo = 10;

ToolTip: Text shown on mouse over.

[ToolTip("The games score.")] public int score = 0;

Component Related

DisallowMultipleComponent: Prevent more than 1 of this component being on a GameObject.

[DisallowMultipleComponent]
public class MyScript : MonoBehaviour
{
}

RequireComponent: Tell GameObject to add this component if it isn't already added.

[RequireComponent(typeof(RigidBody))]
[RequireComponent(typeof(Component1), typeof(Component2), typeof(Component3))]  // You can enter multiple components into attribute.
public class MyClass : MonoBehaviour
{
}

ExecuteInEditMode: Will call MonoBehaviour methods like Update and OnEnable while in EditMode.

[ExecuteInEditMode]
public class MyClass : MonoBehaviour
{
}

ContextMenu: Add a context menu to a MonoBehaviour or ScriptableObject.

[ContextMenu("Reset Score")]
public void ResetHealth()
{
    health = 100;
}

SelectionBase: Will select this GameObject when a sub object is selected in the editor.

[SelectionBase]
public class MyClass : MonoBehaviour
{
}

Serialization

SerializeField: Force Unity to serialize a private field.

[SerializeField] private int score;

NonSerialized: Prevent Unity from serializing a public field.

[NonSerialized] public int score;

FormerlySerializedAs: If you changed the name of a serialized property, you can set this to the old name, so save data will still work.

[FormerlySerializedAs("myValue")] private string m_MyValue;

Serializable: Make a class Serializable so it will be visible in the inspector.

[System.Serializable]
public class MyClass
{
    public int myInt = 10;
    public Color myColor = Color.white;
}

Other

RuntimeInitializeOnLoadMethod: Calls a method once before or after the first scene has loaded. Good for initializing Singletons without having to place objects in the scene.

[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
static void OnLoad()
{
    Debug.Log("Create Singletons");
}

CreateAssetMenu: Add an option to Assets/Create for creating a ScriptableObject.

[CreateAssetMenu(menuName = "My ScriptableObject", order = 100)]
public class MyScriptableObject : ScriptableObject
{
}

Preserve: Preserves a members name when converting to bytecode. Useful if referencing members through reflection.

[Preserve]
static void Boink()
{
    Debug.Log("Boink");
}

CustomGridBrush: "define the class as a grid brush and to make it available in the palette window."

[CustomGridBrush(true, true, true, "Default Brush")]
public class MyBrush : GridBrush
{
}

Undocumented

DefaultExecutionOrder: Probably sets the Script Execution order.

[DefaultExecutionOrder(100)]
public class MyScript : MonoBehaviour
{
}

RejectDragAndDropMaterial: Probably prevents materials being applied through dragging and dropping in the editor.

[RejectDragAndDropMaterial]
public class MyRenderer : MonoBehaviour
{
}

UnityEditor.IconName: Probably allows you to set a scripts icon?

Editor

These should be used in scripts that are inside an Editor folder.

MenuItem: Adds a menu to the Editor toolbar.

[MenuItem("MyMenu/Do Something")]
static void DoSomething()
{
    Debug.Log("Doing Something...");
}

InitializeOnLoadMethod: Called after scripts have been compiled.

[InitializeOnLoadMethod]
static void OnProjectLoadedInEditor()
{
    Debug.Log("Project loaded in Unity Editor");
}

DidReloadScripts: Called after scripts have reloaded. Can take an order parameter. Methods with lower orders are called earlier.

using UnityEditor.Callbacks;

[DidReloadScripts(100)]
static void OnScriptsReloaded()
{
    Debug.Log("Reloaded.");
}
#endif

OnOpenAsset: Called when double clicking an asset in the project browser.

using UnityEditor.Callbacks;

[OnOpenAssetAttribute(1)]
static bool step1(int instanceID, int line)
{
    string name = EditorUtility.InstanceIDToObject(instanceID).name;
    Debug.Log("Open Asset step: 1 (" + name + ")");
    return false; // we did not handle the open
}

PostProcessBuild: Called after game has been built.

using UnityEditor.Callbacks;

[PostProcessBuildAttribute(1)]
public static void OnPostprocessBuild(BuildTarget target, string pathToBuiltProject)
{
}

PostProcessScene: Called after scene has been built.

using UnityEditor.Callbacks;

[PostProcessSceneAttribute (2)]
public static void OnPostprocessScene()
{
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 215,923评论 6 498
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,154评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 161,775评论 0 351
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,960评论 1 290
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,976评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,972评论 1 295
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,893评论 3 416
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,709评论 0 271
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,159评论 1 308
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,400评论 2 331
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,552评论 1 346
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,265评论 5 341
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,876评论 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,528评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,701评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,552评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,451评论 2 352

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,322评论 0 10
  • 今天是2018年 7月 19号。因为季后赛的结束,所以很多球星也要来中国行,像汤普森,哈登,韦德,杜兰特等等。韦德...
    Azis阅读 230评论 0 0
  • 我是机器人 人类喜欢别致神秘, 坚硬的钢铁重新设计, 模仿人的功能, 创造了人工的奇迹。 机器人是人类的兄弟, 新...
    边骥秋生阅读 198评论 2 4
  • 数据库增删改查CURD操作 数据库操作之-查询构造器 查询构造器简介及新增数据 使用查询构造器修改数据 使用查询构...
    威研威语阅读 1,318评论 0 2
  • 我记得以前有人问我在大学里到底该不该谈恋爱,还问我谈恋爱的意义在哪里。以前我的回答是,遇见一个更好的人,有人相伴,...
    Ms念文阅读 766评论 1 1