EntityTreeWindow.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #if ENABLE_VIEW
  2. using System;
  3. using UnityEditor;
  4. using UnityEditor.IMGUI.Controls;
  5. using UnityEngine;
  6. namespace ET
  7. {
  8. public class EntityTreeWindow: EditorWindow
  9. {
  10. internal static ComponentView VIEW_MONO;
  11. private static EntityTreeWindow WINDOW;
  12. private EntityTreeView treeView;
  13. private SearchField searchField;
  14. [MenuItem("ET/Entity Tree Window")]
  15. private static void OpenWindow()
  16. {
  17. if(!Application.isPlaying)
  18. {
  19. EditorUtility.DisplayDialog("警告", "运行后才可使用", "确定");
  20. return;
  21. }
  22. VIEW_MONO = new GameObject("View").AddComponent<ComponentView>();
  23. DontDestroyOnLoad(VIEW_MONO);
  24. WINDOW = GetWindow<EntityTreeWindow>(DockDefine.Types);
  25. WINDOW.titleContent = new GUIContent("Entity Tree Window");
  26. WINDOW.Show();
  27. }
  28. private void OnEnable()
  29. {
  30. this.treeView = new EntityTreeView(new TreeViewState());
  31. this.searchField = new SearchField();
  32. this.searchField.downOrUpArrowKeyPressed += this.treeView.SetFocusAndEnsureSelectedItem;
  33. EditorApplication.playModeStateChanged += OnPlayModeStateChange;
  34. }
  35. private void OnPlayModeStateChange(PlayModeStateChange state)
  36. {
  37. if(state != PlayModeStateChange.ExitingPlayMode)
  38. {
  39. return;
  40. }
  41. WINDOW.Close();
  42. }
  43. private void OnDestroy()
  44. {
  45. EditorApplication.playModeStateChanged -= OnPlayModeStateChange;
  46. DestroyImmediate(VIEW_MONO.gameObject);
  47. VIEW_MONO = null;
  48. }
  49. private void OnInspectorUpdate()
  50. {
  51. this.treeView?.Refresh();
  52. }
  53. private void OnGUI()
  54. {
  55. this.treeView.searchString = this.searchField.OnGUI(
  56. new Rect(
  57. 0,
  58. 0,
  59. position.width - 40f,
  60. 20f
  61. ),
  62. this.treeView.searchString
  63. );
  64. this.treeView.OnGUI(
  65. new Rect(
  66. 0,
  67. 20f,
  68. position.width,
  69. position.height - 40f
  70. )
  71. );
  72. GUILayout.BeginArea(
  73. new Rect(
  74. 20f,
  75. position.height - 18f,
  76. position.width - 40f,
  77. 16f
  78. )
  79. );
  80. using(new EditorGUILayout.HorizontalScope())
  81. {
  82. string style = "miniButton";
  83. if(GUILayout.Button("Expand all", style))
  84. {
  85. this.treeView.ExpandAll();
  86. }
  87. if(GUILayout.Button("Collapse all", style))
  88. {
  89. this.treeView.CollapseAll();
  90. }
  91. }
  92. GUILayout.EndArea();
  93. }
  94. }
  95. }
  96. #endif