SideMenuEditor.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEditor;
  5. [CustomEditor(typeof(SideMenu))]
  6. public class SideMenuEditor : Editor
  7. {
  8. #region Inherited Properties
  9. SerializedProperty visible;
  10. SerializedProperty useSimpleActivation;
  11. SerializedProperty showingClip;
  12. SerializedProperty hidingClip;
  13. SerializedProperty onShow;
  14. SerializedProperty onHide;
  15. SerializedProperty ignoreEventsOnInitialization;
  16. #endregion
  17. SerializedProperty animatedElements;
  18. SerializedProperty deactivateWhileInvisible;
  19. private bool updatedElements;
  20. private int added = 0;
  21. private int removed = 0;
  22. void OnEnable()
  23. {
  24. #region Inherited Properties
  25. visible = serializedObject.FindProperty("Visible");
  26. useSimpleActivation = serializedObject.FindProperty("UseSimpleActivation");
  27. showingClip = serializedObject.FindProperty("ShowingClip");
  28. hidingClip = serializedObject.FindProperty("HidingClip");
  29. onShow = serializedObject.FindProperty("OnShow");
  30. onHide = serializedObject.FindProperty("OnHide");
  31. ignoreEventsOnInitialization = serializedObject.FindProperty("IgnoreEventsOnInitialization");
  32. #endregion
  33. animatedElements = serializedObject.FindProperty("AnimatedElements");
  34. deactivateWhileInvisible = serializedObject.FindProperty("DeactivateWhileInvisible");
  35. }
  36. public override void OnInspectorGUI()
  37. {
  38. SideMenu mySideMenu = target as SideMenu;
  39. ZUIManager zM = FindObjectOfType<ZUIManager>();
  40. #region Activate Button
  41. if (GUILayout.Button("Activate", GUILayout.Height(30)))
  42. {
  43. foreach (SideMenu sM in zM.AllSideMenus)
  44. {
  45. if (sM == null) continue;
  46. Undo.RecordObject(sM.gameObject, "Activate Side-menu");
  47. if (sM == mySideMenu)
  48. {
  49. sM.gameObject.SetActive(true);
  50. }
  51. else
  52. {
  53. sM.gameObject.SetActive(false);
  54. }
  55. }
  56. }
  57. #endregion
  58. #region User Interface
  59. EditorGUILayout.Space();
  60. EditorGUILayout.LabelField("Settings", EditorStyles.boldLabel);
  61. EditorGUILayout.LabelField("Is Visible?", visible.boolValue.ToString());
  62. EditorGUILayout.PropertyField(deactivateWhileInvisible);
  63. EditorGUILayout.Space();
  64. EditorGUILayout.LabelField("Menu Elements", EditorStyles.boldLabel);
  65. EditorGUILayout.PropertyField(animatedElements, true);
  66. EditorGUILayout.Space();
  67. EditorGUILayout.LabelField("Switching", EditorStyles.boldLabel);
  68. if (useSimpleActivation.boolValue == true)
  69. {
  70. 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);
  71. }
  72. EditorGUILayout.PropertyField(useSimpleActivation);
  73. EditorGUILayout.Space();
  74. EditorGUILayout.LabelField("Sounds", EditorStyles.boldLabel);
  75. EditorGUILayout.PropertyField(showingClip);
  76. EditorGUILayout.PropertyField(hidingClip);
  77. EditorGUILayout.Space();
  78. EditorGUILayout.LabelField("Events", EditorStyles.boldLabel);
  79. EditorGUILayout.PropertyField(onShow);
  80. EditorGUILayout.PropertyField(onHide);
  81. EditorGUILayout.PropertyField(ignoreEventsOnInitialization, new GUIContent("Ignore On Initialization"));
  82. EditorGUILayout.Space();
  83. #endregion
  84. #region Tools
  85. EditorGUILayout.LabelField("Tools", EditorStyles.boldLabel);
  86. #region Update Animated Elements Button
  87. if (GUILayout.Button("Update Animated Elements", GUILayout.Height(30)))
  88. {
  89. //Save old elements list to make a check after updating.
  90. List<UIElement> oldElements = mySideMenu.AnimatedElements;
  91. added = 0;
  92. removed = 0;
  93. Undo.RecordObject(mySideMenu, "Update Animated Items");
  94. mySideMenu.AnimatedElements = GetAnimatedElements(mySideMenu.transform);
  95. UIElement popupUE = mySideMenu.GetComponent<UIElement>();
  96. if (popupUE)
  97. mySideMenu.AnimatedElements.Insert(0, popupUE);
  98. //Check which elements are added and which elements are removed.
  99. for (int i = 0; i < mySideMenu.AnimatedElements.Count; i++)
  100. {
  101. Undo.RecordObject(mySideMenu.AnimatedElements[i], "Update Animated Items");
  102. if (!oldElements.Contains(mySideMenu.AnimatedElements[i]))
  103. {
  104. added++;
  105. }
  106. }
  107. removed = oldElements.Count - mySideMenu.AnimatedElements.Count + added;
  108. updatedElements = true;
  109. }
  110. #endregion
  111. #region Elements Updated Info
  112. if (updatedElements)
  113. {
  114. string removedText = removed != 0 ? "Removed " + removed + " element" + (removed == 1 ? "." : "s.") : "";
  115. string addedText = added != 0 ? "Added " + added + " element" + (added == 1 ? ". " : "s. ") : "";
  116. string finalText = (added != 0 || removed != 0) ? addedText + removedText : "Nothing changed. Is the element you want this holder to control being controlled by another holder?";
  117. EditorGUILayout.HelpBox(finalText, (added != 0 || removed != 0) ? MessageType.Info : MessageType.Warning);
  118. }
  119. #endregion
  120. if (!zM)
  121. {
  122. 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.");
  123. return;
  124. }
  125. #region Check Menu Independant Elements
  126. if (mySideMenu.AnimatedElements != null)
  127. {
  128. for (int i = 0; i < mySideMenu.AnimatedElements.Count; i++)
  129. {
  130. if (mySideMenu.AnimatedElements[i] == null) continue;
  131. if (!mySideMenu.AnimatedElements[i].MenuDependent)
  132. {
  133. if (EditorUtility.DisplayDialog("Error", mySideMenu.AnimatedElements[i].gameObject.name + " is menu independant but is inside this Side-menu's elements list.", "Remove it from the list", "Switch it to menu dependant"))
  134. {
  135. Undo.RecordObject(mySideMenu, "Removing from list");
  136. mySideMenu.AnimatedElements.RemoveAt(i);
  137. i--;
  138. continue;
  139. }
  140. else
  141. {
  142. Undo.RecordObject(mySideMenu, "Switch to menu dependant");
  143. mySideMenu.AnimatedElements[i].MenuDependent = true;
  144. }
  145. }
  146. if (mySideMenu.AnimatedElements[i].ControlledBy != mySideMenu)
  147. mySideMenu.AnimatedElements[i].ControlledBy = mySideMenu;
  148. }
  149. }
  150. #endregion
  151. #endregion
  152. serializedObject.ApplyModifiedProperties();
  153. }
  154. List<UIElement> GetAnimatedElements(Transform holder)
  155. {
  156. List<UIElement> ue = new List<UIElement>();
  157. foreach (Transform c in holder)
  158. {
  159. UIElement cUE = c.GetComponent<UIElement>();
  160. if (cUE && cUE.MenuDependent && (cUE.ControlledBy == null || cUE.ControlledBy == (SideMenu)target))
  161. ue.Add(cUE);
  162. ue.AddRange(GetAnimatedElements(c));
  163. }
  164. return ue;
  165. }
  166. }