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.
57 lines
1.4 KiB
57 lines
1.4 KiB
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<DamageIndicatorText> OnComplete;
|
|
|
|
private void Awake()
|
|
{
|
|
mainCamera = Camera.main;
|
|
uiCamera = GameObject.FindGameObjectWithTag("UICamera").GetComponent<Camera>();
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|