Unity编辑器扩展秘籍-利用EditorApplication.contextualPropertyMenu为右键菜单增加自定义菜单选项
假设我们希望为材质右键弹出按钮增加新的功能,应该怎么做呢
我们可以通过注册EditorApplication.contextualPropertyMenu全局回调方法,增加自定义的MenuItem
using UnityEditor;
using UnityEngine;
namespace Yaojz
{
[InitializeOnLoad]
public static class MaterialContextMenuCopy
{
static MaterialContextMenuCopy()
{
EditorApplication.contextualPropertyMenu += OnPropertyContextMenu;
}
private static void OnPropertyContextMenu(GenericMenu menu, SerializedProperty property)
{
if (property.objectReferenceValue == null)
{
return;
}
var objValue = property.objectReferenceValue;
var type = objValue.GetType();
//这里判断是否是特定类型
if (type == typeof(Mesh))
{
menu.AddItem(new GUIContent("复制并引用"),false, () =>
{
});
}
else if (type == typeof(Material))
{
menu.AddItem(new GUIContent("复制并引用"),false, () =>
{
});
}
}
}
}