博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Coroutine 切换调用的例子
阅读量:6924 次
发布时间:2019-06-27

本文共 1917 字,大约阅读时间需要 6 分钟。

hot3.png

原文地址 using System; using System.Collections.Generic; using System.Linq; using UnityEngine; using System.Collections;

[RequireComponent(typeof(GUIText))] public class Hijack : MonoBehaviour {

//This will hold the counting up coroutine IEnumerator _countUp; //This will hold the counting down coroutine IEnumerator _countDown; //This is the coroutine we are currently //hijacking IEnumerator _current;

//A value that will be updated by the coroutine //that is currently running int value = 0;

void Start() { //Create our count up coroutine _countUp = CountUp(); //Create our count down coroutine _countDown = CountDown(); //Start our own coroutine for the hijack StartCoroutine(DoHijack()); }

void Update() { //Show the current value on the screen guiText.text = value.ToString(); }

void OnGUI() { //Switch between the different functions if(GUILayout.Button("Switch functions")) { if(_current == _countUp) _current = _countDown; else _current = _countUp; } }

IEnumerator DoHijack() { while(true) { //Check if we have a current coroutine and MoveNext on it if we do if(_current != null && _current.MoveNext()) { //Return whatever the coroutine yielded, so we will yield the //same thing yield return _current.Current; } else //Otherwise wait for the next frame yield return null; } }

IEnumerator CountUp() { //We have a local increment so the routines //get independently faster depending on how //long they have been active float increment = 0; while(true) { //Exit if the Q button is pressed if(Input.GetKey(KeyCode.Q)) break; increment+=Time.deltaTime; value += Mathf.RoundToInt(increment); yield return null; } }

IEnumerator CountDown() { float increment = 0f; while(true) { if(Input.GetKey(KeyCode.Q)) break; increment+=Time.deltaTime; value -= Mathf.RoundToInt(increment); //This coroutine returns a yield instruction yield return new WaitForSeconds(0.1f); } }

}

转载于:https://my.oschina.net/chaloongjir/blog/405192

你可能感兴趣的文章
mysql解决datetime与timestamp精确到毫秒的问题
查看>>
通过profile 用maven命令打不同配置的变量包
查看>>
Oracle trunc()函数的用法
查看>>
SQL中的left outer join,inner join,right outer join用法详解
查看>>
【SpringBoot】SpringBoot 入门示例
查看>>
Python 3.5 RuntimeError: can't start new thread
查看>>
利用ssh传输文件
查看>>
CentOS最常用命令及快捷键整理
查看>>
静态编译与动态编译的区别
查看>>
常用变量命名规则
查看>>
第一百四十六节,JavaScript,百度分享保持居中--下拉菜单
查看>>
mybatis 3.2.*打印sql结果集
查看>>
level 6 - unit4 - 强调句
查看>>
7 Django系列之关于bootstrap-table插件的简单使用
查看>>
关于window.event.srcElement 和 window.event.target(触发事件的对象)
查看>>
模板 倍增维护RMQ
查看>>
[转]:Android 5.0的调度作业JobScheduler
查看>>
yii2:不使用composer安装yii2-jui的方法
查看>>
用python爬虫抓站的一些技巧总结 zz
查看>>
基于jdom 的 xmluti
查看>>