using UnityEngine; using TMPro; using System; public class DamageIndicatorText : MonoBehaviour { static readonly int _stateNameHash = Animator.StringToHash("damage"); [SerializeField] TextMeshProUGUI text; [SerializeField] Animator animator; Camera mainCamera; Camera uiCamera; public bool IsShowing => gameObject.activeSelf; public event Action OnComplete; private void Awake() { mainCamera = Camera.main; uiCamera = GameObject.FindGameObjectWithTag("UICamera").GetComponent(); } private void Update() { if (animator.IsComplete(_stateNameHash)) Hide(); } public void SetText(string text, float fontSize, Color color) { this.text.color = color; this.text.fontSize = fontSize; this.text.text = text; } public void Show() { gameObject.SetActive(true); animator.Play(_stateNameHash); } public void Show(Vector2 position) { var screenPos = mainCamera.WorldToScreenPoint(position); RectTransformUtility.ScreenPointToLocalPointInRectangle(transform.parent as RectTransform, screenPos, uiCamera, out Vector2 localPos); transform.localPosition = localPos; gameObject.SetActive(true); animator.Play(_stateNameHash); } public void Hide() { gameObject.SetActive(false); } }