123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEditor;
- [CustomEditor(typeof(Popup))]
- public class PopupEditor : Editor {
- #region Inherited Properties
- SerializedProperty visible;
- SerializedProperty useSimpleActivation;
- SerializedProperty showingClip;
- SerializedProperty hidingClip;
- SerializedProperty onShow;
- SerializedProperty onHide;
- SerializedProperty ignoreEventsOnInitialization;
- #endregion
- SerializedProperty animatedElements;
- SerializedProperty deactivateWhileInvisible;
- SerializedProperty titleHolder;
- SerializedProperty bodyHolder;
- private bool updatedElements;
- private int added = 0;
- private int removed = 0;
- void OnEnable()
- {
- #region Inherited Properties
- visible = serializedObject.FindProperty("Visible");
- useSimpleActivation = serializedObject.FindProperty("UseSimpleActivation");
- showingClip = serializedObject.FindProperty("ShowingClip");
- hidingClip = serializedObject.FindProperty("HidingClip");
- onShow = serializedObject.FindProperty("OnShow");
- onHide = serializedObject.FindProperty("OnHide");
- ignoreEventsOnInitialization = serializedObject.FindProperty("IgnoreEventsOnInitialization");
- #endregion
- animatedElements = serializedObject.FindProperty("AnimatedElements");
- deactivateWhileInvisible = serializedObject.FindProperty("DeactivateWhileInvisible");
- titleHolder = serializedObject.FindProperty("TitleHolder");
- bodyHolder = serializedObject.FindProperty("BodyHolder");
- }
- public override void OnInspectorGUI()
- {
- Popup myPopup = target as Popup;
- ZUIManager zM = FindObjectOfType<ZUIManager>();
- #region Activate Button
- if (GUILayout.Button("Activate", GUILayout.Height(30)))
- {
- foreach (Popup p in zM.AllPopups)
- {
- if (p == null) continue;
- Undo.RecordObject(p.gameObject, "Activate Pop-up");
- if (p == myPopup)
- {
- p.gameObject.SetActive(true);
- }
- else
- {
- p.gameObject.SetActive(false);
- }
- }
- }
- #endregion
- #region User Interface
- EditorGUILayout.Space();
- EditorGUILayout.LabelField("Settings", EditorStyles.boldLabel);
- EditorGUILayout.LabelField("Is Visible?", visible.boolValue.ToString());
- EditorGUILayout.PropertyField(deactivateWhileInvisible);
- EditorGUILayout.Space();
- EditorGUILayout.LabelField("Menu Elements", EditorStyles.boldLabel);
- EditorGUILayout.PropertyField(animatedElements, true);
- EditorGUILayout.Space();
- EditorGUILayout.LabelField("Information", EditorStyles.boldLabel);
- EditorGUILayout.PropertyField(titleHolder);
- EditorGUILayout.PropertyField(bodyHolder);
- EditorGUILayout.Space();
- EditorGUILayout.LabelField("Switching", EditorStyles.boldLabel);
- if (useSimpleActivation.boolValue == true)
- {
- 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);
- }
- EditorGUILayout.PropertyField(useSimpleActivation);
- EditorGUILayout.Space();
- EditorGUILayout.LabelField("Sounds", EditorStyles.boldLabel);
- EditorGUILayout.PropertyField(showingClip);
- EditorGUILayout.PropertyField(hidingClip);
- EditorGUILayout.Space();
- EditorGUILayout.LabelField("Events", EditorStyles.boldLabel);
- EditorGUILayout.PropertyField(onShow);
- EditorGUILayout.PropertyField(onHide);
- EditorGUILayout.PropertyField(ignoreEventsOnInitialization, new GUIContent("Ignore On Initialization"));
- EditorGUILayout.Space();
- #endregion
- #region Tools
- EditorGUILayout.LabelField("Tools", EditorStyles.boldLabel);
- #region Update Animated Elements Button
- if (GUILayout.Button("Update Animated Elements", GUILayout.Height(30)))
- {
- //Save old elements list to make a check after updating.
- List<UIElement> oldElements = myPopup.AnimatedElements;
- added = 0;
- removed = 0;
- Undo.RecordObject(myPopup, "Update Animated Items");
- myPopup.AnimatedElements = GetAnimatedElements(myPopup.transform);
- UIElement popupUE = myPopup.GetComponent<UIElement>();
- if (popupUE)
- myPopup.AnimatedElements.Insert(0, popupUE);
-
- //Check which elements are added and which elements are removed.
- for (int i = 0; i < myPopup.AnimatedElements.Count; i++)
- {
- Undo.RecordObject(myPopup.AnimatedElements[i], "Update Animated Items");
- if (!oldElements.Contains(myPopup.AnimatedElements[i]))
- {
- added++;
- }
- }
- removed = oldElements.Count - myPopup.AnimatedElements.Count + added;
- updatedElements = true;
- }
- #endregion
- #region Elements Updated Info
- if (updatedElements)
- {
- string removedText = removed != 0 ? "Removed " + removed + " element" + (removed == 1 ? "." : "s.") : "";
- string addedText = added != 0 ? "Added " + added + " element" + (added == 1 ? ". " : "s. ") : "";
- string finalText = (added != 0 || removed != 0) ? addedText + removedText : "Nothing changed. Is the element you want this holder to control being controlled by another holder?";
- EditorGUILayout.HelpBox(finalText, (added != 0 || removed != 0) ? MessageType.Info : MessageType.Warning);
- }
- #endregion
- if (!zM)
- {
- 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.");
- return;
- }
- #region Check Menu Independant Elements
- if (myPopup.AnimatedElements != null)
- {
- for (int i = 0; i < myPopup.AnimatedElements.Count; i++)
- {
- if (myPopup.AnimatedElements[i] == null) continue;
- if (!myPopup.AnimatedElements[i].MenuDependent)
- {
- 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"))
- {
- Undo.RecordObject(myPopup, "Removing from list");
- myPopup.AnimatedElements.RemoveAt(i);
- i--;
- continue;
- }
- else
- {
- Undo.RecordObject(myPopup, "Switch to menu dependant");
- myPopup.AnimatedElements[i].MenuDependent = true;
- }
- }
- if (myPopup.AnimatedElements[i].ControlledBy != myPopup)
- myPopup.AnimatedElements[i].ControlledBy = myPopup;
- }
- }
- #endregion
- #endregion
- serializedObject.ApplyModifiedProperties();
- }
- List<UIElement> GetAnimatedElements(Transform holder)
- {
- List<UIElement> ue = new List<UIElement>();
- foreach (Transform c in holder)
- {
- UIElement cUE = c.GetComponent<UIElement>();
- if (cUE && cUE.MenuDependent && (cUE.ControlledBy == null || cUE.ControlledBy == (Popup)target))
- ue.Add(cUE);
- ue.AddRange(GetAnimatedElements(c));
- }
- return ue;
- }
- }
|