ECSView.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #if UNITY_EDITOR
  2. using UnityEditor;
  3. #endif
  4. using UnityEngine;
  5. namespace ETModel
  6. {
  7. /// <summary>
  8. /// ET组件可视化
  9. /// </summary>
  10. public class ECSView : MonoBehaviour
  11. {
  12. #region Static Parts
  13. /// <summary>
  14. /// 组件与其对应可视化对象map
  15. /// </summary>
  16. private static DoubleMap<object, ECSView> _dic = new DoubleMap<object, ECSView>();
  17. private static Transform root;
  18. /// <summary>
  19. /// 可视化对象根节点
  20. /// </summary>
  21. private static Transform Root
  22. {
  23. get
  24. {
  25. if (root == null)
  26. {
  27. root = new GameObject("ETViewRoot").transform;
  28. DontDestroyOnLoad(root);
  29. }
  30. return root;
  31. }
  32. }
  33. private static Transform pool;
  34. /// <summary>
  35. /// 组件放入Pool的可视化根节点
  36. /// </summary>
  37. private static Transform Pool
  38. {
  39. get
  40. {
  41. if (pool == null)
  42. {
  43. pool = new GameObject("Pool").transform;
  44. pool.parent = Root;
  45. }
  46. return pool;
  47. }
  48. }
  49. /// <summary>
  50. /// 创建组件的可视化节点
  51. /// </summary>
  52. /// <param name="self"></param>
  53. public static void CreateView(object self)
  54. {
  55. if (!Define.IsEditorMode)
  56. return;
  57. if (_dic.ContainsKey(self))
  58. return;
  59. ECSView view = new GameObject(self.GetType().ToString()).AddComponent<ECSView>();
  60. view.Component = self;
  61. _dic.Add(self, view);
  62. SetParent(self);
  63. }
  64. /// <summary>
  65. /// 销毁组件的可视化节点
  66. /// </summary>
  67. /// <param name="self"></param>
  68. public static void DestroyView(object self)
  69. {
  70. if (!Define.IsEditorMode)
  71. return;
  72. if (_dic.ContainsKey(self))
  73. {
  74. ECSView view = _dic.GetValueByKey(self);
  75. if (view != null)
  76. DestroyImmediate(view.gameObject);
  77. _dic.RemoveByKey(self);
  78. }
  79. }
  80. /// <summary>
  81. /// 根据组件获取可视化节点
  82. /// </summary>
  83. /// <param name="self"></param>
  84. /// <returns></returns>
  85. public static ECSView GetView(object self)
  86. {
  87. if (!Define.IsEditorMode)
  88. return null;
  89. if (_dic.ContainsKey(self))
  90. return _dic.GetValueByKey(self);
  91. return null;
  92. }
  93. /// <summary>
  94. /// 根据可视化节点获取其组件
  95. /// </summary>
  96. /// <param name="self"></param>
  97. /// <returns></returns>
  98. public static object GetComponent(ECSView self)
  99. {
  100. if (!Define.IsEditorMode)
  101. return null;
  102. if (_dic.ContainsValue(self))
  103. return _dic.GetKeyByValue(self);
  104. return null;
  105. }
  106. /// <summary>
  107. /// 放入Pool操作,修改可视化节点到Pool节点下
  108. /// </summary>
  109. /// <param name="self"></param>
  110. public static void ReturnPool(object self)
  111. {
  112. if (!Define.IsEditorMode)
  113. return;
  114. if (self == null)
  115. return;
  116. ECSView selfView = GetView(self);
  117. if (selfView == null)
  118. {
  119. _dic.RemoveByKey(self);
  120. return;
  121. }
  122. selfView.transform.parent = Pool;
  123. }
  124. /// <summary>
  125. /// 设置可视化父对象
  126. /// </summary>
  127. /// <param name="self"></param>
  128. /// <param name="parent"></param>
  129. public static void SetParent(object self, object parent = null)
  130. {
  131. if (!Define.IsEditorMode)
  132. return;
  133. if (self == null)
  134. return;
  135. ECSView selfView = GetView(self);
  136. if (selfView == null)
  137. {
  138. _dic.RemoveByKey(self);
  139. return;
  140. }
  141. ECSView parentView = GetView(parent);
  142. if (parentView != null)
  143. selfView.transform.parent = parentView.transform;
  144. else
  145. selfView.transform.parent = Root;
  146. }
  147. #endregion
  148. /// <summary>
  149. /// 该可视化节点对应的组件,对其属性显示到Inspector视图内
  150. /// </summary>
  151. public object Component;
  152. }
  153. #if UNITY_EDITOR
  154. [InitializeOnLoad]
  155. public class MyHierarchyEditor
  156. {
  157. static MyHierarchyEditor()
  158. {
  159. EditorApplication.hierarchyWindowItemOnGUI += OnHierarchyWindowItemOnGUI;
  160. }
  161. private static void OnHierarchyWindowItemOnGUI(int instanceID, Rect selectionRect)
  162. {
  163. GameObject obj = EditorUtility.InstanceIDToObject(instanceID) as GameObject;
  164. if (obj == null)
  165. return;
  166. if (obj.GetComponent<ECSView>() != null)
  167. {
  168. GUIStyle style = new GUIStyle(){
  169. padding ={ left =EditorStyles.label.padding.left-1, top = EditorStyles.label.padding.top },
  170. normal ={ textColor =Color.red }
  171. };
  172. GUI.Box(selectionRect, GUIContent.none);
  173. GUI.Label(selectionRect, obj.name, style);
  174. }
  175. }
  176. }
  177. #endif
  178. }