using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
class StateItem
{
private string Text;
public StateItem Next;
public StateItem(string text)
{
Text = text;
}
public void Play(MonoBehaviour mono, float delay)
{
Timer.Schedule(mono, delay, () =>
{
mono.gameObject.GetComponent<Text>().text = Text;
if (Next != null)
{
Next.Play(mono, delay);
}
});
}
}
class StateChain
{
public StateItem Head;
private StateItem End;
public void Add(StateItem item)
{
if(item == null)
{
return;
}
if(Head == null)
{
Head = item;
}
else
{
End.Next = item;
}
End = item;
End.Next = Head;
}
public void Play(MonoBehaviour mono, float delay)
{
if(Head != null)
{
Head.Play(mono, delay);
}
}
}
public class Loading : MonoBehaviour
{
void Start()
{
StateChain chain = new StateChain();
chain.Add(new StateItem("Loading."));
chain.Add(new StateItem("Loading.."));
chain.Add(new StateItem("Loading..."));
chain.Add(new StateItem("Loading"));
chain.Play(this, 0.5f);
}
}
把这个脚本挂在包含Text组件的UI上即可
另:延时写法
public class Timer{
private static MonoBehaviour behaviour;
public delegate void Task();
public static void Schedule(MonoBehaviour _behaviour, float delay, Task task)
{
behaviour = _behaviour;
behaviour.StartCoroutine(DoTask(task, delay));
}
private static IEnumerator DoTask(Task task, float delay)
{
yield return new WaitForSeconds(delay);
task();
}
}