博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Unity 协程使用指南
阅读量:4598 次
发布时间:2019-06-09

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

0x00 前言

在使用Unity的过程中,对协程只知道如何使用,但并不知道协程的内部机理,对于自己不清楚的部分就像一块大石压力心里,让自己感觉到担忧和不适。这篇文章一探究竟,彻底揭开协程的面纱,让大家在使用中不再有后顾之忧。

0x01 概念

协程是:程序组件来生成非抢占式多任务子函数,生成的子函数允许在程序里挂起和唤醒操作。

0x02 使用场景

通常协程可以很方便实现延时操作,以及异步加载操作。下面是两个简单协程使用例子。

延时操作

// Use this for initializationvoid Start () {    StartCoroutine (Wait ());}IEnumerator Wait(){    Debug.Log ("start time:" + Time.time);    yield return new WaitForSeconds (1);    Debug.Log ("time:" + Time.time);    yield return new WaitForSeconds(2);    Debug.Log ("time:" + Time.time);}

异步加载资源

// Use this for initializationvoid Start () {;    System.Action
callBack = delegate(string text) { Debug.Log(text); }; StartCoroutine (LoadRes (callBack));}IEnumerator LoadRes(System.Action
callBack){ WWW www = new WWW ("http://www.baidu.com"); yield return www; if (string.IsNullOrEmpty (www.error)) { callBack(www.text); Debug.Log("load success"); } else{ Debug.Log("load failed"); }}

0x03 原理

Unity里的协程通过定义一个返回 IEnumerator类型的函数,先来通过一个函数看看Unity都能返回那些类型:

IEnumerator Test(){    yield return 2;     // 返回整数     yield return 4.2;   // 返回浮点数      yield return null;  // 返回null    yield return new WaitForSeconds(1); // 返回instance    yield return new WWW ("http://www.baidu.com"); // 返回instance}

返回的类型有什么要求?整理一下Unity都实现了那些返回类型:

1、int类型,需要等待的帧数
2、float类型,需要等待的时间(秒)
3、null,等待一帧
4、break,结束协程
5、实例,必须有bool isDone()成员函数,等isDone返回true
6、IEnumerator,等IEnumerator实例的MoveNext()返回false

Unity的返回类型知道了,如何捕获这些返回类型?来看IEnumerator如何实现的?

public interface IEnumerator{    //    // Properties    //    object Current    {        get;    }    //    // Methods    //    bool MoveNext ();    void Reset ();}

通过研究IEnumerator接口,得到通过调用MoveNext,我们可以得到遍历所有yield返回的值,返回的值可以通过Current得到。每次调用MoveNext都会执行夹在yield中间的代码。写个测试程序来验证我们的理论:

public class game_client : MonoBehaviour {    // Use this for initialization    void Start () {        IEnumerator i = Test ();        while (true) {            if(!i.MoveNext()){                break;            }            object cur = i.Current;            if(cur != null)                Debug.Log(cur.GetType());            else                Debug.Log("type is null");        }    }    IEnumerator Test(){        yield return 2;                 yield return 4.2;        yield return null;        yield return new WaitForSeconds(1);        yield return new WWW ("http://www.baidu.com");    }}

通过验证程序,可以得到yield返回的值,有了这些值,就可以实现自己的协程。

0x04 实现

设计接口:

class ScheduleCoroutine{    public void StartCoroutine(IEnumerator coroutine);    public void StopCoroutine(IEnumerator coroutine);    public void Update(int frame, float time);}

设计数据结构:

class CoroutineNode{    public IEnumerator itor;    public string name;    public int frame;    public float time;    public Object instance;    public CoroutineNode pre;    public CoroutineNode next;}

具体实现代码,对于不同的项目需求,有不同的实现方式。这篇文章主要是探寻Unity协程的实现方式。搞清楚原理后,在使用上就会更加得心应手。

0x05 参考

文章参考了很多其他博文,感谢他们的付出。在第一个参考链接里,有具体实现代码。

1、
2、

转载于:https://www.cnblogs.com/fengju/p/6174280.html

你可能感兴趣的文章
无线话筒U段和V段有哪些本质区别?
查看>>
Delphi判断文件是否正在被使用
查看>>
AutoCAD(英文版)中所有英语词汇的翻译
查看>>
powerdesigner添加mysql的字符集ENGINE和DEFAULT CHARACTER SET
查看>>
在.net中使用WebBrowser
查看>>
委托和事件
查看>>
x名称空间详解
查看>>
xamarin.android 消息推送功能--极光推送
查看>>
词向量的cbow模型
查看>>
UDP简单例子
查看>>
viewpager viewpager+fragment
查看>>
第二次冲刺
查看>>
使用JavaScriptSerializer序列化集合、字典、数组、DataTable为JSON字符串 ...
查看>>
20个Flutter实例视频教程-第12节: 流式布局 模拟添加照片效果
查看>>
阶段1 语言基础+高级_1-3-Java语言高级_02-继承与多态_第1节 继承_1_继承的概述
查看>>
用户场景分析
查看>>
hdoj1002
查看>>
数据结构,你还记得吗(中)
查看>>
C#规范整理·资源管理和序列化
查看>>
java IO笔记(InputStream/OutputSteram)
查看>>