举两个例子,在变量上使用[SerializeFiled]属性,可以强制让变量进行序列化,可以在Unity的Editor上进行赋值。

在Class上使用[RequireComponent]属性,就会在Class的GameObject上自动追加所需的Component。

以下是Unity官网文档中找到的所有Attribute,下面将按照顺序,逐个对这些Attribute进行说明和小的测试。
部分例子使用了Unity官方的示例。

UnityEngine

AddComponentMenu

可以在UnityEditor的Component的Menu中增加自定义的项目。菜单可以设置多级,使用斜线/分隔即可。在Hierarchy中选中GameObject的时候,点击该菜单项,就可以在GameObject上追加该Component。
例如如下代码可以完成下图的效果。

  1. [AddComponentMenu("TestMenu/TestComponet")]
  2. public class TestMenu : MonoBehaviour {
  3. }

AssemblyIsEditorAssembly

汇编级属性,使用该属性的Class会被认为是EditorClass。具体用法不明。

ContextMenu

可以在Inspector的ContextMenu中增加选项。
例如,如下代码的效果

  1. public class TestMenu : MonoBehaviour {
  2. [ContextMenu ("Do Something")]
  3. void DoSomething () {
  4. Debug.Log ("Perform operation");
  5. }
  6. }

ContextMenuItemAttribute

这个属性是Unity4.5之后提供的新功能,可以在Inspector上面对变量追加一个右键菜单,并执行指定的函数。
例子:

  1. public class Sample : MonoBehaviour {
  2. [ContextMenuItem("Reset", "ResetName")]
  3. public string name = "Default";
  4. void ResetName() {
  5. name = "Default";
  6. }
  7. }

DisallowMultipleComponent

对一个MonoBehaviour的子类使用这个属性,那么在同一个GameObject上面,最多只能添加一个该Class的实例。
尝试添加多个的时候,会出现下面的提示。

ExecuteInEditMode

默认状态下,MonoBehavior中的Start,Update,OnGUI等方法,需要在Play的状态下才会被执行。
这个属性让Class在Editor模式(非Play模式)下也能执行。
但是与Play模式也有一些区别。
例如:
Update方法只在Scene编辑器中有物体产生变化时,才会被调用。
OnGUI方法只在GameView接收到事件时,才会被调用。

HeaderAttribute

这个属性可以在Inspector中变量的上面增加Header。
例子:

  1. public class ExampleClass : MonoBehaviour {
  2. [Header("生命值")]
  3. public int CurrentHP = 0;
  4. public int MaxHP = 100;
  5.  
  6. [Header("魔法值")]
  7. public int CurrentMP = 0;
  8. public int MaxMP = 0;
  9. }

HideInInspector

在变量上使用这个属性,可以让public的变量在Inspector上隐藏,也就是无法在Editor中进行编辑。

ImageEffectOpaque

在OnRenderImage上使用,可以让渲染顺序在非透明物体之后,透明物体之前。
例子

  1. [ImageEffectOpaque]
  2. void OnRenderImage (RenderTexture source, RenderTexture destination){
  3. }
ImageEffectTransformsToLDR

渲染从从HDR变为LDR 具体使用方法不明。

MultilineAttribute

在string类型上使用,可以在Editor上输入多行文字。

  1. public class TestString : MonoBehaviour {
  2. [MultilineAttribute]
  3. public string mText;
  4. }

NotConvertedAttribute

在变量上使用,可以指定该变量在build的时候,不要转换为目标平台的类型。

NotFlashValidatedAttribute

在变量上使用,在Flash平台build的时候,对该变量不进行类型检查。
Unity5.0中已经移除了这个属性。

NotRenamedAttribute

禁止对变量和方法进行重命名。
Unity5.0中已经移除了这个属性。

PropertyAttribute
RangeAttribute

在int或者float类型上使用,限制输入值的范围

  1. public class TestRange : MonoBehaviour
  2. {
  3. [Range(0, 100)] public int HP;
  4. }
RequireComponent

在Class上使用,添加对另一个Component的依赖。
当该Class被添加到一个GameObject上的时候,如果这个GameObject不含有依赖的Component,会自动添加该Component。
且该Componet不可被移除。

例子

  1. [RequireComponent(typeof(Rigidbody))]
  2. public class TestRequireComponet : MonoBehaviour {
  3.  
  4. }


如果尝试移除被依赖的Component,会有如下提示

RPC

在方法上添加该属性,可以网络通信中对该方法进行RPC调用。

  1. [RPC]
  2. void RemoteMethod(){
  3. }
RuntimeInitializeOnLoadMethodAttribute

此属性仅在Unity5上可用。
在游戏启动时,会自动调用添加了该属性的方法。

  1. class MyClass
  2. {
  3. [RuntimeInitializeOnLoadMethod]
  4. static void OnRuntimeMethodLoad ()
  5. {
  6. Debug.Log("Game loaded and is running");
  7. }
  8. }
SelectionBaseAttribute

当一个GameObject含有使用了该属性的Component的时候,在SceneView中选择该GameObject,Hierarchy上面会自动选中该GameObject的Parent。

SerializeField

在变量上使用该属性,可以强制该变量进行序列化。即可以在Editor上对变量的值进行编辑,即使变量是private的也可以。
在UI开发中经常可见到对private的组件进行强制序列化的用法。
例子

  1. public class TestSerializeField : MonoBehaviour {
  2. [SerializeField]
  3. private string name;
  4.  
  5. [SerializeField]
  6. private Button _button;
  7. }

SharedBetweenAnimatorsAttribute

用于StateMachineBehaviour上,不同的Animator将共享这一个StateMachineBehaviour的实例,可以减少内存占用。

SpaceAttribute

使用该属性可以在Inspector上增加一些空位。 例子:

  1. public class TestSpaceAttributeByLvmingbei : MonoBehaviour {
  2. public int nospace1 = 0;
  3. public int nospace2 = 0;
  4. [Space(10)]
  5. public int space = 0;
  6. public int nospace3 = 0;
  7. }

TextAreaAttribute

该属性可以把string在Inspector上的编辑区变成一个TextArea。
例子:

  1. public class TestTextAreaAttributeByLvmingbei : MonoBehaviour {
  2. [TextArea]
  3. public string mText;
  4. }

TooltipAttribute

这个属性可以为变量上生成一条tip,当鼠标指针移动到Inspector上时候显示。

  1. public class TestTooltipAttributeByLvmingbei : MonoBehaviour {
  2. [Tooltip("This year is 2015!")]
  3. public int year = 0;
  4. }

UnityAPICompatibilityVersionAttribute

用来声明API的版本兼容性

UnityEngine.Serialization

FormerlySerializedAsAttribute

该属性可以令变量以另外的名称进行序列化,并且在变量自身修改名称的时候,不会丢失之前的序列化的值。
例子:

  1. using UnityEngine;
  2. using UnityEngine.Serialization;
  3. public class MyClass : MonoBehaviour {
  4. [FormerlySerializedAs("myValue")]
  5. private string m_MyValue;
  6. public string myValue
  7. {
  8. get { return m_MyValue; }
  9. set { m_MyValue = value; }
  10. }
  11. }

UnityEngine.Editor

该package为Editor开发专用

CallbackOrderAttribute

定义Callback的顺序

CanEditMultipleObjects

Editor同时编辑多个Component的功能

CustomEditor

声明一个Class为自定义Editor的Class

CustomPreviewAttribute

将一个class标记为指定类型的自定义预览
Unity4.5以后提供的新功能
例子:

  1. [CustomPreview(typeof(GameObject))]
  2. public class MyPreview : ObjectPreview
  3. {
  4. public override bool HasPreviewGUI()
  5. {
  6. return true;
  7. }
  8.  
  9. public override void OnPreviewGUI(Rect r, GUIStyle background)
  10. {
  11. GUI.Label(r, target.name + " is being previewed");
  12. }
  13. }
CustomPropertyDrawer

标记自定义PropertyDrawer时候使用。
当自己创建一个PropertyDrawer或者DecoratorDrawer的时候,使用该属性来标记。 TODO: 如何创建属于自己的Attribute

DrawGizmo

可以在Scene视图中显示自定义的Gizmo
下面的例子,是在Scene视图中,当挂有MyScript的GameObject被选中,且距离相机距离超过10的时候,便显示自定义的Gizmo。
Gizmo的图片需要放入Assets/Gizmo目录中。
例子:

  1. using UnityEngine;
  2. using UnityEditor;
  3.  
  4. public class MyScript : MonoBehaviour {
  5.  
  6. }
  7.  
  8. public class MyScriptGizmoDrawer {
  9.  
  10. [DrawGizmo (GizmoType.Selected | GizmoType.Active)]
  11. static void DrawGizmoForMyScript (MyScript scr, GizmoType gizmoType) {
  12. Vector3 position = scr.transform.position;
  13.  
  14. if(Vector3.Distance(position, Camera.current.transform.position) > 10f)
  15. Gizmos.DrawIcon (position, "300px-Gizmo.png");
  16. }
  17.  
  18. }

InitializeOnLoadAttribute

在Class上使用,可以在Unity启动的时候,运行Editor脚本。
需要该Class拥有静态的构造函数。
做一个创建一个空的gameobject的例子。
例子:

  1. using UnityEditor;
  2. using UnityEngine;
  3.  
  4. [InitializeOnLoad]
  5. class MyClass
  6. {
  7. static MyClass ()
  8. {
  9. EditorApplication.update += Update;
  10. Debug.Log("Up and running");
  11. }
  12.  
  13. static void Update ()
  14. {
  15. Debug.Log("Updating");
  16. }
  17. }
InitializeOnLoadMethodAttribute

在Method上使用,是InitializeOnLoad的Method版本。
Method必须是static的。

MenuItem

在方法上使用,可以在Editor中创建一个菜单项,点击后执行该方法,可以利用该属性做很多扩展功能。 需要方法为static。
例子:

  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.Collections;
  4.  
  5. public class TestMenuItem : MonoBehaviour {
  6.  
  7. [MenuItem ("MyMenu/Create GameObject")]
  8. public static void CreateGameObject() {
  9. new GameObject("lvmingbei's GameObject");
  10. }
  11. }

PreferenceItem

使用该属性可以定制Unity的Preference界面。
在这里就使用官方的例子:

  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.Collections;
  4.  
  5. public class OurPreferences {
  6. // Have we loaded the prefs yet
  7. private static bool prefsLoaded = false;
  8.  
  9. // The Preferences
  10. public static bool boolPreference = false;
  11.  
  12. // Add preferences section named "My Preferences" to the Preferences Window
  13. [PreferenceItem ("My Preferences")]
  14. public static void PreferencesGUI () {
  15. // Load the preferences
  16. if (!prefsLoaded) {
  17. boolPreference = EditorPrefs.GetBool ("BoolPreferenceKey", false);
  18. prefsLoaded = true;
  19. }
  20.  
  21. // Preferences GUI
  22. boolPreference = EditorGUILayout.Toggle ("Bool Preference", boolPreference);
  23.  
  24. // Save the preferences
  25. if (GUI.changed)
  26. EditorPrefs.SetBool ("BoolPreferenceKey", boolPreference);
  27. }
  28. }

UnityEditor.Callbacks

这个package中是三个Callback的属性,都需要方法为static的。

OnOpenAssetAttribute

在打开一个Asset后被调用。
例子:

  1. using UnityEngine;
  2. using UnityEditor;
  3. using UnityEditor.Callbacks;
  4.  
  5. public class MyAssetHandler {
  6.  
  7. [OnOpenAssetAttribute(1)]
  8. public static bool step1(int instanceID, int line) {
  9. string name = EditorUtility.InstanceIDToObject(instanceID).name;
  10. Debug.Log("Open Asset step: 1 ("+name+")");
  11. return false; // we did not handle the open
  12. }
  13.  
  14. // step2 has an attribute with index 2, so will be called after step1
  15. [OnOpenAssetAttribute(2)]
  16. public static bool step2(int instanceID, int line) {
  17. Debug.Log("Open Asset step: 2 ("+instanceID+")");
  18. return false; // we did not handle the open
  19. }
  20. }
PostProcessBuildAttribute

该属性是在build完成后,被调用的callback。
同时具有多个的时候,可以指定先后顺序。
例子:

  1. using UnityEngine;
  2. using UnityEditor;
  3. using UnityEditor.Callbacks;
  4.  
  5. public class MyBuildPostprocessor {
  6. [PostProcessBuildAttribute(1)]
  7. public static void OnPostprocessBuild(BuildTarget target, string pathToBuiltProject) {
  8. Debug.Log( pathToBuiltProject );
  9. }
  10. }
PostProcessSceneAttribute

使用该属性的函数,在scene被build之前,会被调用。
具体使用方法和PostProcessBuildAttribute类似。

转自 http://blog.csdn.net/spring_shower/article/details/48708337

Unity3d Attribute 总结的更多相关文章

  1. Unity3d Attribute 总结(转)

      举两个例子,在变量上使用[SerializeFiled]属性,可以强制让变量进行序列化,可以在Unity的Editor上进行赋值. 在Class上使用[RequireComponent]属性,就会 ...

  2. Unity3D编辑器扩展(五)——常用特性(Attribute)以及Selection类

    前面写了四篇关于编辑器的: Unity3D编辑器扩展(一)——定义自己的菜单按钮 Unity3D编辑器扩展(二)——定义自己的窗口 Unity3D编辑器扩展(三)——使用GUI绘制窗口 Unity3D ...

  3. 关于Unity3D中的SerialField这个Attribute的功能

    首先我们看看效果,以下是源文件的内容: 然后对应的面板: 要注意的地方其实就这里: 可以看出,public默认就可以在面板中进行修改,相应的设为private的isCreateSoldier却不会出现 ...

  4. Unity3D 学习笔记

    不是什么技术文章,纯粹是我个人学习是遇到一些觉得需要注意的要点,当成笔记. 1.关于调试,在Android下无法断点,Debug也无法查看,查看日志方法可以启动adb的log功能,或者自己写个GUI控 ...

  5. Unity3D引擎扩展中的编辑器定制方法

    http://gamerboom.com/archives/36432 作者:Richard Fine Unity3D的方便之处在于,它很容易地扩展编辑器套件.每款游戏都对加工有着不同的需求,可以快速 ...

  6. Unity3D Optimizing Graphics Performance for iOS

    原地址:http://blog.sina.com.cn/s/blog_72b936d801013ptr.html icense Comparisons http://unity3d.com/unity ...

  7. [Unity3D]脚本中Start()和Awake()的区别

    Unity3D初学者经常把Awake和Start混淆. 简单说明一下,Awake在MonoBehavior创建后就立刻调用,Start将在MonoBehavior创建后在该帧Update之前,在该Mo ...

  8. Unity3D研究院之Inspector面板枚举的别名与排序

    虽然mono是支持unicode的.可以在枚举里写中文,但是我还是觉得写英文好一些.可是在编辑器上策划是希望看到的是中文的,还有就是枚举的展示排序功能,策划在编辑的时候为了方便希望把常用的枚举排上前面 ...

  9. c# 与 Unity3d 中的序列化

    圣典中对于Unity3D的序列化介绍很容易和C#的序列化介绍搞混,做个笔记,方便以后查找. 很多资料算是拾人牙慧. 一.Serializable 序列化 Inherits from Attribute ...

随机推荐

  1. 搜索引擎Hoot的源码阅读(提供源码)

    开门见山,最近阅读了一下一款开源引擎的源码,受益良多(学到了一些套路).外加好久没有写博客了(沉迷吃鸡,沉迷想念姑娘),特别开一篇.Hoot 的源码地址, 原理介绍地址.外加我看过之后的注释版本,当然 ...

  2. Stopwatch运行时间 Parallel并行任务

    using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using S ...

  3. 独立部署GeoWebCache

    在进行GIS项目开发中,常使用Geoserver作为开源的地图服务器,Geoserver是一个JavaEE项目,常通过Tomcat进行部署.而GeoWebCache是一个采用Java实现用于缓存WMS ...

  4. 指针和动态分配内存 (不定长度数组)------新标准c++程序设计

    背景: 数组的长度是定义好的,在整个程序中固定不变.c++不允许定义元素个数不确定的数组.例如: int n; int a[n]; //这种定义是不允许的 但是在实际编程中,往往会出现要处理的数据数量 ...

  5. codevs 3044 矩形面积求并

    3044 矩形面积求并   题目描述 Description 输入n个矩形,求他们总共占地面积(也就是求一下面积的并) 输入描述 Input Description 可能有多组数据,读到n=0为止(不 ...

  6. [51nod1236] 序列求和 V3(斐波那契数列)

    题面 传送门 题解 把求和的柿子用斐波那契数列的通项公式展开 \[ \begin{aligned} Ans &=\sum\limits_{i = 1}^{n} \left(\frac{(\fr ...

  7. Java面向对象之关键字super 入门实例

    一.基础概念 (一)super关键字 super关键字的用法和this相似.this代表的是当前对象.super代表的是父类中内存空间. 子父类中是不会出现同名属性的情况. (二)继承中.成员变量问题 ...

  8. mysql双机互相备份

    互备/***************************************master服务器**************************************/vi my.cnf[ ...

  9. idea中文输入问题

    desc: idea2017.3.4输入中文,光标不跟随. 解决方案:

  10. golang包引用解析

    golang包引用解析 环境变量配置如下: GOROOT----[C:\Go] GOPATH----[F:\workspace\go_home] vs code配置如下: F:\workspace\g ...