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.
 
 
 
 
 
 

77 lines
2.0 KiB

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<DamageIndicatorText> OnComplete;
private void Awake()
{
mainCamera = Camera.main;
uiCamera = GameObject.FindGameObjectWithTag("UICamera").GetComponent<Camera>();
text = GetComponent<TextMeshProUGUI>();
tweenAnimations = GetComponents<DOTweenAnimation>();
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);
}
}