Unity插件菜单

来源:互联网 发布:淘宝转化率控制在多少 编辑:程序博客网 时间:2024/05/17 04:54

Unity MenuItem API

public MenuItem(string itemName);public MenuItem(string itemName, bool isValidateFunction);public MenuItem(string itemName, bool isValidateFunction, int priority);// 若前后两个菜单的priority相差超过10,就会自动创建一个分隔线

Unity MenuItemAttribute API

// 添加一个自定义菜单跟项[MenuItem ("MyMenu/Do Something")]static void DoSomething () {    Debug.Log ("Doing Something...");}// 添加一个待验证的项[MenuItem ("MyMenu/Log Selected Transform Name")]static void LogSelectedTransformName (){    Debug.Log ("Selected Transform is on " + Selection.activeTransform.gameObject.name + ".");}// 作为上面菜单项的验证函数, ValidateXXX[MenuItem ("MyMenu/Log Selected Transform Name", true)]static bool ValidateLogSelectedTransformName () {    // Return false if no transform is selected.    return Selection.activeTransform != null;}// 添加一个菜单,并且设置相应的快捷键[MenuItem ("MyMenu/Do Something with a Shortcut Key %g")]static void DoSomethingWithAShortcutKey () {    Debug.Log ("Doing something with a Shortcut Key...");}// 给一个组件界面右键菜单,添加项[MenuItem ("CONTEXT/Rigidbody/Double Mass")]static void DoubleMass (MenuCommand command) {    Rigidbody body = (Rigidbody)command.context;    body.mass = body.mass * 2;    Debug.Log ("Doubled Rigidbody's Mass to " + body.mass + " from Context Menu.");}// 在Create GameObject菜单添加项[MenuItem("GameObject/MyCategory/Custom Game Object", false, 10)]static void CreateCustomGameObject(MenuCommand menuCommand) {    // Create a custom game object    GameObject go = new GameObject("Custom Game Object");    // Ensure it gets reparented if this was a context click (otherwise does nothing)    GameObjectUtility.SetParentAndAlign(go, menuCommand.context as GameObject);    // Register the creation in the undo system    Undo.RegisterCreatedObjectUndo(go, "Create " + go.name);    Selection.activeObject = go;}

CreateAssetMenu API

// 这种方式,只能拿来修改具体的序列化类,不能统一写在一个地方[CreateAssetMenu(fileName="x",menuName="(custom) X")][System.Serializable]public class X : ScriptableObject {    public float m_a;}
原创粉丝点击