using UnityEngine; using TMPro; using System; public class DamageIndicatorText : MonoBehaviour { static readonly int _damageNameHash = Animator.StringToHash("damage"); [SerializeField] TextMeshProUGUI text; [SerializeField] Animator animator; static Camera _mainCamera; static Camera _uiCamera; public bool IsShowing => gameObject.activeInHierarchy; public event Action OnComplete; private void Awake() { if(_mainCamera == null) _mainCamera = Camera.main; if(_uiCamera == null) _uiCamera = GameObject.FindGameObjectWithTag(GameProperty.Instance.UICameraTag).GetComponent(); } private void Update() { if (animator.IsComplete(_damageNameHash)) { OnComplete?.Invoke(this); 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(_damageNameHash); } 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(_damageNameHash); } public void Hide() { gameObject.SetActive(false); } }