Unity 编辑器扩展(二)MenuItem(添加右击自定义菜单)

在Project视图添加右击自定义菜单

将自定义菜单首层目录写为Assets即可. 

using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

public class Test : MonoBehaviour
{
    [MenuItem("Assets/ItemA")]
    static void MyTestA()
    {
        Debug.Log(1);
    }
}

 在Hierarchy视图添加右击创建自定义物体菜单

using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

public class Test : MonoBehaviour
{
    [MenuItem("GameObject/ItemA",false,10)]
    static void MyTestA(MenuCommand menuCommand)
    {
        GameObject obj = new GameObject("ItemA");//创建新物体
        GameObjectUtility.SetParentAndAlign(obj, menuCommand.context as GameObject);//设置父节点为当前选中物体
        Undo.RegisterCreatedObjectUndo(obj, "Create" + obj.name);//注册到Undo系统,允许撤销
        Selection.activeObject = obj;//将新建物体设为当前选中物体
        Debug.Log(1);
    }
}

 MenuItem表示当前正在操作的组件,通过context属性获取到对应的脚本对象.

 在Inspector视图添加某组件对应的右击自定义菜单

using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

public class Test : MonoBehaviour
{
    [MenuItem("CONTEXT/Button/MyItem")]
    static void MyTestA()
    {
        Debug.Log(1);
    }
}

 格式:[MenuItem("CONTEXT/脚本名称(组件名称)/菜单名称")]

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值