unity自定义特性7.2

 

前言

在导入别人插件的时候,常常会发现上面菜单栏中会多出几个菜单,在后来的学习中发现,原来unity还可以自定义特性

unity中的特性类分别定义在两个命名空间中   unityEngine 和 unityEditor.

AddComponentMenu特性在 UnityEngine中,Unity3D中常用的一些特性定义在UnityEngine中;

[AddComponentMenu("Transform/DeBugPlatform")]

这个要加在 一个Mono脚本前声明

[AddComponentMenu("Transform/DeBugPlatform")]

public class AttrDebugPlatfom:MonoBehaviour

{

void Start()

{

      Debug.Log( Application.platform.ToString());

}

}

这个作用就是运行时Debug平台。

可以把鼠标放到上面,VS F12 追踪到源可以发现AddComponentMenu 类是从 System.Attribute类中派生而来的

符合 CLS(Common Language Specification) 关于定制的要求,至少需要一个公共构造器。

例如以下

namespace UnityEngine{

public class AddComponentMent:System.Attribute

{

    public AddCompoentMenu(string compoentName)

    {.....

    }

}

}

在UnityEditor中是和编辑器相关的一些功能

MenuItem特性 这是个编辑器类最好放在Assets/Editor文件下

using UnityEditor;

using UnityEngine;

public class MyMenu:MonBehaviour

{

[MenuItem("May/shortcut Key %g ")]

static void DoSomeThingWithShortcutKey()

{

    Debug.Log("Do SomeThing With Shot cut key");

}

}

这里的%g在windows系统中代表快捷键ctrl+g Os X是 cmd+g

增加二倍重力的菜单项

[MenuItem("May/Rigbody/Double Mass")]

static void DoubleMass(MenuCommand command)

{

    Rigbody body=(Rigbody)command.context;

     body.mass*=2;

      print("body's mass is"+body.mass);

}

//增加一个菜单来创建自定义GameObjects;

[MenuItem("GameObject/MyCategory/Custom Game Object",false,10)]

static void CreatCustomGameObject(MenuCommand menuCommand)

{

     GameObject go = new  GameObject("Custom Game Object");

     GameObjectUtility. SetParentAndAlign(go, menuCommand.context as GameObject);

     Undo.RegisterCreatedObjectUndo(go,“Create”+go.name);

    Selection.activeObject=go;

}

有时候需要经常调整物体属性,数值之类。 一个MenuItem就可以很方便的解决这些问题;

比如:

using UnityEngine;

using Systems;

CubeInfo:MonoBehaviour

{

public Vector3 cubeScale=new Vector3(1.5f,1.5f,1.5f);

public Color CubeColor=Color.red;

}

Update()

{

    this.transform.localScale= cubeScale;    

    gameObject.GetComponent<MeshRenderer>().material.color=cubeColor;

    //修改cube比例和颜色

}


[MenuItem("CONTEXT/CubeInfo/SetColorAndVector")]

static void  SetCubeInfo(MenuCommand command)

{

CubeInfo cubeInfo=(CubeInfo)command.context;//获取CubeInfo脚本,物体上有挂载才行

cubeInfo.cubeScale=new Vector3(1f,1f,1f);

cubeInfo.cubeColor=Color.white;   //初始化脚本数据、

//MenuCommand的 基本使用,来一次性 初始化修改数据  对数量大的数据特别好用 。更多请访问unityAPI

}

还可以对脚本 判断有没有选中激活

[MenuItem("MyMenu/Log Selected Transform Name",true)]

 static bool ValidateLogSelectedTransformName()

{

 return Selection.activeTransform!=null;

}

   

还可以定义增加的定制特性类

System.AttributeUsageAttribute 类实例

特性的适用范围 适用于引用类型 不必适用于值类型或方法,或属性

制定引用类型

[AttributeUsage(AttributeTargets.Class,Inherited=false)]

public class CustomAttribute: System.Attribute

{

    string name;

        public CustomAttribute(string name)

        {

            this.name=name;

        }

}

AttributeUsageAttribute 类是指定另一特性类的用法的简单类


 它的构造函数为:

public AttributeUsageAttribute( AttributeTargets vaildOn)

{

        m_attributeTarget =validOn;

}

internal  AttributeUsageAttribute(AttributeTarget validOn, bool allowMultiple,bool inherited)

{

      m_attributeTarget =validOn;

    m_allowMultiple =allowMultiple;

    m_inherited=inherited;

}


AttributeTargets类型的参数 vaildOn  来指定 特性的适用范围 

它需要 一个AttributeTargets 类型 的参数 vaildOn 指明 特性的适用范围。

AttributeTargets 枚举的定义的代码如下

//AttributeTargets  这个 枚举值的定义如下

public enum AttributeTargets

{

    Assembly =0x0001,

    Module=0x0002,

    Class=0x0004,

     Struct =0x0008,

    Enum =0x0010,

    Constructor=0x0020,

   Method= 0x0040,

    Property =0x0080,

    Field =0x100,

     Event=0x200,

     Interface=0x400,

     Parameter=0x800,

    Delegate =0x1000,

  All=     Assembly |  Module | Class | Struct | Enum | Constructor | Method | Property | Field | Event | Interface | Parameter | Delegate

ClassMembers = | Class | Struct | Enum | Constructor | Method | Property | Field |      Event | Delegate | Interface

}

对CustomAttribute定义 实例化的  AttributeUsageAttribute 实例时 ,将 AttributeTargets.Class 作为参数传入

AttributeUsageAttribute 类的 构造函数,因此CustomAttribute 特性只能作用于引用类型。


定义这个特性

[CustomAttribute('ChenClass')]

class ChenClass

{

     public static void main(){}

}

http://www.cnblogs.com/murongxiaopifu

参考小黄书

猜你喜欢

转载自blog.csdn.net/qq2512667/article/details/80249527