2026-05-22 13:31:04 +09:00
|
|
|
using System.Collections;
|
|
|
|
|
using TMPro;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
[RequireComponent(typeof(TMP_Text))]
|
|
|
|
|
public class MarqueeText : MonoBehaviour
|
|
|
|
|
{
|
2026-05-29 17:29:50 +09:00
|
|
|
public float speed = 14f;
|
|
|
|
|
public float pauseStart = 1.8f;
|
|
|
|
|
public float pauseEnd = 0.9f;
|
2026-05-22 13:31:04 +09:00
|
|
|
|
|
|
|
|
private TMP_Text _label;
|
|
|
|
|
private RectTransform _rect;
|
2026-05-29 17:29:50 +09:00
|
|
|
private Coroutine _scrollRoutine;
|
2026-05-22 13:31:04 +09:00
|
|
|
|
|
|
|
|
private void Awake()
|
|
|
|
|
{
|
|
|
|
|
_label = GetComponent<TMP_Text>();
|
|
|
|
|
_rect = GetComponent<RectTransform>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private IEnumerator Start()
|
|
|
|
|
{
|
2026-05-29 17:29:50 +09:00
|
|
|
yield return null;
|
|
|
|
|
Refresh();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnDisable()
|
|
|
|
|
{
|
|
|
|
|
StopScrolling();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Refresh()
|
|
|
|
|
{
|
|
|
|
|
if (!isActiveAndEnabled || _label == null || _rect == null || transform.parent == null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
StopScrolling();
|
|
|
|
|
SetX(0f);
|
2026-05-22 13:31:04 +09:00
|
|
|
|
|
|
|
|
_label.ForceMeshUpdate();
|
|
|
|
|
float textW = _label.preferredWidth;
|
|
|
|
|
float containerW = ((RectTransform)transform.parent).rect.width;
|
|
|
|
|
float dist = textW - containerW;
|
|
|
|
|
|
|
|
|
|
if (dist > 1f)
|
2026-05-29 17:29:50 +09:00
|
|
|
_scrollRoutine = StartCoroutine(ScrollLoop(dist));
|
2026-05-22 13:31:04 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private IEnumerator ScrollLoop(float dist)
|
|
|
|
|
{
|
|
|
|
|
while (true)
|
|
|
|
|
{
|
|
|
|
|
SetX(0f);
|
|
|
|
|
yield return new WaitForSeconds(pauseStart);
|
|
|
|
|
|
|
|
|
|
float x = 0f;
|
|
|
|
|
while (x > -dist)
|
|
|
|
|
{
|
|
|
|
|
x = Mathf.MoveTowards(x, -dist, speed * Time.deltaTime);
|
|
|
|
|
SetX(x);
|
|
|
|
|
yield return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
yield return new WaitForSeconds(pauseEnd);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SetX(float x) =>
|
|
|
|
|
_rect.anchoredPosition = new Vector2(x, _rect.anchoredPosition.y);
|
2026-05-29 17:29:50 +09:00
|
|
|
|
|
|
|
|
private void StopScrolling()
|
|
|
|
|
{
|
|
|
|
|
if (_scrollRoutine == null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
StopCoroutine(_scrollRoutine);
|
|
|
|
|
_scrollRoutine = null;
|
|
|
|
|
}
|
2026-05-22 13:31:04 +09:00
|
|
|
}
|