56 lines
1.4 KiB
C#
56 lines
1.4 KiB
C#
|
|
using System.Collections;
|
||
|
|
using TMPro;
|
||
|
|
using UnityEngine;
|
||
|
|
|
||
|
|
[RequireComponent(typeof(TMP_Text))]
|
||
|
|
public class MarqueeText : MonoBehaviour
|
||
|
|
{
|
||
|
|
public float speed = 35f;
|
||
|
|
public float pauseStart = 1.5f;
|
||
|
|
public float pauseEnd = 0.6f;
|
||
|
|
|
||
|
|
private TMP_Text _label;
|
||
|
|
private RectTransform _rect;
|
||
|
|
|
||
|
|
private void Awake()
|
||
|
|
{
|
||
|
|
_label = GetComponent<TMP_Text>();
|
||
|
|
_rect = GetComponent<RectTransform>();
|
||
|
|
}
|
||
|
|
|
||
|
|
private IEnumerator Start()
|
||
|
|
{
|
||
|
|
yield return null; // layout 완료 후 실행
|
||
|
|
|
||
|
|
_label.ForceMeshUpdate();
|
||
|
|
float textW = _label.preferredWidth;
|
||
|
|
float containerW = ((RectTransform)transform.parent).rect.width;
|
||
|
|
float dist = textW - containerW;
|
||
|
|
|
||
|
|
if (dist > 1f)
|
||
|
|
StartCoroutine(ScrollLoop(dist));
|
||
|
|
}
|
||
|
|
|
||
|
|
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);
|
||
|
|
}
|