协程方法
协程方法
示例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.