TestHierarchyPopupMenuEditor.cs
using UnityEditor;
using UnityEngine;
public class TestHierarchyPopupMenuEditor : ScriptableObject {
[MenuItem("GameObject/Create New Child", true)]
private static bool ValidateCreateChild() {
GameObject[] gameObjects = Selection.gameObjects;
return gameObjects.Length > 0;
}
[MenuItem("GameObject/Create New Child", false, 11)]
private static void CreateChild() {
GameObject[] gameObjects = Selection.gameObjects;
for (int i = 0; i < gameObjects.Length; i++) {
var child = new GameObject("New Child");
child.transform.parent = gameObjects[i].transform;
}
}
}
修改为按下并释放鼠标中键弹出指定菜单
TestHierarchyPopupMenuEditor.cs
using UnityEditor;
using UnityEngine;
public class TestHierarchyPopupMenuEditor : ScriptableObject {
[InitializeOnLoadMethod]
private static void StartInitializeOnLoadMethod() {
EditorApplication.hierarchyWindowItemOnGUI += OnHierarchyGUI;
}
private static void OnHierarchyGUI(int instanceID, Rect selectionRect) {
if (Event.current!=null) {
Debug.Log($"button:{Event.current.button}, type:{Event.current.type}");
}
if (Event.current != null && Event.current.button == 2 && Event.current.type == EventType.MouseUp) { // 按下并释放鼠标中键时
//if (Selection.activeTransform) { // 选中激活一个 Transform 时才弹出
Vector2 mousePosition = Event.current.mousePosition;
EditorUtility.DisplayPopupMenu(new Rect(mousePosition.x, mousePosition.y, 0, 0), "GameObject/", null); // 弹出 GameObject 菜单
//}
}
}
}