Unity3d 协程的注意问题(新手须注意,老手须加勉)
关于unity3d的协程,非常的好用,比如等待几秒执行,等待下一帧执行等!
但是也有潜在的问题:
1.协程是单线程的,在主线程中完成
2.如果发现yield, 那么这一帧会结束,那么等下一帧调用此脚本的时候继续执行!
如果同一个协程方法被两个方法在相对短的时间内都进行调用,那么就会出现逻辑上的错误!这个时候需要用锁来锁定!
我举例如下:
using UnityEngine;
using System.Collections;
public class YieldScpipt : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public void RunContinue()
{
StartCoroutine(run());
}
IEnumerator run()
{
print("1");
//yield return 0;
print("2");
//yield return 0;
print("3");
yield return 0;
}
}
在另一个类中用Test方法进行调用
public class Test : MonoBehaviour {
public YieldScpipt ys;
// Use this for initialization
void Start () {
for(int i=0;i<3;i++)
{
ys.RunContinue();
}
}
// Update is called once per frame
void Update () {
}
}
那么会打印如下:1,2,3,1,2,3.。。。预期的顺序
如果把注释去掉
IEnumerator run()
{
print("1");
yield return 0;
print("2");
yield return 0;
print("3");
yield return 0;
}
那么打印的顺序就不能保证了,这就是我遇到的问题!
当然,我不是用for循环这样引起的,我是不经意间写错了逻辑,导致在很短的时间内,这个方法被两次或者多次调用,导致了逻辑错误!
有什么问题请指出!欢迎与广州老龙联系!
Unity3d 协程的注意问题(新手须注意,老手须加勉)的更多相关文章
- Unity3D协程yield的理解
Unity3D的协程概括地将就是:对于一段程序,你可以加上yield标明哪里需要暂停,然后在下一帧或者一段时间后,系统会继续执行这段代码.协程的作用:①延迟一段时间执行代码.②等某个操作完成之后再执行 ...
- [转]Unity3D协程介绍 以及 使用
作者ChevyRay ,2013年9月28日,snaker7译 原文地址:http://unitypatterns.com/introduction-to-coroutines/ 在Unity中,协 ...
- Unity3D协程
协程介绍 Unity的协程系统是基于C#的一个简单而强大的接口 ,IEnumerator,它允许你为自己的集合类型编写枚举器.这一点你不必关注太多,我们直接进入一个简单的例子来看看协程到底能干什么.首 ...
- Unity3D协程介绍 以及 使用
作者ChevyRay ,2013年9月28日,snaker7译 原文地址:http://unitypatterns.com/introduction-to-coroutines/ 在Unity中,协 ...
- Unity3D协程(转)
这篇文章转自:http://blog.csdn.net/huang9012/article/details/38492937 协程介绍 在Unity中,协程(Coroutines)的形式是我最喜欢的功 ...
- Unity3D 协程 浅谈
协程 理解:协程不是线程,也不是异步执行(知道就行). 1.协程和MonoBehaviour的Update函数一样,也是在MainThread中执行的(一定得明白这句话意思). void Start ...
- Unity3d 协程
参考文章: http://blog.csdn.net/onafioo/article/details/48979939 http://www.cnblogs.com/zhaoqingqing/p/37 ...
- Unity3D 协程 Coroutine
协程(Coroutine)的概念存在于很多编程语言,例如Lua.ruby等.而由于Unity3D是单线程的,因此它同样实现了协程机制来实现一些类似于多线程的功能,但是要明确一点协程不是进程或线程,其执 ...
- Unity3D 协程的介绍和使用
我是快乐的搬运工 http://blog.csdn.net/u011397120/article/details/61236055 ---------------------------------- ...
随机推荐
- android中实现简单的播放
MediaPlayer mediaPlayer1; mediaPlayer1 = MediaPlayer.create(getBaseContext(), R.raw.ic_yanyuan); med ...
- Nginx+Tomcat+memcached负载均衡实现session共享
http://blog.csdn.net/love_ubuntu/article/details/8464983 1. 安装各个软件不用说了. 2. 到tomcat的安装目录lib中,加入: me ...
- Lua Script
注意事项: 1:Lua 只支持数字类型,浮点类型的值,在转换成Redis 协议值得时候,小数点会被忽略(如果需要在Lua中使用浮点值,建议用字符串代替) 2:Lua 返回表中如果遇到nils(空),转 ...
- jquery的事件绑定
暂时有 bind(),live(),delegate(),on() 这四个事件监听函数 对应的4个事件解除函数分别是: unbind(),die(),undelegate(),off() bind:向 ...
- Sqlserver With as
with t as (select * from emp where depno=10) 总结:可以看做将查询出来的语句块表示为一个临时表 select * from t where empno=xx ...
- windows下能读写linux分区的软件 转
1. ext2ifs 这个工具与explore2fs都是John Newbigin使用Delphi写的,explore2fs Copyright (C) 2000,Ext2IFS v0.3 Copyr ...
- c#打开txt文件并导入到textbox中
OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Title = " 请选择您要导入的模板文件:&qu ...
- 00_ForTest
-----该页是爬虫的测试页请忽略------- 1234545@qq.comadasdsdasdsad阿打算多少其热情为 asdasdasdasd4w5wsdvv啊实打实大 啊实打实大asdasda ...
- leetcode342——Power of Four(C++)
Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Example:Giv ...
- 绑定本地Service并与之通信
绑定Service需要调用 public boolean bindService (Intent service, ServiceConnection conn, int flags): 传入一个Se ...