UIElementTransition.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. using UnityEngine;
  2. using System.Collections;
  3. using System;
  4. /// <summary>
  5. /// This component is responsible for animating the showing and hiding of a UI element.
  6. /// It will also avoid the user from clicking while the element is being animated.
  7. /// This avoids double clicking by mistake.
  8. /// </summary>
  9. public class UIElementTransition : MonoBehaviour
  10. {
  11. /// <summary>
  12. /// Fired when the show transition is complete
  13. /// </summary>
  14. public Action OnShowComplete;
  15. /// <summary>
  16. /// Fired when the hide transition is complete
  17. /// </summary>
  18. public Action OnHideComplete;
  19. /// <summary>
  20. /// Fired when the hiding transition is going to start
  21. /// </summary>
  22. public Action OnHiding;
  23. /// <summary>
  24. /// The position when is transitioned out (or from where to start)
  25. /// </summary>
  26. public Vector3 hiddenPosition;
  27. public bool fadeInAndOut;
  28. Vector3 _originalPosition;
  29. Collider _collider;
  30. UIWidget[] _widgets;
  31. const float TRANSITION_IN_TIME = 0.5f;
  32. const float TRANSITION_OUT_TIME = 0.5f;
  33. void Start()
  34. {
  35. _originalPosition = transform.localPosition;
  36. transform.localPosition = hiddenPosition;
  37. _collider = GetComponent<Collider>();
  38. }
  39. public void Show()
  40. {
  41. //clear any iTweens from previous Hide, just in case
  42. iTween.Stop(gameObject);
  43. gameObject.SetActive(true);
  44. if (_collider != null)
  45. {
  46. _collider.enabled = false; //to avoid clicking while transitioning
  47. }
  48. iTween.MoveTo(gameObject, iTween.Hash(
  49. "islocal", true,
  50. "ignoretimescale", true,
  51. "time", TRANSITION_IN_TIME,
  52. "position", _originalPosition,
  53. "easetype", iTween.EaseType.easeOutBack,
  54. "oncompletetarget", gameObject,
  55. "oncomplete", "OniTweenShowFinished"));
  56. if (fadeInAndOut)
  57. {
  58. FadeIn();
  59. }
  60. }
  61. public void Hide()
  62. {
  63. if (OnHiding != null)
  64. {
  65. OnHiding();
  66. }
  67. if (_collider != null)
  68. {
  69. _collider.enabled = false; //to avoid clicking while transitioning
  70. }
  71. iTween.MoveTo(gameObject, iTween.Hash(
  72. "islocal", true,
  73. "ignoretimescale", true,
  74. "time", TRANSITION_OUT_TIME,
  75. "position", hiddenPosition,
  76. "easetype", iTween.EaseType.easeInBack,
  77. "oncompletetarget", gameObject,
  78. "oncomplete", "OniTweenHideFinished"));
  79. if (fadeInAndOut)
  80. {
  81. FadeOut();
  82. }
  83. }
  84. #region iTween Callbacks
  85. void OniTweenShowFinished()
  86. {
  87. if (_collider != null)
  88. {
  89. GetComponent<Collider>().enabled = true;
  90. }
  91. if (OnShowComplete != null)
  92. {
  93. OnShowComplete();
  94. }
  95. }
  96. void OniTweenHideFinished()
  97. {
  98. gameObject.SetActive(false);
  99. if (OnHideComplete != null)
  100. {
  101. OnHideComplete();
  102. }
  103. }
  104. void OniTweenUpdateAlpha(float newAlpha)
  105. {
  106. UpdateWidgetsAlpha(newAlpha);
  107. }
  108. #endregion
  109. void UpdateWidgetsAlpha(float newAlpha)
  110. {
  111. foreach (UIWidget w in _widgets)
  112. {
  113. w.alpha = newAlpha;
  114. }
  115. }
  116. void FadeIn()
  117. {
  118. if (_widgets == null)
  119. {
  120. _widgets = GetComponentsInChildren<UIWidget>();
  121. UpdateWidgetsAlpha(0);
  122. }
  123. if (_widgets.Length != 0)
  124. {
  125. iTween.ValueTo(gameObject, iTween.Hash(
  126. "from", _widgets[0].alpha,
  127. "to", 1,
  128. "time", TRANSITION_IN_TIME,
  129. "onupdate", "OniTweenUpdateAlpha",
  130. "onupdatetarget", gameObject));
  131. }
  132. }
  133. void FadeOut()
  134. {
  135. if (_widgets == null ||
  136. _widgets.Length == 0)
  137. {
  138. return;
  139. }
  140. iTween.ValueTo(gameObject, iTween.Hash(
  141. "from", _widgets[0].alpha,
  142. "to", 0,
  143. "time", TRANSITION_OUT_TIME,
  144. "onupdate", "OniTweenUpdateAlpha",
  145. "onupdatetarget", gameObject));
  146. }
  147. }