UIElementTransition.cs 3.4 KB

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