Unity 6.3
0 онлайн 58 гостей 3 в системе
Вход

MonoBehaviour.StopCoroutine

Declaration

public void StopCoroutine(string methodName);
public void StopCoroutine(IEnumerator routine);
public void StopCoroutine(Coroutine routine);

Параметры

Параметр Описание
methodName Название короутины.
рутинная Имя функции в коде, включая сопутствующие процедуры.

Описание

Остановка первой сопутствующей программы, именуемой methodName, или сопутствующей программы, хранящейся в routine, выполняющей данное поведение.

StopCoroutine принимает один из трех аргументов, которые указывают, какую коррутину остановить:

  • Струнная функция, именующая активную сопутствующую программу.
  • Переменная IEnumerator, использованная ранее для создания сопутствующей программы.
  • Coroutine для остановки вручную созданного Coroutine.

Вы должны использовать тот же тип параметра для остановки сопутствующей программы с StopCoroutine, который использовался для ее запуска с StartCoroutine.

Сопутствующие программы также останавливаются, если:

  • Значение GameObject.activeSelf становится `false` для GameObject, к которому прикреплен скрипт.
  • Скрипт MonoBehaviour уничтожается при вызове Object.Destroy.

Примечание: Отключение скрипта MonoBehaviour путем установки Behaviour.enabled на `false` не останавливает сопутствующие программы.

StopCoroutine(null) вызывает NullReferenceException. Добавьте проверки в код, чтобы убедиться, что аргумент не является нулевым, прежде чем вызывать StopCoroutine.

В следующем примере используются 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); } } }

В следующем примере используется параметр 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; } }