当前位置: 首页 > news >正文

平凉网站建设redu微信开发者文档小程序

平凉网站建设redu,微信开发者文档小程序,江苏中小企业建站价格,安装网站程序的流程一.携程概述 官方的解释是,携程允许你可以在多个帧中执行任务。在Unity中,携程是一个可以暂停并在后续帧中从暂停处继续执行的方法。 二.携程写法 下面示例使用携程和Update打印前5帧的时间间隔,展示了携程的基础写法 using System.Colle…

一.携程概述

官方的解释是,携程允许你可以在多个帧中执行任务。在Unity中,携程是一个可以暂停并在后续帧中从暂停处继续执行的方法。

二.携程写法

下面示例使用携程和Update打印前5帧的时间间隔,展示了携程的基础写法

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class demo2 : MonoBehaviour
{private int frameNum = 1;void Start(){StartCoroutine("coroutine");}public IEnumerator coroutine(){Debug.Log("coroutine frame1:" + Time.deltaTime);yield return null;Debug.Log("coroutine frame2:" + Time.deltaTime);yield return null;Debug.Log("coroutine frame3:" + Time.deltaTime);yield return null;Debug.Log("coroutine frame4:" + Time.deltaTime);yield return null;Debug.Log("coroutine frame5:" + Time.deltaTime);yield return null;}void Update(){if (frameNum <= 5){Debug.Log("------ Update:" + frameNum + "  " + Time.deltaTime);frameNum++;}}
}

  • 从打印结果来看,携程和Update一样会每帧调用一次
  • StartCoroutine用于开启携程
  • 返回值类型固定为IEnumerator
  • 返回值yield return null表示下一帧从此处之后开始执行,等同于yield return 一个数字

这里IEnumerator接口和yield关键字是C#的,不了解的可查看前两篇文章

三.Unity规定的携程返回值含义(标红的较为常用)

代码含义
yield return null;  yield retun x(x代表任意数字)下一帧再执行后续代码

yield return new WaitForSeconds(0.1f);

yield return new WaitForSecondsRealtime(0.1f); //不受timescale影响

等待固定时间执行后续代码
yield return FunctionName();函数执行完毕后执行后续代码
yield return Coroutine;协程执行完毕后执行后续代码
yield return new WaitForEndOfFrame();帧渲染完成后执行后续代码
yield return new WaitForFixedUpdate();物理帧更新后执行后续代码
yield break;结束该协程
yield return startCoroutine("funcName")等携程funName结束后执行后续代码

四.携程在事件函数中的执行顺序

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class demo1 : MonoBehaviour
{private bool logStart = true;void Start(){StartCoroutine("coroutine1");}void Update(){if (Input.GetKeyDown(KeyCode.S)){logStart = !logStart;Debug.Log(logStart);}if (logStart){Debug.Log("-------------------");Debug.Log("Update:" + Time.deltaTime);}}void LateUpdate(){if (logStart){Debug.Log("LateUpdate:" + Time.deltaTime);}}public IEnumerator coroutine1(){while (true){if (Input.GetKeyDown(KeyCode.S)){yield break;}else{Debug.Log("coroutine1:" + Time.deltaTime);yield return null;}}}
}

从打印结果来看,携程在Update之后,LateUpdate之前执行,官网的事件函数示意图也说明了这一点

五.携程的作用

1.替代Update处理一些耗时,按帧执行的任务,避免Update过于复杂

2.处理调用耗时API(比如切换场景)完成后在做什么的情况

六.携程可以传参可以嵌套

下面例子演示了crt1等待crt2结束后再执行后续,并给crt2传递参数

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class demo2 : MonoBehaviour
{void Start(){StartCoroutine("crt1");}public IEnumerator crt1(){Debug.Log("crt1 do task1");//携程2传参,等待携程2执行完成后,再执行后续代码yield return StartCoroutine("crt2", 3.0f);Debug.Log("crt1 do task2");}public IEnumerator crt2(float time){yield return new WaitForSeconds(time);Debug.Log("crt2 do task after " + time + "sec");yield return new WaitForSeconds(2);Debug.Log("crt2 finish");}void Update(){if (Input.GetKeyDown(KeyCode.S)){StopCoroutine("crt2");Debug.Log("crt2 is stopped");}if (Input.GetKeyDown(KeyCode.A)){StopAllCoroutines();Debug.Log("All crt stopped");}}
}

七.停止携程

StopCoroutine("funcName");  //停止携程funcName
StopAllCoroutines();        //停止脚本内所有携程
http://www.yayakq.cn/news/567072/

相关文章:

  • 文山知名网站建设公司tradekey外贸平台官网
  • 杭州网站运营十年乐云seo青少年思想道德建设网站
  • 装修公司做网销的网站外贸网站建设内容包括哪些
  • 海淀网站建设本溪访问一个网站的全过程
  • 设计做的网站哪些好营销推广策划方案范文
  • 太原网站开发上市公司年报查询网站
  • 做的最好的相亲网站做博物馆网站最重要
  • iis6.1配置网站3seo
  • 东莞网站定制网站页面设计主要包括
  • 需要做网站的企业电话河南省能源规划建设局网站
  • 中山品牌网站建设报价常州网站排名提升
  • wordpress网站的cdn怎么设置企业站网站
  • 英文网站建设easy百度关键词优化
  • 莆田有哪几家做网站设计的做视频网站视频的软件
  • 便宜做网站的公司百度app免费下载安装最新版
  • 手工网站做蛋糕盒子wordpress上传音乐文件
  • 公司网站建设代理一般做多久温州网站建设有限公司
  • 杂志社网站建设意义危险网站怎么做腾讯云认证
  • 新手做网站设计京网站建设
  • 中国联通网站备案系统郑州it培训机构
  • 宁波医院网站建设怎么开设自己的网站
  • 企业网站建设合同范本免费佛山茂名网站建设
  • 网站系统重要性免费做app的网站
  • 网站首页按钮图片青岛建韩国网站的公司
  • 网站的ftp账号密码注册电子邮箱号
  • 长春专业做网站的公司开发app和做网站
  • 网站建设 硬件投入旅游软件排行榜前十名
  • 网站可以改内链结构吗青岛网站建设报价
  • 儿童教育网站源码软文写作实训总结
  • 网站新建设请示建设充值网站多钱