MenuEditor.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. [CustomEditor(typeof(Menu))]
  6. public class MenuEditor : Editor {
  7. #region Inherited Properties
  8. SerializedProperty visible;
  9. SerializedProperty useSimpleActivation;
  10. SerializedProperty showingClip;
  11. SerializedProperty hidingClip;
  12. SerializedProperty onShow;
  13. SerializedProperty onHide;
  14. SerializedProperty ignoreEventsOnInitialization;
  15. #endregion
  16. SerializedProperty previousMenu;
  17. SerializedProperty nextMenu;
  18. SerializedProperty deactivateWhileInvisible;
  19. SerializedProperty animatedElements;
  20. SerializedProperty multiMenusAnimatedElements;
  21. SerializedProperty switchAfter;
  22. private bool updatedElements;
  23. private int added = 0;
  24. private int removed = 0;
  25. void OnEnable()
  26. {
  27. #region Inherited Properties
  28. visible = serializedObject.FindProperty("Visible");
  29. useSimpleActivation = serializedObject.FindProperty("UseSimpleActivation");
  30. showingClip = serializedObject.FindProperty("ShowingClip");
  31. hidingClip = serializedObject.FindProperty("HidingClip");
  32. onShow = serializedObject.FindProperty("OnShow");
  33. onHide = serializedObject.FindProperty("OnHide");
  34. ignoreEventsOnInitialization = serializedObject.FindProperty("IgnoreEventsOnInitialization");
  35. #endregion
  36. previousMenu = serializedObject.FindProperty("PreviousMenu");
  37. nextMenu = serializedObject.FindProperty("NextMenu");
  38. deactivateWhileInvisible = serializedObject.FindProperty("DeactivateWhileInvisible");
  39. animatedElements = serializedObject.FindProperty("AnimatedElements");
  40. multiMenusAnimatedElements = serializedObject.FindProperty("MultiMenusAnimatedElements");
  41. switchAfter = serializedObject.FindProperty("SwitchAfter");
  42. }
  43. public override void OnInspectorGUI()
  44. {
  45. Menu myMenu = target as Menu;
  46. ZUIManager zuiManager = FindObjectOfType<ZUIManager>();
  47. #region Activate Button
  48. if (GUILayout.Button("Activate", GUILayout.Height(30)))
  49. {
  50. List<Menu> AllMenus = GetAllMenus();
  51. foreach (Menu m in AllMenus)
  52. {
  53. if (m == null) continue;
  54. Undo.RecordObject(m.gameObject, "Activate Menu");
  55. if (m == myMenu)
  56. {
  57. m.gameObject.SetActive(true);
  58. }
  59. else
  60. {
  61. m.gameObject.SetActive(false);
  62. foreach (UIElement ue in m.MultiMenusAnimatedElements)
  63. {
  64. if (ue != null)
  65. ue.gameObject.SetActive(false);
  66. }
  67. }
  68. }
  69. foreach (UIElement ue in myMenu.MultiMenusAnimatedElements)
  70. {
  71. if (ue != null)
  72. ue.gameObject.SetActive(true);
  73. }
  74. }
  75. #endregion
  76. #region User Interface
  77. EditorGUILayout.Space();
  78. EditorGUILayout.LabelField("Settings", EditorStyles.boldLabel);
  79. EditorGUILayout.LabelField("Is Visible?", visible.boolValue.ToString());
  80. EditorGUILayout.PropertyField(previousMenu);
  81. EditorGUILayout.PropertyField(nextMenu);
  82. EditorGUILayout.PropertyField(deactivateWhileInvisible);
  83. EditorGUILayout.Space();
  84. EditorGUILayout.LabelField("Menu Elements", EditorStyles.boldLabel);
  85. EditorGUILayout.PropertyField(animatedElements, true);
  86. EditorGUILayout.PropertyField(multiMenusAnimatedElements, true);
  87. EditorGUILayout.Space();
  88. EditorGUILayout.LabelField("Switching", EditorStyles.boldLabel);
  89. if (useSimpleActivation.boolValue == true)
  90. {
  91. EditorGUILayout.HelpBox("No animations will be played, all the animated elements will be ignored because \"Use Simple Activation\" option is set to true.", MessageType.Info);
  92. }
  93. EditorGUILayout.PropertyField(useSimpleActivation);
  94. EditorGUILayout.PropertyField(switchAfter);
  95. EditorGUILayout.Space();
  96. EditorGUILayout.LabelField("Sounds", EditorStyles.boldLabel);
  97. EditorGUILayout.PropertyField(showingClip);
  98. EditorGUILayout.PropertyField(hidingClip);
  99. EditorGUILayout.Space();
  100. EditorGUILayout.LabelField("Events", EditorStyles.boldLabel);
  101. EditorGUILayout.PropertyField(onShow);
  102. EditorGUILayout.PropertyField(onHide);
  103. EditorGUILayout.PropertyField(ignoreEventsOnInitialization, new GUIContent("Ignore On Initialization"));
  104. EditorGUILayout.Space();
  105. #endregion
  106. #region Tools
  107. EditorGUILayout.LabelField("Tools", EditorStyles.boldLabel);
  108. #region Update Animated Elements Button
  109. if (GUILayout.Button("Update Animated Elements", GUILayout.Height(30)))
  110. {
  111. //Save old elements list to make a check after updating.
  112. List<UIElement> oldElements = myMenu.AnimatedElements;
  113. added = 0;
  114. removed = 0;
  115. Undo.RecordObject(myMenu, "Update Animated Items");
  116. myMenu.AnimatedElements = GetAnimatedElements(myMenu.transform);
  117. UIElement menuUE = myMenu.GetComponent<UIElement>();
  118. if (menuUE)
  119. myMenu.AnimatedElements.Insert(0, menuUE);
  120. //Check which elements are added and which elements are removed.
  121. for (int i = 0; i < myMenu.AnimatedElements.Count; i++)
  122. {
  123. Undo.RecordObject(myMenu.AnimatedElements[i], "Update Animated Items");
  124. if (!oldElements.Contains(myMenu.AnimatedElements[i]))
  125. {
  126. added++;
  127. }
  128. }
  129. removed = oldElements.Count - myMenu.AnimatedElements.Count + added;
  130. updatedElements = true;
  131. }
  132. #endregion
  133. #region Elements Updated Info
  134. if (updatedElements)
  135. {
  136. string removedText = removed != 0 ? "Removed " + removed + " element" + (removed == 1? "." : "s.") : "";
  137. string addedText = added != 0 ? "Added " + added + " element" + (added == 1 ? ". " : "s. ") : "";
  138. string finalText = (added != 0 || removed != 0) ? addedText + removedText : "Nothing changed. Is the element you want this holder to control being controlled by another holder?";
  139. EditorGUILayout.HelpBox(finalText, (added != 0 || removed != 0) ? MessageType.Info : MessageType.Warning);
  140. }
  141. #endregion
  142. if (!zuiManager)
  143. {
  144. Debug.LogError("There's no ZUIManager script in the scene, you can have it by using the menu bar ZUI>Creation Window>Setup. Or by creating an empty GameObject and add ZUIManager script to it.");
  145. return;
  146. }
  147. #region Set As Default Menu Button
  148. if (zuiManager.CurActiveMenu != myMenu)
  149. {
  150. if (GUILayout.Button("Set As Default Menu", GUILayout.Height(30)))
  151. {
  152. Undo.RecordObject(zuiManager, "Set Default Menu");
  153. zuiManager.CurActiveMenu = myMenu;
  154. }
  155. }
  156. else
  157. {
  158. GUILayout.Space(7.5f);
  159. GUILayout.BeginHorizontal();
  160. GUILayout.FlexibleSpace();
  161. GUILayout.Label("Default Menu", GUILayout.Height(15));
  162. GUILayout.FlexibleSpace();
  163. GUILayout.EndHorizontal();
  164. GUILayout.Space(7.5f);
  165. }
  166. #endregion
  167. #region Check Menu Independant Elements
  168. if (myMenu.AnimatedElements != null)
  169. {
  170. for (int i = 0; i < myMenu.AnimatedElements.Count; i++)
  171. {
  172. if (myMenu.AnimatedElements[i] == null) continue;
  173. if (!myMenu.AnimatedElements[i].MenuDependent)
  174. {
  175. if (EditorUtility.DisplayDialog("Error", myMenu.AnimatedElements[i].gameObject.name + " is menu independant but is inside this menu's elements list.", "Remove it from the list", "Switch it to menu dependant"))
  176. {
  177. Undo.RecordObject(myMenu, "Removing from list");
  178. myMenu.AnimatedElements.RemoveAt(i);
  179. i--;
  180. continue;
  181. }
  182. else
  183. {
  184. Undo.RecordObject(myMenu, "Switch to menu dependant");
  185. myMenu.AnimatedElements[i].MenuDependent = true;
  186. }
  187. }
  188. if (myMenu.AnimatedElements[i].ControlledBy != myMenu)
  189. myMenu.AnimatedElements[i].ControlledBy = myMenu;
  190. }
  191. }
  192. if (myMenu.MultiMenusAnimatedElements != null)
  193. {
  194. for (int i = 0; i < myMenu.MultiMenusAnimatedElements.Count; i++)
  195. {
  196. if (myMenu.MultiMenusAnimatedElements[i] == null) continue;
  197. if (!myMenu.MultiMenusAnimatedElements[i].MenuDependent)
  198. {
  199. if (EditorUtility.DisplayDialog("Error", myMenu.MultiMenusAnimatedElements[i].gameObject.name + " is menu independant but is inside this Menu's elements list.", "Remove it from the list", "Switch it to menu dependant"))
  200. {
  201. Undo.RecordObject(myMenu, "Removing from list");
  202. myMenu.MultiMenusAnimatedElements.RemoveAt(i);
  203. i--;
  204. continue;
  205. }
  206. else
  207. {
  208. Undo.RecordObject(myMenu, "Switch to menu dependant");
  209. myMenu.MultiMenusAnimatedElements[i].MenuDependent = true;
  210. }
  211. }
  212. }
  213. }
  214. #endregion
  215. #endregion
  216. serializedObject.ApplyModifiedProperties();
  217. }
  218. List<UIElement> GetAnimatedElements(Transform holder)
  219. {
  220. List<UIElement> ue = new List<UIElement>();
  221. foreach (Transform c in holder)
  222. {
  223. UIElement cUE = c.GetComponent<UIElement>();
  224. if (cUE && cUE.MenuDependent && (cUE.ControlledBy == null || cUE.ControlledBy == (Menu)target))
  225. ue.Add(cUE);
  226. ue.AddRange(GetAnimatedElements(c));
  227. }
  228. return ue;
  229. }
  230. List<Menu> GetAllMenus()
  231. {
  232. List<Menu> menus = new List<Menu>();
  233. Canvas[] allCanvases = FindObjectsOfType<Canvas>();
  234. foreach (Canvas c in allCanvases)
  235. {
  236. menus.AddRange(GetMenusInCanvas(c.transform));
  237. }
  238. return menus;
  239. }
  240. List<Menu> GetMenusInCanvas(Transform canvas)
  241. {
  242. List<Menu> ts = new List<Menu>();
  243. foreach (Transform c in canvas)
  244. {
  245. Menu cMenu = c.GetComponent<Menu>();
  246. if (cMenu)
  247. ts.Add(cMenu);
  248. ts.AddRange(GetMenusInCanvas(c));
  249. }
  250. return ts;
  251. }
  252. }