示例1
using System.Collections;
using UnityEngine;
public class Test : MonoBehaviour
{
private void Start()
{
StartCoroutine(MyCoroutine(2, "success"));
}
IEnumerator MyCoroutine(int i, string str)
{
Debug.Log(i);
yield return new WaitForSeconds(5f);
Debug.Log(str); // 打印i的5s后执行
yield return new WaitForSeconds(5f);
Debug.Log("123"); // 打印str的5s后执行
}
}
该段代码的功能是间隔一定时间打印内容。
示例2
using System.Collections;
using UnityEngine;
public class Test : MonoBehaviour
{
private void Start()
{
StartCoroutine(MyCoroutine(2));
}
IEnumerator MyCoroutine(int i)
{
while (true)
{
yield return new WaitForSeconds(1);
Debug.Log(i);
}
}
}
该段代码的作用是每过1秒打印一次数字2.
标签:协程,Log,WaitForSeconds,MyCoroutine,new,Debug,using,方法 From: https://www.cnblogs.com/gatran/p/17206086.html