Files
2026-05-26 16:15:54 +08:00

70 lines
1.9 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
namespace game
{
[RequireComponent(typeof(TextMeshPro))]
public class FloatingText : MonoBehaviour
{
[Header("Type")]
public EventPinballBonusType FloatingTextType;
[Header("Movement Settings")]
public float moveSpeed = 1f;
public float fadeDuration = 1f;
[Header("Renderer")]
public int OrderInLayer = 3;
private TextMeshPro textMesh;
private float currentAlpha = 1f;
private float timer = 0f;
private Vector3 moveDirection = Vector3.up;
private void Awake()
{
textMesh = GetComponent<TextMeshPro>();
var renderer = GetComponent<Renderer>();
renderer.sortingOrder = OrderInLayer;
}
public void Initialize(string text, Vector3 position)
{
transform.position = position;
textMesh.text = text;
currentAlpha = 1f;
timer = 0f;
gameObject.SetActive(true);
}
private void Update()
{
// 移动
transform.position += moveDirection * moveSpeed * Time.deltaTime;
// 淡出
timer += Time.deltaTime;
if (timer >= fadeDuration)
{
if (FloatingTextType == EventPinballBonusType.TypeScore)
{
FloatingTextPool.Instance.ReturnToScorePool(this);
}
else if (FloatingTextType == EventPinballBonusType.TypeMultiple)
{
FloatingTextPool.Instance.ReturnToMultiplePool(this);
}
return;
}
currentAlpha = Mathf.Lerp(1f, 0f, timer / fadeDuration);
var color = textMesh.color;
color.a = currentAlpha;
textMesh.color = color;
}
}
}