ScreenBase.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using UnityEngine;
  2. using System.Collections;
  3. using System;
  4. /// <summary>
  5. /// This component is responsible for representing a screen hierarchy. It will tell all its
  6. /// underlying UI elements to show/hide accordingly.
  7. /// </summary>
  8. public class ScreenBase : MonoBehaviour
  9. {
  10. /// <summary>
  11. /// Fire when all elements have finished animating the show
  12. /// </summary>
  13. public Action OnShowComplete;
  14. /// <summary>
  15. /// Fire when all elements have finished animating the hide
  16. /// </summary>
  17. public Action OnHideComplete;
  18. public bool showOnStart = true;
  19. UIElementTransition[] _elements;
  20. const float MAX_TRANSITION_TIME = 0.5f;
  21. bool _isStarted;
  22. #region Unity Callbacks
  23. protected virtual void Awake()
  24. {
  25. bool includeInactive = true;
  26. _elements = GetComponentsInChildren<UIElementTransition>(includeInactive);
  27. }
  28. protected virtual IEnumerator Start()
  29. {
  30. _isStarted = true;
  31. //Show the menu in the next frame, otherwise the anchors wouldn't have been set up yet
  32. yield return null;
  33. if (showOnStart)
  34. {
  35. Show();
  36. }
  37. }
  38. #endregion
  39. public virtual void Show()
  40. {
  41. gameObject.SetActive (true);
  42. if (_isStarted == false)
  43. {
  44. return;
  45. }
  46. SendMessage("ShowMsg", SendMessageOptions.DontRequireReceiver); //other components might want to know about this event
  47. foreach (UIElementTransition e in _elements)
  48. {
  49. e.Show();
  50. }
  51. StartCoroutine(OnShowTransitionsCompleteCoroutine());
  52. }
  53. public virtual void Hide()
  54. {
  55. SendMessage("HideMsg",SendMessageOptions.DontRequireReceiver); //other components might want to know about this event
  56. //Debug.Log(_elements.Length);
  57. foreach (UIElementTransition e in _elements)
  58. {
  59. e.Hide();
  60. }
  61. if(this.gameObject.activeSelf)
  62. {
  63. StartCoroutine(OnHideTransitionsCompleteCoroutine());
  64. }
  65. }
  66. IEnumerator OnShowTransitionsCompleteCoroutine()
  67. {
  68. yield return new WaitForSeconds(MAX_TRANSITION_TIME);
  69. if (OnShowComplete != null)
  70. {
  71. OnShowComplete();
  72. }
  73. }
  74. IEnumerator OnHideTransitionsCompleteCoroutine()
  75. {
  76. yield return new WaitForSeconds(MAX_TRANSITION_TIME);
  77. if (OnHideComplete != null)
  78. {
  79. OnHideComplete();
  80. }
  81. //Stop any iTweens just in case they were not finished.
  82. //If we don't remove the tweens, it could cause some problems when re-enabling the screen
  83. //where they would continue and conflict with new tweens
  84. foreach (UIElementTransition e in _elements)
  85. {
  86. iTween.Stop(e.gameObject);
  87. }
  88. gameObject.SetActive(false);
  89. }
  90. }