unity中WWW isDone方法只能在主线程中调用
2013-06-26 11:45
239 查看
项目中要使用动态加载,原计划是生成WWW对象后,放到一个容器里。由一个独立线程轮询容器里的对象,如果www.isDone为true时,回调一个接口把结果交给请求方。
new Thread( new ThreadStart( XXX.run ) );
运行以后出现下面问题:
详细错误:
get_isDone can only be called from the main thread.
Constructors and field initializers will be executed from the loading thread when loading a scene.
Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.
提示说isDone必须在主线程内被调用。
无奈,原来线程中轮训的代码放到MonoBehaviour的派生类中,利用update()方法被循环(每一桢)调用的特点来轮询容器。
update是每一桢调用,这样的轮询间隔没有必要,所以需要在update方法中加一deltaTime的累加,到达需要的步长时再执行业务代码。updateStep为0时则不执行延迟调用。
if( 0 < updateStep ){
tm += Time.deltaTime;
if( tm >= 2 ){
tm = 0f;
// Call custom update
update();
}
} else {
// Call custom update
update();
}
new Thread( new ThreadStart( XXX.run ) );
运行以后出现下面问题:
详细错误:
get_isDone can only be called from the main thread.
Constructors and field initializers will be executed from the loading thread when loading a scene.
Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.
提示说isDone必须在主线程内被调用。
无奈,原来线程中轮训的代码放到MonoBehaviour的派生类中,利用update()方法被循环(每一桢)调用的特点来轮询容器。
update是每一桢调用,这样的轮询间隔没有必要,所以需要在update方法中加一deltaTime的累加,到达需要的步长时再执行业务代码。updateStep为0时则不执行延迟调用。
if( 0 < updateStep ){
tm += Time.deltaTime;
if( tm >= 2 ){
tm = 0f;
// Call custom update
update();
}
} else {
// Call custom update
update();
}
相关文章推荐
- [原]unity中WWW isDone方法只能在主线程中调用
- 关于在for循环里调用ajax时只能取到最后一个数据的bug的解决方法
- Unity SLua 如何调用Unity中C#方法
- Unity调用OC方法
- Unity解决脚本相互调用的几种方法
- Struts2-动态方法调用-Could not find action or result-Method update for action user is not allowed
- Unity4.6.3之间隔、延时调用方法
- unity中调用dll(char*,以及带有函数指针的参数)方法总结
- 安卓开发解决android.os.NetworkOnMainThreadException异常方法(主线程不能直接调用webservice)
- unity GameObject.SendMessage调用的方法有多个参数的调用方法
- [Unity2d系列教程] 004.Unity如何调用ios的方法(SDK集成相关)
- unity 运行时类调用方法顺序
- iOS 9以上系统调用不了支付宝客户端只能调用网页解决方法
- unity 调用其它类中方法几种方法
- Unity调用android的方法,android调用Unity的方法,unity和安卓的交互
- 调用其他服务器的api时,暂时处理跨域问题的方法——No 'Access-Control-Allow-Origin' header is present on the requested resour
- Unity SLua 如何调用Unity中C#方法
- Unity调用WebService的方法,支持IL2CPP
- Unity,WebGL, 页面JS调用Unity方法
- Inside CRT: Debug Heap Management -- Learn how heap management is done in a debug build[www.codeguru.com]