You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

63 lines
1.6 KiB

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<DamageIndicatorText> OnComplete;
private void Awake()
{
if(_mainCamera == null)
_mainCamera = Camera.main;
if(_uiCamera == null)
_uiCamera = GameObject.FindGameObjectWithTag(GameProperty.Instance.UICameraTag).GetComponent<Camera>();
}
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);
}
}