Hierarchy拓展

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEditor;
  5. public class TestHierachy : MonoBehaviour
  6. {
  7. [MenuItem("GameObject/Function2",false,0)]
  8. static void Function2()
  9. {
  10. Debug.Log("运行成功");
  11. }
  12. [InitializeOnLoadMethod]
  13. static void IntiallizeOnLoad()
  14. {
  15. //Hierarchy面板右侧出现按键操作
  16. EditorApplication.hierarchyWindowItemOnGUI += (instanceID, rect) =>
  17. {
  18. //GUI.Button(new Rect(100, 100, 100, 100), "Test1");
  19. if (Selection.activeObject != null && instanceID == Selection.activeObject.GetInstanceID())
  20. {
  21. rect.x = rect.width - 50;
  22. rect.width = 100;
  23. if (GUI.Button(rect, "delete")) //按键删除功能
  24. {
  25. GameObject.DestroyImmediate(Selection.activeObject);
  26. }
  27. }
  28. };
  29. }
  30. }

重写菜单

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEditor;
  5. //重写菜单
  6. public class TestMenuOverride
  7. {
  8. //监听右键事件
  9. [InitializeOnLoadMethod]
  10. static void InitOnLoad()
  11. {
  12. EditorApplication.hierarchyWindowItemOnGUI += (instanceID,rect) =>
  13. {
  14. if(Event.current != null && Event.current.button == 1 && Event.current.type == EventType.MouseUp) {
  15. Debug.Log("鼠标抬起");
  16. //弹出自定义的菜单 "Assets"带标使用的该菜单,可修改为自定义的菜单
  17. EditorUtility.DisplayPopupMenu(new Rect(Event.current.mousePosition.x, Event.current.mousePosition.y,0,0) ,"Assets", null);
  18. //不让事件继续往下传递,不然会弹出原生的菜单
  19. Event.current.Use();//把事件用掉就不会往下传递了
  20. }
  21. };
  22. }
  23. }