PopupEditor.cs 7.8 KB

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