AddLifeTextScript.cs 2.1 KB

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