协程的基本使用和了解
using UnityEngine;
using System.Collections;
///
/// 协程的基本使用和了解
/// 在做游戏开发工作中,往往会有一些需求,需要延迟执行某个程序,这里我们就使用协程。
/// 注意协程不能在Update或者FixedUpdate使用。
///
public class Tests : MonoBehaviour {
void Update () {
if(Input.GetKeyDown(KeyCode.J))
{
//StartCoroutine(IETest());
StartCoroutine(IETest(100f));
}
if(Input.GetKeyDown(KeyCode.K))
{
//StartCoroutine(IETestTime());
StartCoroutine(IETestTime(3f));
}
}
///
/// 无参数,帧等待
///
///
IEnumerator IETest()
{
Debug.Log(“开启一道携程时,先执行这段代码”);
yield return 2;
Debug.Log(“等待2帧后,执行这段代码”);
}
///
/// 有参数,帧等待,参数的数量以及类型根据需求自定义添加
///
/// 多少帧
///
IEnumerator IETest(float frame)
{
Debug.Log(“开启一道携程时,先执行这段代码”);
yield return frame;
Debug.Log(“等待” + frame + “,执行这段代码”);
}
///
/// 无参数,时间等待
///
///
IEnumerator IETestTime()
{
Debug.Log(“开启一道携程时,先执行这段代码”);
yield return new WaitForSeconds(2f);
Debug.Log(“等待2秒后,执行这段代码”);
}
///
/// 有参数,时间等待。参数的数量以及类型根据需求自定义添加
///
/// 多少秒
///
IEnumerator IETestTime(float time)
{
Debug.Log(“开启一道携程时,先执行这段代码”);
yield return new WaitForSeconds(time);
Debug.Log(“等待” + time + “秒后,执行这段代码”);
}
}
希望大家可以多给建议,后续还会添加更多关于协程的问题,敬请期待!
协程的基本使用和了解
最新推荐文章于 2024-11-19 10:25:23 发布