using UnityEngine; using TMPro; using DG.Tweening; using System; [RequireComponent(typeof(TextMeshProUGUI))] public class DamageIndicatorText : MonoBehaviour { Camera mainCamera; Camera uiCamera; TextMeshProUGUI text; DOTweenAnimation[] tweenAnimations; int playCount = 0; public bool IsShowing => gameObject.activeSelf; public event Action OnComplete; private void Awake() { mainCamera = Camera.main; uiCamera = GameObject.FindGameObjectWithTag("UICamera").GetComponent(); text = GetComponent(); tweenAnimations = GetComponents(); for (int i = 0; i < tweenAnimations.Length; i++) { tweenAnimations[i].hasOnComplete = true; tweenAnimations[i].onComplete.AddListener(OnAnimComplete); } } 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); playCount = tweenAnimations.Length; for (int i = 0; i < tweenAnimations.Length; i++) { tweenAnimations[i].DORestart(true); } } 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); playCount = tweenAnimations.Length; for (int i = 0; i < tweenAnimations.Length; i++) { tweenAnimations[i].DORestart(true); } } private void OnAnimComplete() { playCount--; if (playCount == 0) { OnComplete?.Invoke(this); Hide(); } } public void Hide() { gameObject.SetActive(false); } }