using UnityEngine; using TMPro; using System; public class DamageIndicatorText : MonoBehaviour { static readonly int _stateNameHash = Animator.StringToHash("damage"); [SerializeField] TextMeshProUGUI text; [SerializeField] Animator animator; static Camera _mainCamera; static Camera _uiCamera; public bool IsShowing => text.enabled; 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(_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() { text.enabled = 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; text.enabled = true; animator.Play(_stateNameHash); } public void Hide() { text.enabled = false; } }