FloatingValueUi.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Sirenix.OdinInspector;
  5. using TMPro;
  6. namespace KairoEngine.UI
  7. {
  8. public class FloatingValueUi : MonoBehaviour
  9. {
  10. public TextMeshProUGUI textMesh;
  11. public RectTransform rectTransform;
  12. public float lifetime = 1f;
  13. private Vector3 pos;
  14. private float counter = 0f;
  15. private float positionOffset;
  16. public void Setup(Vector3 position, string text, float positionOffset, float lifetime, float fontSize)
  17. {
  18. textMesh.text = text;
  19. pos = new Vector3(position.x, position.y + 2.5f, position.z);
  20. this.lifetime = lifetime;
  21. this.positionOffset = positionOffset;
  22. textMesh.fontSize = fontSize;
  23. }
  24. public void Update()
  25. {
  26. textMesh.enabled = true;
  27. if (pos != null && rectTransform != null)
  28. {
  29. pos.y += positionOffset * Time.deltaTime;
  30. Vector3 screenPos = Camera.main.WorldToScreenPoint(pos);
  31. rectTransform.position = screenPos;
  32. }
  33. counter += Time.deltaTime;
  34. if(counter > lifetime)
  35. {
  36. Destroy(gameObject);
  37. }
  38. if(counter > lifetime/2)
  39. {
  40. float a = textMesh.color.a - (textMesh.color.a * (lifetime * 2)) * Time.deltaTime;
  41. Color c = new Color(textMesh.color.r, textMesh.color.g, textMesh.color.b, a);
  42. textMesh.color = c;
  43. }
  44. }
  45. }
  46. }