using UnityEngine; using System.Collections; using System; /// /// This component is responsible for representing a screen hierarchy. It will tell all its /// underlying UI elements to show/hide accordingly. /// public class ScreenBase : MonoBehaviour { /// /// Fire when all elements have finished animating the show /// public Action OnShowComplete; /// /// Fire when all elements have finished animating the hide /// public Action OnHideComplete; public bool showOnStart = true; UIElementTransition[] _elements; const float MAX_TRANSITION_TIME = 0.5f; bool _isStarted; #region Unity Callbacks protected virtual void Awake() { bool includeInactive = true; _elements = GetComponentsInChildren(includeInactive); } protected virtual IEnumerator Start() { _isStarted = true; //Show the menu in the next frame, otherwise the anchors wouldn't have been set up yet yield return null; if (showOnStart) { Show(); } } #endregion public virtual void Show() { try { if (gameObject != null) { gameObject.SetActive(true); } } catch (Exception) { throw; } if (_isStarted == false) { return; } SendMessage("ShowMsg", SendMessageOptions.DontRequireReceiver); //other components might want to know about this event foreach (UIElementTransition e in _elements) { e.Show(); } StartCoroutine(OnShowTransitionsCompleteCoroutine()); } public virtual void Hide() { SendMessage("HideMsg",SendMessageOptions.DontRequireReceiver); //other components might want to know about this event //Debug.Log(_elements.Length); if (_elements!=null) foreach (UIElementTransition e in _elements) { e.Hide(); } if(this.gameObject.activeSelf) { StartCoroutine(OnHideTransitionsCompleteCoroutine()); } } IEnumerator OnShowTransitionsCompleteCoroutine() { yield return new WaitForSeconds(MAX_TRANSITION_TIME); if (OnShowComplete != null) { OnShowComplete(); } } IEnumerator OnHideTransitionsCompleteCoroutine() { yield return new WaitForSeconds(MAX_TRANSITION_TIME); if (OnHideComplete != null) { OnHideComplete(); } //Stop any iTweens just in case they were not finished. //If we don't remove the tweens, it could cause some problems when re-enabling the screen //where they would continue and conflict with new tweens foreach (UIElementTransition e in _elements) { iTween.Stop(e.gameObject); } gameObject.SetActive(false); } }