123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- using UnityEngine;
- using System.Collections;
- using System;
- /// <summary>
- /// This component is responsible for representing a screen hierarchy. It will tell all its
- /// underlying UI elements to show/hide accordingly.
- /// </summary>
- public class ScreenBase : MonoBehaviour
- {
- /// <summary>
- /// Fire when all elements have finished animating the show
- /// </summary>
- public Action OnShowComplete;
- /// <summary>
- /// Fire when all elements have finished animating the hide
- /// </summary>
- 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<UIElementTransition>(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()
- {
-
- gameObject.SetActive (true);
- 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);
- 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);
- }
- }
|