119 lines
3.8 KiB
C#
119 lines
3.8 KiB
C#
using System.Collections.Generic;
|
|
using GazeLab.Combat;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
namespace GazeLab.UI
|
|
{
|
|
public sealed class FloatingCombatText : MonoBehaviour
|
|
{
|
|
private static readonly Queue<FloatingCombatText> textPool = new Queue<FloatingCombatText>();
|
|
private static readonly List<FloatingCombatText> activeTexts = new List<FloatingCombatText>();
|
|
|
|
private RectTransform rectTransform;
|
|
private Text uiText;
|
|
private Outline outline;
|
|
private Vector3 worldOrigin;
|
|
private float elapsed;
|
|
private float duration = 0.95f;
|
|
private Color baseColor;
|
|
|
|
public static void SpawnOnCanvas(RectTransform canvasRoot, Font font, Vector3 worldPos, string message, Color color, float scale = 1f)
|
|
{
|
|
if (canvasRoot == null || string.IsNullOrEmpty(message)) return;
|
|
|
|
FloatingCombatText ft = null;
|
|
while (textPool.Count > 0 && ft == null)
|
|
{
|
|
ft = textPool.Dequeue();
|
|
}
|
|
|
|
if (ft == null)
|
|
{
|
|
GameObject go = new GameObject("FloatingText", typeof(RectTransform), typeof(Text), typeof(Outline));
|
|
ft = go.AddComponent<FloatingCombatText>();
|
|
}
|
|
|
|
ft.transform.SetParent(canvasRoot, false);
|
|
ft.gameObject.SetActive(true);
|
|
ft.Initialize(font, worldPos, message, color, scale);
|
|
activeTexts.Add(ft);
|
|
}
|
|
|
|
public static void RecycleAllActive()
|
|
{
|
|
var arr = activeTexts.ToArray();
|
|
for (int i = 0; i < arr.Length; i++)
|
|
{
|
|
if (arr[i] != null)
|
|
{
|
|
arr[i].Recycle();
|
|
}
|
|
}
|
|
activeTexts.Clear();
|
|
}
|
|
|
|
public void Recycle()
|
|
{
|
|
if (!gameObject.activeSelf) return;
|
|
activeTexts.Remove(this);
|
|
gameObject.SetActive(false);
|
|
textPool.Enqueue(this);
|
|
}
|
|
|
|
private void Initialize(Font font, Vector3 wPos, string msg, Color col, float scale)
|
|
{
|
|
elapsed = 0f;
|
|
if (rectTransform == null) rectTransform = GetComponent<RectTransform>();
|
|
rectTransform.sizeDelta = new Vector2(360f, 50f);
|
|
rectTransform.localScale = Vector3.one * Mathf.Clamp(scale, 0.65f, 1.6f);
|
|
|
|
if (uiText == null) uiText = GetComponent<Text>();
|
|
uiText.font = font != null ? font : Resources.GetBuiltinResource<Font>("Arial.ttf");
|
|
uiText.text = msg;
|
|
uiText.fontSize = 20;
|
|
uiText.fontStyle = FontStyle.Bold;
|
|
uiText.alignment = TextAnchor.MiddleCenter;
|
|
uiText.color = col;
|
|
uiText.raycastTarget = false;
|
|
|
|
if (outline == null) outline = GetComponent<Outline>();
|
|
outline.effectColor = new Color(0f, 0f, 0f, 0.85f);
|
|
outline.effectDistance = new Vector2(1.5f, -1.5f);
|
|
|
|
worldOrigin = wPos;
|
|
baseColor = col;
|
|
UpdatePosition(0f);
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
elapsed += Time.deltaTime;
|
|
float t = Mathf.Clamp01(elapsed / duration);
|
|
UpdatePosition(t);
|
|
|
|
if (elapsed >= duration)
|
|
{
|
|
Recycle();
|
|
}
|
|
}
|
|
|
|
private void UpdatePosition(float t)
|
|
{
|
|
if (Core.CameraViewportService.MainCamera == null || rectTransform == null) return;
|
|
|
|
Vector3 floatedWorld = worldOrigin + Vector3.up * (t * 0.75f);
|
|
Vector3 screenPt = Core.CameraViewportService.WorldToScreenPoint(floatedWorld);
|
|
rectTransform.position = screenPt;
|
|
|
|
if (uiText != null)
|
|
{
|
|
Color c = baseColor;
|
|
c.a = Mathf.Clamp01(1f - t * t);
|
|
uiText.color = c;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|