最近在研究Attribute,感觉挺好玩,搜到一篇不错的文章,分享给大家

原文:未知?找到后补上!

举两个例子,在变量上使用[SerializeFiled]属性,可以强制让变量进行序列化,可以在Unity的Editor上进行赋值。
在Class上使用[RequireComponent]属性,就会在Class的GameObject上自动追加所需的Component。

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

UnityEngine

AddComponentMenu

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

[AddComponentMenu("TestMenu/TestComponet")]
public class TestMenu : MonoBehaviour {
}

AssemblyIsEditorAssembly

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

ContextMenu

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

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

ContextMenuItemAttribute

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

public class Sample : MonoBehaviour {
[ContextMenuItem("Reset", "ResetName")]
public string name = "Default";
void ResetName() {
name = "Default";
}
}

DisallowMultipleComponent

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

ExecuteInEditMode

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

HeaderAttribute

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

public class ExampleClass : MonoBehaviour {
[Header("生命值")]
public int CurrentHP = 0;
public int MaxHP = 100; [Header("魔法值")]
public int CurrentMP = 0;
public int MaxMP = 0;
}

HideInInspector

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

ImageEffectOpaque

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

[ImageEffectOpaque]
void OnRenderImage (RenderTexture source, RenderTexture destination){
}
ImageEffectTransformsToLDR

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

MultilineAttribute

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

public class TestString : MonoBehaviour {
[MultilineAttribute]
public string mText;
}

NotConvertedAttribute

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

NotFlashValidatedAttribute

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

NotRenamedAttribute

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

PropertyAttribute
RangeAttribute

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

public class TestRange : MonoBehaviour
{
[Range(0, 100)] public int HP;
}
RequireComponent

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

例子

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


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

RPC

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

[RPC]
void RemoteMethod(){
}
RuntimeInitializeOnLoadMethodAttribute

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

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

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

SerializeField

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

public class TestSerializeField : MonoBehaviour {
[SerializeField]
private string name; [SerializeField]
private Button _button;
}

SharedBetweenAnimatorsAttribute

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

SpaceAttribute

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

public class TestSpaceAttributeByLvmingbei : MonoBehaviour {
public int nospace1 = 0;
public int nospace2 = 0;
[Space(10)]
public int space = 0;
public int nospace3 = 0;
}

TextAreaAttribute

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

public class TestTextAreaAttributeByLvmingbei : MonoBehaviour {
[TextArea]
public string mText;
}

TooltipAttribute

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

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

UnityAPICompatibilityVersionAttribute

用来声明API的版本兼容性

UnityEngine.Serialization

FormerlySerializedAsAttribute

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

using UnityEngine;
using UnityEngine.Serialization;
public class MyClass : MonoBehaviour {
[FormerlySerializedAs("myValue")]
private string m_MyValue;
public string myValue
{
get { return m_MyValue; }
set { m_MyValue = value; }
}
}

UnityEngine.Editor

该package为Editor开发专用

CallbackOrderAttribute

定义Callback的顺序

CanEditMultipleObjects

Editor同时编辑多个Component的功能

CustomEditor

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

CustomPreviewAttribute

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

[CustomPreview(typeof(GameObject))]
public class MyPreview : ObjectPreview
{
public override bool HasPreviewGUI()
{
return true;
} public override void OnPreviewGUI(Rect r, GUIStyle background)
{
GUI.Label(r, target.name + " is being previewed");
}
}
CustomPropertyDrawer

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

DrawGizmo

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

using UnityEngine;
using UnityEditor; public class MyScript : MonoBehaviour { } public class MyScriptGizmoDrawer { [DrawGizmo (GizmoType.Selected | GizmoType.Active)]
static void DrawGizmoForMyScript (MyScript scr, GizmoType gizmoType) {
Vector3 position = scr.transform.position; if(Vector3.Distance(position, Camera.current.transform.position) > 10f)
Gizmos.DrawIcon (position, "300px-Gizmo.png");
} }

InitializeOnLoadAttribute

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

using UnityEditor;
using UnityEngine; [InitializeOnLoad]
class MyClass
{
static MyClass ()
{
EditorApplication.update += Update;
Debug.Log("Up and running");
} static void Update ()
{
Debug.Log("Updating");
}
}
InitializeOnLoadMethodAttribute

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

MenuItem

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

using UnityEngine;
using UnityEditor;
using System.Collections; public class TestMenuItem : MonoBehaviour { [MenuItem ("MyMenu/Create GameObject")]
public static void CreateGameObject() {
new GameObject("lvmingbei's GameObject");
}
}

PreferenceItem

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

using UnityEngine;
using UnityEditor;
using System.Collections; public class OurPreferences {
// Have we loaded the prefs yet
private static bool prefsLoaded = false; // The Preferences
public static bool boolPreference = false; // Add preferences section named "My Preferences" to the Preferences Window
[PreferenceItem ("My Preferences")]
public static void PreferencesGUI () {
// Load the preferences
if (!prefsLoaded) {
boolPreference = EditorPrefs.GetBool ("BoolPreferenceKey", false);
prefsLoaded = true;
} // Preferences GUI
boolPreference = EditorGUILayout.Toggle ("Bool Preference", boolPreference); // Save the preferences
if (GUI.changed)
EditorPrefs.SetBool ("BoolPreferenceKey", boolPreference);
}
}

UnityEditor.Callbacks

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

OnOpenAssetAttribute

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

using UnityEngine;
using UnityEditor;
using UnityEditor.Callbacks; public class MyAssetHandler { [OnOpenAssetAttribute(1)]
public 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
} // step2 has an attribute with index 2, so will be called after step1
[OnOpenAssetAttribute(2)]
public static bool step2(int instanceID, int line) {
Debug.Log("Open Asset step: 2 ("+instanceID+")");
return false; // we did not handle the open
}
}
PostProcessBuildAttribute

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

using UnityEngine;
using UnityEditor;
using UnityEditor.Callbacks; public class MyBuildPostprocessor {
[PostProcessBuildAttribute(1)]
public static void OnPostprocessBuild(BuildTarget target, string pathToBuiltProject) {
Debug.Log( pathToBuiltProject );
}
}
PostProcessSceneAttribute

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

Unity C# 关于Attribute的使用的更多相关文章

  1. Unity中使用Attribute

    Attribute是c#的语言特性 msdn说明如下: The Attribute class associates predefined system information or user-def ...

  2. 从Unity中的Attribute到AOP(七)

    本章我们将依然讲解Unity中的Attribute,继续命名空间在UnityEngine里的. PropertyAttribute,这个特性主要来控制变量或者类在Inspector里面的显示方式.和P ...

  3. 从Unity中的Attribute到AOP(六)

    本文将重点对Unity剩下常用的Attribute进行讲解,其他不常用的Attribute各位可以自行去官方文档查阅. 首先是UnityEngine命名空间下的. ColorUsage,这个主要作用于 ...

  4. 从Unity中的Attribute到AOP(五)

    今天主要来讲一下Unity中带Menu的Attribute. 首先是AddComponentMenu.这是UnityEngine命名空间下的一个Attribute. 按照官方文档的说法,会在Compo ...

  5. 从Unity中的Attribute到AOP(四)

    本篇我们将逐一讲解Unity中经常使用的Attribute(Unity对应的文档版本为2018.1b). 首先是Serializable,SerializeField以及NonSerialized,H ...

  6. 从Unity中的Attribute到AOP(三)

    上一篇我们对系统的Attributes进行了MSIL代码的查看,了解到了其本质就是一个类的构造函数.本章我们将编写自己的Attributes. 首先我们定义书的属性代码,如下: [AttributeU ...

  7. 从Unity中的Attribute到AOP(八)

    本文将讲一下在UnityEditor命名空间下的一些特性. CallBackOrder,这个Attribute是所有带callback index的Attribute的基类,由于官方也没有给出详细的说 ...

  8. 从Unity中的Attribute到AOP(二)

    上一篇文章我们初步了解了一下Attributes的含义,并且使用系统自带的Attributes写了点代码.在进一步解剖我们的代码之前,我觉得有个概念可能需要巩固一下:什么是元数据? 我们知道C#代码会 ...

  9. 从Unity中的Attribute到AOP(一)

    首先来看一下微软官方对Attributes(C#)的定义: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/conce ...

随机推荐

  1. Vim编辑器的学习

    在老师的带领下,最近也算是涨了见识.自己安装并尝试着体验了Vim的一些基本功能,可能是作为初学者,总感觉其指令太过复杂,就文本编辑而言,给我的最大感受就是神而乎之,一头雾水.目前我对这款编译器的掌握水 ...

  2. [poj 2456] Aggressive cows 二分

    Description Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stal ...

  3. 对接ebay,订单系统开发

    要求,从ebay 下载的 csv文件,导入销售订单的系统,成为自己的订单. 首先,上传文件. 第二步,将上传的csv读入系统的一个变量 根据','来分割成一个数组 $str = file_get_co ...

  4. nagios安装使用指南

    话不多说,下面开始,nagios具体的介绍,可以搜一下,这篇文章为作者在实际操作中整理出来,写出来的都是负责人的内容~ 环境准备 此文档共用2台服务器的配置,操作系统均为centOS6.7,安装用户都 ...

  5. db2联邦数据库

    目标机器:192.168.0.16 本地机器:192.168.0.18 .登陆本地数据库 db2 connect to dwmm user dainst using dainst ## 打开联邦数据库 ...

  6. 并查集【洛谷P1197】 [JSOI2008]星球大战

    P1197 [JSOI2008]星球大战 题目描述 很久以前,在一个遥远的星系,一个黑暗的帝国靠着它的超级武器统治着整个星系. 某一天,凭着一个偶然的机遇,一支反抗军摧毁了帝国的超级武器,并攻下了星系 ...

  7. 「模拟赛20180406」膜树 prufer编码+概率

    题目描述 给定一个完全图,保证\(w_{u,v}=w_{v,u}\)且\(w_{u,u}=0\),等概率选取一个随机生成树,对于每一对\((u,v)\),求\(dis(u,v)\)的期望值对\(998 ...

  8. ArcGIS api for javascript-图层控制(图层树)

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  9. CF912D Fishes 期望

    题意翻译 Description 有一个长为nnn ,宽为mmm 的鱼缸,还有一个边长为rrr 的正方形渔网.你可以往鱼缸里放kkk 条鱼,问用渔网随机在浴缸里捞鱼的最大期望是多少.不懂什么是期望的自 ...

  10. 安装gitlab-runner

    # 下载 $sudo wget -O /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/lat ...