LetterAnimator.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using System.Collections;
  4. namespace CompassNavigatorPro {
  5. public delegate void OnAnimationEndDelegate(int poolIndex);
  6. public class LetterAnimator : MonoBehaviour {
  7. public float startTime, revealDuration, startFadeTime, fadeDuration;
  8. public Text text, textShadow;
  9. public int poolIndex;
  10. public OnAnimationEndDelegate OnAnimationEnds;
  11. public bool used;
  12. Vector3 localScale;
  13. public void Play () {
  14. localScale = text.transform.localScale;
  15. Update ();
  16. }
  17. // Update is called once per frame
  18. void Update () {
  19. float now = Time.time;
  20. float elapsed = now - startTime;
  21. if (elapsed < revealDuration) { // revealing
  22. float t = Mathf.Clamp01 (elapsed / revealDuration);
  23. UpdateTextScale (t);
  24. UpdateTextAlpha (t);
  25. } else if (now < startFadeTime) {
  26. UpdateTextScale (1.0f);
  27. UpdateTextAlpha (1.0f);
  28. } else if (now < startFadeTime + fadeDuration) {
  29. float t = Mathf.Clamp01 (1.0f - (now - startFadeTime) / fadeDuration);
  30. UpdateTextAlpha (t);
  31. } else {
  32. OnAnimationEnds(poolIndex);
  33. enabled = false;
  34. }
  35. }
  36. void UpdateTextScale (float t) {
  37. text.transform.localScale = localScale * t;
  38. textShadow.transform.localScale = localScale * t;
  39. }
  40. void UpdateTextAlpha (float t) {
  41. text.color = new Color (text.color.r, text.color.g, text.color.b, t);
  42. textShadow.color = new Color (0, 0, 0, t);
  43. }
  44. }
  45. }