foreword
When importing other people's plug-ins, it is often found that there will be several more menus in the menu bar above. In later learning, I found that Unity can also customize features
The feature classes in unity are defined in two namespaces unityEngine and unityEditor respectively.
The AddComponentMenu feature is in UnityEngine, and some commonly used features in Unity3D are defined in UnityEngine;
[AddComponentMenu("Transform/DeBugPlatform")]
This should be declared before a Mono script
[AddComponentMenu("Transform/DeBugPlatform")]
public class AttrDebugPlatfom:MonoBehaviour
{
void Start()
{
Debug.Log( Application.platform.ToString());
}
}
This role is the runtime Debug platform.
You can put the mouse on it, and VS F12 traces the source to find that the AddComponentMenu class is derived from the System.Attribute class
To comply with the CLS (Common Language Specification) requirements for customization, at least one public constructor is required.
For example the following
namespace UnityEngine{
public class AddComponentMent:System.Attribute
{
public AddCompoentMenu(string compoentName)
{.....
}
}
}
In UnityEditor are some functions related to the editor
MenuItem feature This is an editor class best placed in the Assets/Editor file
using UnityEditor;
using UnityEngine;
public class MyMenu:MonBehaviour
{
[MenuItem("May/shortcut Key %g ")]
static void DoSomeThingWithShortcutKey()
{
Debug.Log("Do SomeThing With Shot cut key");
}
}
The %g here represents the shortcut key ctrl+g Os X is cmd+g in the windows system
Menu item that adds double gravity
[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);
}
//Add a menu to create custom 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;
}
Sometimes it is necessary to frequently adjust the properties of objects, values and the like. A MenuItem can easily solve these problems;
for example:
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;
//Modify cube scale and color
}
[MenuItem("CONTEXT/CubeInfo/SetColorAndVector")]
static void SetCubeInfo(MenuCommand command)
{
CubeInfo cubeInfo=(CubeInfo)command.context;//Get the CubeInfo script, only if there is a mount on the object
cubeInfo.cubeScale=new Vector3(1f,1f,1f);
cubeInfo.cubeColor=Color.white; // Initialize script data,
//The basic use of MenuCommand to initialize and modify data at one time is especially useful for large amounts of data. For more, please visit unityAPI
}
You can also judge whether the script is activated or not
[MenuItem("MyMenu/Log Selected Transform Name",true)]
static bool ValidateLogSelectedTransformName()
{
return Selection.activeTransform!=null;
}
You can also define additional custom feature classes
System.AttributeUsageAttribute class instance
Attributes that apply to reference types do not have to apply to value types or methods, or properties
Make a reference type
[AttributeUsage(AttributeTargets.Class,Inherited=false)]
public class CustomAttribute: System.Attribute
{
string name;
public CustomAttribute(string name)
{
this.name=name;
}
}
The AttributeUsageAttribute class is a simple class that specifies the usage of another attribute class
Its constructor is:
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 type parameter failOn to specify the scope of application of the attribute
It requires a parameter of type AttributeTargets, failOn, to indicate the scope of the attribute.
The code for the definition of the AttributeTargets enumeration is as follows
// The definition of the enumeration value of AttributeTargets is as follows
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
}
When defining an instantiated AttributeUsageAttribute instance for CustomAttribute, pass in AttributeTargets.Class as a parameter
Constructor of the AttributeUsageAttribute class, so the CustomAttribute attribute only works on reference types.
define this property
[CustomAttribute('ChenClass')]
class ChenClass
{
public static void main(){}
}
http://www.cnblogs.com/murongxiaopifu
Refer to the Little Yellow Book