ScreenBase.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. try
  42. {
  43. if (gameObject != null)
  44. {
  45. gameObject.SetActive(true);
  46. }
  47. }
  48. catch (Exception)
  49. {
  50. throw;
  51. }
  52. if (_isStarted == false)
  53. {
  54. return;
  55. }
  56. SendMessage("ShowMsg", SendMessageOptions.DontRequireReceiver); //other components might want to know about this event
  57. foreach (UIElementTransition e in _elements)
  58. {
  59. e.Show();
  60. }
  61. StartCoroutine(OnShowTransitionsCompleteCoroutine());
  62. }
  63. public virtual void Hide()
  64. {
  65. SendMessage("HideMsg",SendMessageOptions.DontRequireReceiver); //other components might want to know about this event
  66. //Debug.Log(_elements.Length);
  67. if (_elements!=null)
  68. foreach (UIElementTransition e in _elements)
  69. {
  70. e.Hide();
  71. }
  72. if(this.gameObject.activeSelf)
  73. {
  74. StartCoroutine(OnHideTransitionsCompleteCoroutine());
  75. }
  76. }
  77. IEnumerator OnShowTransitionsCompleteCoroutine()
  78. {
  79. yield return new WaitForSeconds(MAX_TRANSITION_TIME);
  80. if (OnShowComplete != null)
  81. {
  82. OnShowComplete();
  83. }
  84. }
  85. IEnumerator OnHideTransitionsCompleteCoroutine()
  86. {
  87. yield return new WaitForSeconds(MAX_TRANSITION_TIME);
  88. if (OnHideComplete != null)
  89. {
  90. OnHideComplete();
  91. }
  92. //Stop any iTweens just in case they were not finished.
  93. //If we don't remove the tweens, it could cause some problems when re-enabling the screen
  94. //where they would continue and conflict with new tweens
  95. foreach (UIElementTransition e in _elements)
  96. {
  97. iTween.Stop(e.gameObject);
  98. }
  99. gameObject.SetActive(false);
  100. }
  101. }