栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 游戏开发 > 其他

Unity 协程 yield return的使用

其他 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Unity 协程 yield return的使用

public void Start()
        {
            //开启协程
            Coroutine testCoroutine = StartCoroutine(Test());
            //停止指定协程
            StopCoroutine(testCoroutine);
 
            //协程可以同时开启多个
            StartCoroutine("Test");
            //经实测,StopCoroutine("Test")只能停止StartCoroutine("Test")开启的协程,对StartCoroutine(Test())开启的协程无效
            StopCoroutine("Test");
 
            //停止本脚本内所有协程
            StopAllCoroutines();
        }
 
        IEnumerator Test()
        {
            //等待下一帧Update之后,继续执行后续代码
            yield return null;
 
            //等待在所有相机和GUI渲染之后,直到帧结束,继续执行后续代码
            yield return new WaitForEndOfFrame();
 
            //等待下一个FixedUpdate之后,继续执行后续代码
            yield return new WaitForFixedUpdate();
 
            //等待3秒之后,继续执行后续代码,使用缩放时间暂停协程执行达到给定的秒数
            yield return new WaitForSeconds(3.0f);
 
            //等待3秒之后,继续执行后续代码,使用未缩放的时间暂停协程执行达到给定的秒数
            yield return new WaitForSecondsRealtime(3.0f);
 
            //等待直到Func返回true,继续执行后续代码
            //yield return new WaitUntil(System.Func);
            yield return new WaitUntil(() => true);
 
            //等待直到Func返回false,继续执行后续代码
            //yield return new WaitWhile(System.Func);
            yield return new WaitWhile(() => false);
 
            //等待新开启的协程完成后,继续执行后续代码,可以利用这一点,实现递归
            yield return StartCoroutine(Test());
 
            //for循环
            for (int i = 0; i < 10; i++)
            {
                Debug.Log(i);
                yield return new WaitForSeconds(1);
            }
 
            //while循环,while(true):如果循环体内有yield return···语句,不会因为死循环卡死
            int j = 0;
            while (j < 10)
            {
                j++;
                Debug.Log(j);
                yield return new WaitForSeconds(1);
            }
 
            //终止协程
            yield break;
        }

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/901094.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号