public class test1 : MonoBehaviour
{
WaitForSecondsRealtime wfs;
void Start()
{
Debug.Log(">>>>>>>>>>>>>>0.0");
wfs = new WaitForSecondsRealtime(10);
StartCoroutine(DelayInvoke1());
StartCoroutine(DelayInvoke2());
StartCoroutine(DelayInvoke3());
}
// Update is called once per frame
void Update()
{
}
IEnumerator DelayInvoke1()
{
yield return wfs;
Debug.Log(">>>>>>>>>>>>>>0.1");
}
IEnumerator DelayInvoke2()
{
yield return wfs;
Debug.Log(">>>>>>>>>>>>>>0.2");
}
IEnumerator DelayInvoke3()
{
yield return new WaitForSecondsRealtime(5);
Debug.Log(">>>>>>>>>>>>>>0.3");
StartCoroutine(DelayInvoke2());
}
}
运行结果:
public class test2 : MonoBehaviour
{
WaitForSeconds wfs;
void Start()
{
Debug.Log(">>>>>>>>>>>>>>0.0");
wfs = new WaitForSeconds(10);
StartCoroutine(DelayInvoke1());
StartCoroutine(DelayInvoke2());
StartCoroutine(DelayInvoke3());
}
// Update is called once per frame
void Update()
{
}
IEnumerator DelayInvoke1()
{
yield return wfs;
Debug.Log(">>>>>>>>>>>>>>0.1");
}
IEnumerator DelayInvoke2()
{
yield return wfs;
Debug.Log(">>>>>>>>>>>>>>0.2");
}
IEnumerator DelayInvoke3()
{
yield return new WaitForSeconds(5);
Debug.Log(">>>>>>>>>>>>>>0.3");
StartCoroutine(DelayInvoke2());
}
}
运行结果:
结果分析:在用WaitForSeconds时,用老的对象时,等待的时间是开协程的时间+传入的参数。
但是用WaitForSecondsRealtime时,用老的对象,等待的时间是线性的,是在之前时间节点结束后才开始新的时间。