123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- using UnityEngine;
- using System.Collections;
- using System;
- /// <summary>
- /// This component is responsible for animating the showing and hiding of a UI element.
- /// It will also avoid the user from clicking while the element is being animated.
- /// This avoids double clicking by mistake.
- /// </summary>
- public class UIElementTransition : MonoBehaviour
- {
- /// <summary>
- /// Fired when the show transition is complete
- /// </summary>
- public Action OnShowComplete;
- /// <summary>
- /// Fired when the hide transition is complete
- /// </summary>
- public Action OnHideComplete;
- /// <summary>
- /// Fired when the hiding transition is going to start
- /// </summary>
- public Action OnHiding;
- /// <summary>
- /// The position when is transitioned out (or from where to start)
- /// </summary>
- public Vector3 hiddenPosition;
- public bool fadeInAndOut;
- Vector3 _originalPosition;
- Collider _collider;
- UIWidget[] _widgets;
- const float TRANSITION_IN_TIME = 0.5f;
- const float TRANSITION_OUT_TIME = 0.5f;
- void Start()
- {
- _originalPosition = transform.localPosition;
- transform.localPosition = hiddenPosition;
- _collider = GetComponent<Collider>();
- }
- public void Show()
- {
- //clear any iTweens from previous Hide, just in case
- iTween.Stop(gameObject);
- gameObject.SetActive(true);
- if (_collider != null)
- {
- _collider.enabled = false; //to avoid clicking while transitioning
- }
- iTween.MoveTo(gameObject, iTween.Hash(
- "islocal", true,
- "ignoretimescale", true,
- "time", TRANSITION_IN_TIME,
- "position", _originalPosition,
- "easetype", iTween.EaseType.easeOutBack,
- "oncompletetarget", gameObject,
- "oncomplete", "OniTweenShowFinished"));
- if (fadeInAndOut)
- {
- FadeIn();
- }
- }
- public void Hide()
- {
- if (OnHiding != null)
- {
- OnHiding();
- }
- if (_collider != null)
- {
- _collider.enabled = false; //to avoid clicking while transitioning
- }
- iTween.MoveTo(gameObject, iTween.Hash(
- "islocal", true,
- "ignoretimescale", true,
- "time", TRANSITION_OUT_TIME,
- "position", hiddenPosition,
- "easetype", iTween.EaseType.easeInBack,
- "oncompletetarget", gameObject,
- "oncomplete", "OniTweenHideFinished"));
- if (fadeInAndOut)
- {
- FadeOut();
- }
- }
- #region iTween Callbacks
- void OniTweenShowFinished()
- {
- if (_collider != null)
- {
- GetComponent<Collider>().enabled = true;
- }
- if (OnShowComplete != null)
- {
- OnShowComplete();
- }
- }
- void OniTweenHideFinished()
- {
- gameObject.SetActive(false);
- if (OnHideComplete != null)
- {
- OnHideComplete();
- }
- }
- void OniTweenUpdateAlpha(float newAlpha)
- {
- UpdateWidgetsAlpha(newAlpha);
- }
- #endregion
- void UpdateWidgetsAlpha(float newAlpha)
- {
- foreach (UIWidget w in _widgets)
- {
- w.alpha = newAlpha;
- }
- }
- void FadeIn()
- {
- if (_widgets == null)
- {
- _widgets = GetComponentsInChildren<UIWidget>();
- UpdateWidgetsAlpha(0);
- }
- if (_widgets.Length != 0)
- {
- iTween.ValueTo(gameObject, iTween.Hash(
- "from", _widgets[0].alpha,
- "to", 1,
- "time", TRANSITION_IN_TIME,
- "onupdate", "OniTweenUpdateAlpha",
- "onupdatetarget", gameObject));
- }
- }
- void FadeOut()
- {
- if (_widgets == null ||
- _widgets.Length == 0)
- {
- return;
- }
- iTween.ValueTo(gameObject, iTween.Hash(
- "from", _widgets[0].alpha,
- "to", 0,
- "time", TRANSITION_OUT_TIME,
- "onupdate", "OniTweenUpdateAlpha",
- "onupdatetarget", gameObject));
- }
- }
|