FloatingValueUi.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Sirenix.OdinInspector;
  5. using TMPro;
  6. namespace KairoEngine.CharacterSystem
  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. public void Setup(Vector3 position, string text, float lifetime = 0.7f)
  16. {
  17. textMesh.text = text;
  18. pos = new Vector3(position.x, position.y + 2.5f, position.z);
  19. this.lifetime = lifetime;
  20. }
  21. public void Update()
  22. {
  23. textMesh.enabled = true;
  24. if (pos != null && rectTransform != null)
  25. {
  26. pos.y += 0.5f * Time.deltaTime;
  27. Vector3 screenPos = Camera.main.WorldToScreenPoint(pos);
  28. rectTransform.position = screenPos;
  29. }
  30. counter += Time.deltaTime;
  31. if(counter > lifetime)
  32. {
  33. Destroy(gameObject);
  34. }
  35. float a = textMesh.color.a - (textMesh.color.a * lifetime) * Time.deltaTime;
  36. Color c = new Color(textMesh.color.r, textMesh.color.g, textMesh.color.b, a);
  37. textMesh.color = c;
  38. }
  39. }
  40. }