TextAnnouncement.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using UnityEngine;
  2. using System.Collections;
  3. using TMPro;
  4. [RequireComponent(typeof(PoolObject))]
  5. public class TextAnnouncement : MonoBehaviour
  6. {
  7. public float spinColorSpeed = 0;
  8. public Color startSpinColor = Color.red;
  9. public float introTime = 0.5f;
  10. public float stayTime = 0.5f;
  11. public float outroTime = 0.25f;
  12. public float upSpeed = 5;
  13. PoolObject _poolObject;
  14. TextMeshProUGUI _label;
  15. Vector3 _originalScale;
  16. HSBColor hsbColor;
  17. void Awake()
  18. {
  19. _originalScale = transform.localScale;
  20. _poolObject = GetComponent<PoolObject>();
  21. _label = GetComponentInChildren<TextMeshProUGUI>();
  22. iTween.Init(gameObject);
  23. }
  24. void Update()
  25. {
  26. transform.localPosition += new Vector3(0, upSpeed * Time.deltaTime, 0);
  27. }
  28. public void SetText(string text)
  29. {
  30. iTween.Stop(gameObject);
  31. _label.text = text;
  32. Vector3 startScale = _originalScale * 0.01f;
  33. transform.localScale = startScale; //otherwise NGUI would give errors
  34. this.ScaleTo(_originalScale, introTime, ease: MyTween.Ease.easeOutBack);
  35. this.ScaleTo(startScale, outroTime, introTime + stayTime, MyTween.Ease.easeInBack, () =>
  36. {
  37. _poolObject.Despawn();
  38. });
  39. if (spinColorSpeed != 0)
  40. {
  41. hsbColor = new HSBColor(startSpinColor);
  42. this.ValueTo(0, 1 * spinColorSpeed, introTime + stayTime + outroTime, (hue) =>
  43. {
  44. hsbColor.h = hue % 1f;
  45. _label.color = hsbColor.ToColor();
  46. });
  47. }
  48. }
  49. }