UIElementsGroupEditor.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEditor;
  5. [CustomEditor(typeof(UIElementsGroup))]
  6. public class UIElementsGroupEditor : 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 prewarm;
  18. SerializedProperty animatedElements;
  19. SerializedProperty deactivateWhileInvisible;
  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. prewarm = serializedObject.FindProperty("Prewarm");
  35. animatedElements = serializedObject.FindProperty("AnimatedElements");
  36. deactivateWhileInvisible = serializedObject.FindProperty("DeactivateWhileInvisible");
  37. }
  38. public override void OnInspectorGUI()
  39. {
  40. UIElementsGroup myElementsGroup = target as UIElementsGroup;
  41. #region User Interface
  42. EditorGUILayout.Space();
  43. EditorGUILayout.LabelField("Settings", EditorStyles.boldLabel);
  44. EditorGUILayout.PropertyField(visible);
  45. EditorGUILayout.PropertyField(prewarm);
  46. EditorGUILayout.PropertyField(deactivateWhileInvisible);
  47. EditorGUILayout.Space();
  48. EditorGUILayout.LabelField("Menu Elements", EditorStyles.boldLabel);
  49. EditorGUILayout.PropertyField(animatedElements, true);
  50. EditorGUILayout.Space();
  51. EditorGUILayout.LabelField("Switching", EditorStyles.boldLabel);
  52. if (useSimpleActivation.boolValue == true)
  53. {
  54. 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);
  55. }
  56. EditorGUILayout.PropertyField(useSimpleActivation);
  57. EditorGUILayout.Space();
  58. EditorGUILayout.LabelField("Sounds", EditorStyles.boldLabel);
  59. EditorGUILayout.PropertyField(showingClip);
  60. EditorGUILayout.PropertyField(hidingClip);
  61. EditorGUILayout.Space();
  62. EditorGUILayout.LabelField("Events", EditorStyles.boldLabel);
  63. EditorGUILayout.PropertyField(onShow);
  64. EditorGUILayout.PropertyField(onHide);
  65. EditorGUILayout.PropertyField(ignoreEventsOnInitialization, new GUIContent("Ignore On Initialization"));
  66. EditorGUILayout.Space();
  67. #endregion
  68. #region Tools
  69. EditorGUILayout.LabelField("Tools", EditorStyles.boldLabel);
  70. #region Update Animated Elements Button
  71. if (GUILayout.Button("Update Animated Elements", GUILayout.Height(30)))
  72. {
  73. //Save old elements list to make a check after updating.
  74. List<UIElement> oldElements = myElementsGroup.AnimatedElements;
  75. added = 0;
  76. removed = 0;
  77. Undo.RecordObject(myElementsGroup, "Update Animated Items");
  78. myElementsGroup.AnimatedElements = GetAnimatedElements(myElementsGroup.transform);
  79. UIElement freeMenuUE = myElementsGroup.GetComponent<UIElement>();
  80. if (freeMenuUE)
  81. myElementsGroup.AnimatedElements.Insert(0, freeMenuUE);
  82. //Check which elements are added and which elements are removed.
  83. for (int i = 0; i < myElementsGroup.AnimatedElements.Count; i++)
  84. {
  85. Undo.RecordObject(myElementsGroup.AnimatedElements[i], "Update Animated Items");
  86. if (!oldElements.Contains(myElementsGroup.AnimatedElements[i]))
  87. {
  88. added++;
  89. }
  90. }
  91. removed = oldElements.Count - myElementsGroup.AnimatedElements.Count + added;
  92. updatedElements = true;
  93. }
  94. #endregion
  95. #region Elements Updated Info
  96. if (updatedElements)
  97. {
  98. string removedText = removed != 0 ? "Removed " + removed + " element" + (removed == 1 ? "." : "s.") : "";
  99. string addedText = added != 0 ? "Added " + added + " element" + (added == 1 ? ". " : "s. ") : "";
  100. string finalText = (added != 0 || removed != 0) ? addedText + removedText : "Nothing changed. Is the element you want this holder to control being controlled by another holder?";
  101. EditorGUILayout.HelpBox(finalText, (added != 0 || removed != 0) ? MessageType.Info : MessageType.Warning);
  102. }
  103. #endregion
  104. #region Check Menu Independant Elements
  105. if (myElementsGroup.AnimatedElements != null)
  106. {
  107. for (int i = 0; i < myElementsGroup.AnimatedElements.Count; i++)
  108. {
  109. if (myElementsGroup.AnimatedElements[i] == null) continue;
  110. if (!myElementsGroup.AnimatedElements[i].MenuDependent)
  111. {
  112. if (EditorUtility.DisplayDialog("Error", myElementsGroup.AnimatedElements[i].gameObject.name + " is menu independant but is inside this Element Group's elements list.", "Remove it from the list", "Switch it to menu dependant"))
  113. {
  114. Undo.RecordObject(myElementsGroup, "Removing from list");
  115. myElementsGroup.AnimatedElements.RemoveAt(i);
  116. i--;
  117. continue;
  118. }
  119. else
  120. {
  121. Undo.RecordObject(myElementsGroup, "Switch to menu dependant");
  122. myElementsGroup.AnimatedElements[i].MenuDependent = true;
  123. }
  124. }
  125. if (myElementsGroup.AnimatedElements[i].ControlledBy != myElementsGroup)
  126. myElementsGroup.AnimatedElements[i].ControlledBy = myElementsGroup;
  127. }
  128. }
  129. #endregion
  130. #endregion
  131. serializedObject.ApplyModifiedProperties();
  132. }
  133. List<UIElement> GetAnimatedElements(Transform holder)
  134. {
  135. List<UIElement> ue = new List<UIElement>();
  136. foreach (Transform c in holder)
  137. {
  138. UIElement cUE = c.GetComponent<UIElement>();
  139. if (cUE && cUE.MenuDependent && (cUE.ControlledBy == null || cUE.ControlledBy == (UIElementsGroup)target))
  140. ue.Add(cUE);
  141. ue.AddRange(GetAnimatedElements(c));
  142. }
  143. return ue;
  144. }
  145. }