ComboTextBehavior.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. using JetBrains.Annotations;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using TMPro;
  5. using UnityEngine;
  6. public class ComboTextBehavior : MonoBehaviour
  7. {
  8. private static Vector2 SpawningRelativePosition
  9. {
  10. get
  11. {
  12. return new Vector2(0.0f, -160.0f);
  13. }
  14. }
  15. private static Vector2 FadeoutRelativePosition
  16. {
  17. get
  18. {
  19. return new Vector2(0.0f, -60.0f);
  20. }
  21. }
  22. private const float Lifetime = 0.8f;
  23. private const float FadeinThreshold = 0.1f;
  24. private float Age = 0.0f;
  25. // Start is called before the first frame update
  26. void Start()
  27. {
  28. gameObject.GetComponent<TextMeshProUGUI>().color = new Color(1.0f, 1.0f, 1.0f, 0.0f);
  29. gameObject.GetComponent<RectTransform>().anchorMin = new Vector2(0.5f, 1.0f);
  30. gameObject.GetComponent<RectTransform>().anchorMax = new Vector2(0.5f, 1.0f);
  31. }
  32. // Update is called once per frame
  33. void Update()
  34. {
  35. Age += Time.deltaTime;
  36. if (Age > Lifetime)
  37. {
  38. Destroy(gameObject);
  39. }
  40. else
  41. {
  42. float currentAlpha = (Age < FadeinThreshold) ? Mathf.InverseLerp(0.0f, FadeinThreshold, Age) : Mathf.InverseLerp(Lifetime, FadeinThreshold, Age);
  43. gameObject.GetComponent<TextMeshProUGUI>().color = new Color(1.0f, 1.0f, 1.0f, currentAlpha);
  44. float scale = Mathf.Lerp(1.0f, 1.5f, currentAlpha);
  45. gameObject.transform.localScale = new Vector3(scale, scale, scale);
  46. float currentPosAlpha = Mathf.InverseLerp(0.0f, Lifetime, Age);
  47. float PosY = Mathf.Lerp(SpawningRelativePosition.y, FadeoutRelativePosition.y, currentPosAlpha);
  48. gameObject.GetComponent<RectTransform>().anchoredPosition = new Vector2(SpawningRelativePosition.x, PosY);
  49. }
  50. }
  51. // set the number of combo for display
  52. public void SetComboNumber(int num)
  53. {
  54. gameObject.GetComponent<TextMeshProUGUI>().text = "Combo × " + num;
  55. }
  56. }