UINodeEditor.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEditor;
  5. using UnityEngine.UI;
  6. [CustomEditor(typeof(UINode))]
  7. [CanEditMultipleObjects]
  8. public class UINodeEditor : Editor
  9. {
  10. struct UINodeEditorSetting
  11. {
  12. public string[] keys;
  13. public string[] values;
  14. public UINodeEditorSetting(string[] keys, string[] values)
  15. {
  16. this.keys = keys;
  17. this.values = values;
  18. }
  19. }
  20. static List<UINodeEditorSetting> UINodeEditorSettings = new List<UINodeEditorSetting>
  21. {
  22. //new UINodeEditorSetting(new string[2]{ typeof(Button).FullName, typeof(Image).FullName }, new string[1]{ typeof(Image).FullName }),
  23. new UINodeEditorSetting(new string[1]{ typeof(Text).FullName }, new string[1]{ typeof(Text).FullName }),
  24. new UINodeEditorSetting(new string[1]{ typeof(Button).FullName }, new string[1]{ typeof(Button).FullName }),
  25. new UINodeEditorSetting(new string[1]{ typeof(Toggle).FullName }, new string[1]{ typeof(Toggle).FullName }),
  26. new UINodeEditorSetting(new string[1]{ typeof(Slider).FullName }, new string[1]{ typeof(Slider).FullName }),
  27. new UINodeEditorSetting(new string[1]{ typeof(Scrollbar).FullName }, new string[1]{ typeof(Scrollbar).FullName }),
  28. new UINodeEditorSetting(new string[1]{ typeof(Dropdown).FullName }, new string[1]{ typeof(Dropdown).FullName }),
  29. new UINodeEditorSetting(new string[1]{ typeof(InputField).FullName }, new string[1]{ typeof(InputField).FullName }),
  30. };
  31. void OnEnable()
  32. {
  33. }
  34. public override void OnInspectorGUI()
  35. {
  36. base.OnInspectorGUI();
  37. serializedObject.Update();
  38. List<UINode> nodes = new List<UINode>();
  39. UnityEngine.Object[] targetObjects = serializedObject.targetObjects;
  40. foreach (UnityEngine.Object targetObject in targetObjects)
  41. {
  42. UINode uiNode = targetObject as UINode;
  43. if (uiNode == null)
  44. {
  45. continue;
  46. }
  47. if (uiNode.keys == null)
  48. {
  49. uiNode.keys = new List<string>();
  50. }
  51. if (uiNode.values == null)
  52. {
  53. uiNode.values = new List<bool>();
  54. }
  55. //check
  56. Component[] components = uiNode.gameObject.GetComponents<Component>();
  57. for (int i = uiNode.keys.Count - 1; i >= 0; i--)
  58. {
  59. string key = uiNode.keys[i];
  60. bool b = false;
  61. foreach (var component in components)
  62. {
  63. if (component.GetType().FullName == key)
  64. {
  65. b = true;
  66. break;
  67. }
  68. }
  69. if (!b)
  70. {
  71. uiNode.keys.RemoveAt(i);
  72. uiNode.values.RemoveAt(i);
  73. }
  74. }
  75. List<string> addComp = new List<string>();
  76. foreach (var component in components)
  77. {
  78. bool b = false;
  79. for (int i = 0; i < uiNode.keys.Count; i++)
  80. {
  81. string key = uiNode.keys[i];
  82. if (component.GetType().FullName == key)
  83. {
  84. b = true;
  85. break;
  86. }
  87. }
  88. if (!b)
  89. {
  90. addComp.Add(component.GetType().FullName);
  91. //设置默认
  92. uiNode.keys.Add(component.GetType().FullName);
  93. uiNode.values.Add(false);
  94. }
  95. }
  96. if (addComp.Count > 0)
  97. {
  98. foreach (var settings in UINodeEditorSettings)
  99. {
  100. bool b = true;
  101. foreach (var key in settings.keys)
  102. {
  103. bool tmpB = false;
  104. foreach (var addC in addComp)
  105. {
  106. if (addC == key)
  107. {
  108. tmpB = true;
  109. break;
  110. }
  111. }
  112. if (!tmpB)
  113. {
  114. b = false;
  115. break;
  116. }
  117. }
  118. if (b)
  119. {
  120. foreach (var value in settings.values)
  121. {
  122. uiNode.SetValue(value, true);
  123. }
  124. }
  125. }
  126. }
  127. nodes.Add(uiNode);
  128. }
  129. Dictionary<string, bool> keyValues = new Dictionary<string, bool>();
  130. Color tmp = GUI.color;
  131. foreach (var firstNode in nodes)
  132. {
  133. for (int i = 0; i < firstNode.keys.Count; i++)
  134. {
  135. string key = firstNode.keys[i];
  136. if (key == "UINode")
  137. {
  138. continue;
  139. }
  140. bool currB = firstNode.values[i];
  141. bool b = true;
  142. bool bSame = true;
  143. foreach (var node in nodes)
  144. {
  145. bool value;
  146. if (!node.GetValue(key, out value))
  147. {
  148. b = false;
  149. break;
  150. }
  151. else
  152. {
  153. if (value != currB)
  154. {
  155. bSame = false;
  156. }
  157. }
  158. }
  159. if (b)
  160. {
  161. if (!bSame)
  162. {
  163. GUI.color = Color.red;
  164. currB = false;
  165. }
  166. string name = key;
  167. int indexOf = name.LastIndexOf(".");
  168. if (indexOf != -1)
  169. {
  170. name = name.Substring(indexOf + 1, name.Length - indexOf - 1);
  171. }
  172. bool toggle = EditorGUILayout.Toggle(name, currB);
  173. if (currB != toggle)
  174. {
  175. keyValues[key] = toggle;
  176. }
  177. if (!bSame)
  178. GUI.color = tmp;
  179. }
  180. }
  181. break;
  182. }
  183. GUI.color = tmp;
  184. foreach (var key in keyValues.Keys)
  185. {
  186. foreach (var n in nodes)
  187. {
  188. n.SetValue(key, keyValues[key]);
  189. }
  190. }
  191. //EditorUtility.SetDirty(target);
  192. serializedObject.ApplyModifiedProperties();
  193. }
  194. }