Unity ContextMenu 扩展组件的环境菜单(在 Inspector 视图组件名称上的右击下拉菜单)
public class EndArea : MonoBehaviour {
#if UNITY_EDITOR
[ContextMenu("Generate")]
private void Generate () {
Debug.Log("Generate");
}
#endif
}
扩展内置组件的环境菜单
EditorRigidbodyMenu.cs
#if UNITY_EDITOR
using UnityEditor;
using UnityEngine;
public class EditorRigidbodyMenu : Editor {
[MenuItem("CONTEXT/Rigidbody/在Rigidbody上右键")] // 注意此处使用的不是 ContextMenu
static void DoIt () {
EditorUtility.DisplayDialog("MyTool", "Do It in C# !", "OK", "");
}
}
#endif
多个组件共同使用用一个方法
#if UNITY_EDITOR
using UnityEditor;
using UnityEngine;
public class EditorMeshContextMenu : Editor {
[MenuItem("CONTEXT/MeshRenderer/在 MeshRenderer上右键")]
[MenuItem("CONTEXT/SkinnedMeshRenderer/在 SkinnedMeshRenderer上右键")]
static void DoIt () {
EditorUtility.DisplayDialog("MyTool", "Do It in C# !", "OK", "");
}
}
#endif