Unity插件菜单

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值