unity3d 后台资源加载 yield用法

来源:互联网 发布:python 隐马尔可夫模型 编辑:程序博客网 时间:2024/05/17 03:01
项目需求是一个应用在启动时需要读取几个配置文件,原始文档自然是excel格式,为了方便,我把它们转存为csv格式,即用逗号分割的文本格式,然后在js脚本中自己解析,倒也轻松,但是问题就是解析得比较慢。 一个100行80多列的文档解析需要几秒钟,这个有点难受了。
更可怕的是unity发布到ios上的程序启动有个限制,超过一定的时间没有加载完成会被系统强制终止。
控制台错误输入如下:


Aug 20 17:00:45 xxx-iPhone SpringBoard[30] <Warning>: xxx failed to launch in time
Aug 20 17:00:45 xxx-iPhone SpringBoard[30] <Warning>: Forcing crash report of xxx[1067]...
Aug 20 17:00:46 xxx-iPhone SpringBoard[30] <Warning>: Finished crash reporting.
Aug 20 17:00:46 xxx-iPhone com.apple.launchd[1] (UIKitApplication:com.xxx.xxx[0xd288][1067]) <Notice>: (UIKitApplication:com.xxx.xxx[0xd288]) Exited: Killed: 9

其中的xxx是代替了个人的一些信息,注意几个关键字: 
failed to launch in time (没能及时启动成功)
Exited : Killed : 9 (以kill 9的方式退出, kill 9 是被强制终止)


为解决此问题搜了解决方法,查到有个yield可以做点事情,研究了一下,现将结果记录于此备忘。
yield的核心功能是让代码在此返回,下一循环(update)继续执行
看unity自己的例子:
function Start () {
StartCoroutine("DoSomething", 2.0);
yield WaitForSeconds(1);
StopCoroutine("DoSomething");
}

function DoSomething (someParameter : float) {
while (true) {
print("DoSomething Loop");
// Yield execution of this coroutine and return to the main loop until next frame
yield;
}
}
上面的一段先不管,先看下面一段,循环中的yield会让函数暂时返回,下一帧继续,这个在c++里面是没有的,一开始难以理解,以为类似于MFC里面的Dialog.DoModal(),于是就这么用了:
function Start(){
    LoadA();
    LoadB();
    LoadC();
}
LoadA(), LoadB(), LoadC()分别是一个类似于上面DoSomething的函数,但是我的LoadB是依赖LoadA完成的,就是说如果LoadA没有完成时是不能开始LoadB的,这样用就出错了。
因为LoadA并没有想Dialog.DoModal()一样,而是直接返回了,就是说第一帧,LoadA,LoadB,LoadC都执行了,这非我预想。经过加入各种输出信息,终于明白过来,函数是立即返回的,但是里面的循环工作(yield后面的代码)会在后续帧里面得以继续执行
为此,需要重新找到LoadA完成的切入点,我是这样做的:
function LoadA()
{
for( var i : int = 0; i < 99; ++i)
 {
    // do something ......

    yield;
 }
CallFunctionFinished();
}
然后在CallFunctionFinished()这个函数里面调用LoadB,当然也可以用SendMessage把调用做的低耦合一点。
还有个StartCoroutine,在js里面是可无的,unity的文档中明确说了:
When using JavaScript it is not necessary to use StartCoroutine, the compiler will do this for you. When writing C# code you must call StartCoroutine.


如果要做一个loading的进度条,就只需要在我们读取数据的循环中加入计数,然后在update函数中读取计数显示进度条即可。
原创粉丝点击