unity unity stopcoroutinee 停止

相关链接:
开启与关闭协程:using UnityE
using System.C
public class TestCoroutine : MonoBehaviour {
void Start ()
StartCoroutine("PrintA");//最多只能传递一个参数,并且性能消耗会更大一点
//StopCoroutine("PrintA");
IEnumerator b = PrintB();
StartCoroutine(b);
//StopCoroutine(b);
//StopCoroutine(PrintB());//不能关闭
//StopAllCoroutines();//只能关闭这个脚本的协程
IEnumerator PrintA()
yield return new WaitForSeconds(1);
print("A");
IEnumerator PrintB()
yield return new WaitForSeconds(1);
print("B");
}using UnityE
using System.C
public class TestCoroutine2 : MonoBehaviour {
void Start ()
//StartCoroutine(PrintC());
//gameObject.SetActive(false);//可以关闭协程
StartCoroutine(PrintD());
this.enabled =//不可以关闭协程
IEnumerator PrintC()
yield return new WaitForSeconds(1);
print("C");
IEnumerator PrintD()
yield return new WaitForSeconds(1);
print("D");
}嵌套协程:using UnityE
using System.C
//yield return 代码段返回 0 或者 null 的意思就是告诉 Unity 等下一帧再从这里开始执行这个方法
//在 Wait() 方法成功执行返回之前不继续执行 SaySomeThings() 方法中的代码,
//等到 Wait() 方法执行结束之后再继续执行
public class TimerExample : MonoBehaviour {
void Start()
StartCoroutine(SaySomeThings());
//Say some messages separated by time
IEnumerator SaySomeThings()
Debug.Log("The routine has started");
yield return StartCoroutine(Wait(1.0f));
Debug.Log("1 second has passed since the last message");
yield return StartCoroutine(Wait(2.5f));
Debug.Log("2.5 seconds have passed since the last message");
//Our wait function
IEnumerator Wait(float duration)
for (float timer = 0; timer & timer += Time.deltaTime)
yield return 0;
}/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////一、通过设置MonoBehaviour脚本的enabled对协程是没有影响的,但如果 gameObject.SetActive(false) 则已经启动的协程则完全停止了,即使在Inspector把gameObject 激活还是没有继续执行。也就说协程虽然是在MonoBehvaviour启动的(StartCoroutine)但是协程函数的地位完全是跟MonoBehaviour是一个层次的,不受MonoBehaviour的状态影响,但跟MonoBehaviour脚本一样受gameObject 控制,也应该是和MonoBehaviour脚本一样每帧“轮询” yield 的条件是否满足。二、yield 后面可以有的表达式:
a) null - the coroutine executes the next time that it is eligible
b) WaitForEndOfFrame - the coroutine executes on the frame, after all of the rendering and GUI is complete
c) WaitForFixedUpdate - causes this coroutine to execute at the next physics step, after all physics is calculated
d) WaitForSeconds - causes the coroutine not to execute for a given game time period
e) WWW - waits for a web request to complete (resumes as if WaitForSeconds or null)
f) Another coroutine - in which case the new coroutine will run to completion before the yielder is resumed值得注意的是 WaitForSeconds()受Time.timeScale影响,当Time.timeScale = 0f 时,yield return new WaitForSecond(x) 将不会满足。如果想忽略Time.timeScale影响,可以使用Time.realtimeSinceStartup,具体可以看这里:http://blog.csdn.net/Fredomyan/article/details/三、协程其实就是一个IEnumerator(迭代器),IEnumerator 接口有两个方法 Current 和 MoveNext() ,只有当MoveNext()返回 true时才可以访问 Current,否则会报错。迭代器方法运行到 yield return 语句时,会返回一个expression表达式并保留当前在代码中的位置。 当下次调用迭代器函数时执行从该位置重新启动。Unity在每帧做的工作就是:调用 协程(迭代器)MoveNext() 方法,如果返回 true ,就从当前位置继续往下执行。四、如果你想在多个脚本中访问到某个 Coroutine,你只需要将 Coroutine 方法声明为静态的就可以了;尽管这货看起来很像多线程的玩意儿,但实际上 Coroutine 并不是多线程的,它们跟你的脚本运行在同一个线程中;如果你的方法需要进行大量的计算,那么你可以考虑使用 Coroutine 来分步完成它;IEnumerator 方法不能有 ref 或 out 的参数,但是可以通过引用传入;
如果您想留下此文,您可以将其发送至您的邮箱(将同时以邮件内容&PDF形式发送)
相关文章推荐
(Ctrl+Enter提交) &&
已有0人在此发表见解
&在& 20:53收藏到了
&&在信息爆炸的时代,您的知识需要整理,沉淀,积累!Lai18为您提供一个简单实用的文章整理收藏工具,在这里您可以收藏对您有用的技术文章,自由分门别类,在整理的过程中,用心梳理自己的知识!相信,用不了多久,您收藏整理的文章将是您一生的知识宝库!
· 蜀ICP备号-1浅谈Unity3D中的Coroutine及其使用(延时、定时调用函数) - 博客频道 - CSDN.NET
while(alive) { step++; }
分类:Unity
一、Coroutine(协程)的概念和本质
在网上的一些资料当中,一直将Coroutine当作一个线程来描述,这样是不准确的。因为Coroutine并不是一个新的线程,它仍旧是属于主线程的一部分。Coroutine本质上是一种轻量级的thread,它的开销会比使用thread少很多。多个Coroutine可以按照次序在一个thread里面执行,一个Coroutine如果处于block状态,可以交出执行权,让其他的Coroutine继续执行。
而在Unity当中,Coroutine是在所有的Update函数执行完成之后才开始执行的,也就是在LateUpdate()之后执行,主要是用来协助主线程进行工作,就像手动添加了一个另外的update()函数在里面一样,一般情况下我们用于延时触发的功能。下面是关于MonoBehavior的执行顺序图,可以看到Coroutine是在LateUpdate之后的。
& & & & & & & & & & & & & & & & & & & & & & & & & & & & &&
而我们注意到上图中,Coroutine左边标注着几个yield模块,这个就是我们下面要讲的如何使用Coroutine里的内容了。
二、Coroutine的使用
在Untiy里面,如果想要将一个函数Fun作为一个Coroutine使用,有两点是需要注意的
1、Fun的返回类型要为IEnumerator
2、yield的使用
yield ,在Coroutine里面更像是一个红绿灯的作用,在满足紧跟在它后面的条件之前,这个协程会挂起,把执行权交给调用它的父函数,满足条件时就可以执行yield下面的代码。大家可以执行一下下面的代码看看效果
using UnityE
using System.C
public class ExampleClass : MonoBehaviour
IEnumerator WaitAndPrint()
// suspend execution for 5 seconds
yield return new WaitForSeconds(5);
print(&WaitAndPrint & + Time.time);
IEnumerator Start()
print(&Starting & + Time.time);
// Start function WaitAndPrint as a coroutine
yield return StartCoroutine(&WaitAndPrint&);
print(&Done & + Time.time);
}大家应该能得到下面的输出(本人的Unity版本是5.0.1f1)
另外大家可以再执行一下下面的代码,可以观察一下看看跟上面的有何异同
using UnityE
using System.C
public class ExampleClass : MonoBehaviour
IEnumerator WaitAndPrint()
// suspend execution for 5 seconds
yield return new WaitForSeconds(5);
print(&WaitAndPrint & + Time.time);
void Start()
print(&Starting & + Time.time);
// Start function WaitAndPrint as a coroutine
StartCoroutine(&WaitAndPrint&);
print(&Done & + Time.time);
}得到的输出如下:
从上面可以看到,我们是通过StarCoroutine的方法来启动一个新的Coroutine的,在第一段代码当中,把Start方法也声明成一个IEnumerator类型的时候,当执行到yield 模块,就会进行阻塞,等待yield模块完成了之后再继续往下执行,而声名成普通类型的时候,当启动了新的Coroutine的时候,遇到yield了,并不会把整个Start阻塞,而是会记录下当前执行的位置,然后返回父函数中执行,每一帧过后会检测yield是否满足,当满足yield条件,则返回到yield后面执行。
在Unity3D中,使用StartCoroutine(string methodName)和StartCoroutine(IEnumerator routine)都可以开启一个线程。区别在于使用字符串作为参数可以开启线程并在线程结束前终止线程,相反使用IEnumerator 作为参数只能等待线程的结束而不能随时终止(除非使用StopAllCoroutines()方法);另外使用字符串作为参数时,开启线程时最多只能传递一个参数,并且性能消耗会更大一点,而使用IEnumerator 作为参数则没有这个限制。
在Unity3D中,使用StopCoroutine(string methodName)来终止一个协同程序,使用StopAllCoroutines()来终止所有可以终止的协同程序,但这两个方法都只能终止该MonoBehaviour中的协同程序
还有一种终止Coroutine的方法,将gameObject的active属性设为False,但是设回true的时候并不会启动Coroutine。
注意:将Coroutine所在的脚本设为enable = false并不能够将Coroutine停止,因为它跟Monobehavior是同层级的。
排名:千里之外
(23)(1)(4)(14)(14)(2)(8)(1)(5)(2)Coroutine Continue Failure when using StopCoroutine - Unity Answers
Navigation
Unity account
You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio.
Coroutine Continue Failure when using StopCoroutine
I'm getting this seemingly harmless error message, and it's kind of like yeah, that was the idea... Is it safe to suppress it? Is there a workaround with the same functionality? I may need the adaptability of using Coroutine instead of IEnumerator, and I read that string is slower...
Make sure to call StopCoroutine() on the same object (MonoBehavior) that you started the coroutine.
Coroutine myCoroutine = world.StartCoroutine(MyCoroutine());
world.StopCoroutine(myCoroutine);
. Sounds like it's been fixed multiple times but keeps coming back. It is apparently harmless, and can be worked around by stopping with the IEnumerator instead of the Coroutine.
Hint: You can notify a user about this post by typing @username
Attachments: Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
25 People are following this question.我在这摘要下:
coroutine,中文翻译&协程&。这个概念可能有点冷门,不过百度之,说是一种很古老的编程模型了,以前的操作系统里进程调度里用到过,现在操作系统的进程调度都是根据时间片和优先级来进行轮换,以前是要程序自己来释放cpu的控制权,一直不释放一直也就占用着cpu,这种要求程序自己来进行调度的编程模型应该就叫&协程&了。
协程和线程差不多,线程的调度是由操作系统完成的,协程把这项任务交给了程序员自己实现,当然也就可以提高灵活性,另外协程的开销比线程要小,在程序里可以开更多的协程。
一些语言里自带了对coroutine的实现,比如lua。c里面虽然没有coroutine,不过windows下提供了一种叫fiber的机制,叫做&纤程&,算是一种轻量级线程。
一。什么是协同程序
&&&&&& 协同程序,即在主程序运行时同时开启另一段逻辑处理,来协同当前程序的执行。换句话说,开启协同程序就是开启一个线程。
二。协同程序的开启与终止
&&&&&& 在Unity3D中,使用MonoBehaviour.StartCoroutine方法即可开启一个协同程序,也就是说该方法必须在MonoBehaviour或继承于MonoBehaviour的类中调用。
&&&&&& 在Unity3D中,使用StartCoroutine(string methodName)和StartCoroutine(IEnumerator routine)都可以开启一个线程。区别在于使用字符串作为参数可以开启线程并在线程结束前终止线程,相反使用IEnumerator 作为参数只能等待线程的结束而不能随时终止(除非使用StopAllCoroutines()方法);另外使用字符串作为参数时,开启线程时最多只能传递一个参数,并且性能消耗会更大一点,而使用IEnumerator 作为参数则没有这个限制。
&&&&&&& 在Unity3D中,使用StopCoroutine(string methodName)来终止一个协同程序,使用StopAllCoroutines()来终止所有可以终止的协同程序,但这两个方法都只能终止该MonoBehaviour中的协同程序。
&&&&&&& 还有一种方法可以终止协同程序,即将协同程序所在gameobject的active属性设置为false,当再次设置active为ture时,协同程序并不会再开启;如是将协同程序所在脚本的enabled设置为false则不会生效。这是因为协同程序被开启后作为一个线程在运行,而MonoBehaviour也是一个线程,他们成为互不干扰的模块,除非代码中用调用,他们共同作用于同一个对象,只有当对象不可见才能同时终止这两个线程。然而,为了管理我们额外开启的线程,Unity3D将协同程序的调用放在了MonoBehaviour中,这样我们在编程时就可以方便的调用指定脚本中的协同程序,而不是无法去管理,特别是对于只根据方法名来判断线程的方式在多人开发中很容易出错,这样的设计保证了对象、脚本的条理化管理,并防止了重名。
&这有些个国外的人解释的,算是比较清楚,只是还是没有原理上的解释:
我的一些粗浅小结:
1.Coroutines顾名思议是用来协助主要进程的,在Unity中感觉就是一个可动态添加和移除的Update()函数。它的调用在所有Update函数之后。
Unity原文:
If you start a coroutine in LateUpdate it will also be called after LateUpdate just before rendering.
Coroutines are executed after all Update functions.
2.yield就像是一个红绿灯,在满足紧跟在它后面的条件之前,这个协程会挂起,把执行权交给调用它的父函数,满足条件时就可以执行yield下面的代码。
Unity原文:
Normal coroutine updates are run after the Update function returns. A coroutine is function that can suspend its execution (yield) until the given given YieldInstruction finishes. Different uses of Coroutines:
The coroutine will continue after all Update functions have been called on the next frame.
yield WaitForSeconds(2);Continue after a specified time delay, after all Update functions have been called for the frame
yield WaitForFixedUpdate();Continue after all FixedUpdate has been called on all scripts
yield WWWContinue after a WWW download has completed.
yield StartCoroutine(MyFunc); Chains the coroutine, and will wait for the MyFunc coroutine to complete first.
阅读(...) 评论()}

我要回帖

更多关于 unity 停止播放动画 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信