从右向左滚动的跑马灯:
using System;
using DG.Tweening;
using TMPro;
using UnityEngine;
namespace Ricimi.TimeLimit
{
/// <summary>
/// 跑马灯。
/// </summary>
public class UITimeLimit_Marquee : MonoBehaviour
{
[Header("滚动速度"), SerializeField]
private float scrollDuration = 20f;
[SerializeField]
private Ease scrollEase = Ease.Linear;
[SerializeField]
private TextMeshProUGUI _scrollingText;
[SerializeField]
private RectTransform _scrollRectTransform;
private Tweener _tweener;
private int flag = -1;
private void OnEnable()
{
StartScroll();
}
private void OnDisable()
{
_tweener ?.Kill();
}
private void StartScroll()
{
_tweener ?.Kill();
float contentWidth = _scrollRectTransform.rect.width;
_scrollRectTransform.DOKill();
if (flag == -1)
{
_scrollRectTransform.anchoredPosition = new Vector2(contentWidth + 10, 0f);
_scrollRectTransform.DOAnchorPosX(0, scrollDuration)
.SetEase(scrollEase)
.OnComplete(() => {
flag = 0;
DelayStartScroll();
});
}
else if (flag == 0)
{
_scrollRectTransform.anchoredPosition = new Vector2(0, 0f);
_scrollRectTransform.DOAnchorPosX(-contentWidth, scrollDuration)
.SetEase(scrollEase)
.OnComplete(() => {
flag = 0;
DelayStartScroll();
});
}
}
private void DelayStartScroll()
{
Invoke(nameof(StartScroll), 3);
}
public void SetText(string text)
{
_scrollingText.text = text;
}
}
}