methodName | 协同程序的名称。 |
routine | 代码中函数的名称,包括协同程序。 |
停止在该行为上运行的第一个名为 methodName
的协同程序或存储在 routine
中的协同程序。
StopCoroutine 接受以下三个参数(用于指定要停止的协同程序)之一:
一个字符串函数,用于命名激活的协同程序
之前用于创建该协同程序的 IEnumerator
变量。
一个 /Coroutine/,用于停止手动创建的 /Coroutine/。
注意:不要混淆三个参数。
如果使用了字符串作为 StartCoroutine 中的参数,请在 StopCoroutine 中使用该字符串。
同样,在 StartCoroutine 和 StopCoroutine 中使用相同的 StopCoroutine
。
最后才为 StopCoroutine
使用创建时使用的 /Coroutine/。
在 CS 示例中,使用了 IEnumerator 类型。
using UnityEngine; using System.Collections;
public class Example : MonoBehaviour { // keep a copy of the executing script private IEnumerator coroutine;
// Use this for initialization void Start() { print("Starting " + Time.time); coroutine = WaitAndPrint(3.0f); StartCoroutine(coroutine); print("Done " + Time.time); }
// print to the console every 3 seconds. // yield is causing WaitAndPrint to pause every 3 seconds public IEnumerator WaitAndPrint(float waitTime) { while (true) { yield return new WaitForSeconds(waitTime); print("WaitAndPrint " + Time.time); } }
void Update() { if (Input.GetKeyDown("space")) { StopCoroutine(coroutine); print("Stopped " + Time.time); } } }
下面的 cs 示例演示如何使用 StopCoroutine(Coroutine)。
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class ExampleClass : MonoBehaviour { void Start() { StartCoroutine(coroutineA()); }
IEnumerator coroutineA() { // wait for 1 second yield return new WaitForSeconds(1.0f); Debug.Log("coroutineA() started: " + Time.time);
// wait for another 1 second and then create b yield return new WaitForSeconds(1.0f); Coroutine b = StartCoroutine(coroutineB());
yield return new WaitForSeconds(2.0f); Debug.Log("coroutineA() finished " + Time.time);
// B() was expected to run for 10 seconds // but was shut down here after 3.0f StopCoroutine(b); yield return null; }
IEnumerator coroutineB() { float f = 0.0f; float start = Time.time;
Debug.Log("coroutineB() started " + start);
while (f < 10.0f) { Debug.Log("coroutineB(): " + f); yield return new WaitForSeconds(1.0f); f = f + 1.0f; }
// Intended to handling exit of the this coroutine. // However coroutineA() shuts coroutineB() down. This // means the following lines are not called. float t = Time.time - start; Debug.Log("coroutineB() finished " + t); yield return null; } }
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.