在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/脚本名称(组件名称)/菜单名称")]