MonoBehaviour.StopCoroutine
Deklarasyon
public void StopCoroutine(string methodName);
Deklarasyon
public void StopCoroutine(IEnumerator routine);
Deklarasyon
public void StopCoroutine(Coroutine routine);
Parametreler
methodName | Coroutine'nin adı. |
routine | Kodda, coroutine'ler dahil fonksiyonun adı. |
Açıklama
İlk coroutine'i, methodName olarak adlandırılan veya bu davranışta çalışan routine içinde depolanan coroutine'i durdurur.
StopCoroutine, hangi coroutine'in durdurulacağını belirten üç argümandan birini alır:
- Aktif coroutine'i adlandıran bir string fonksiyonu
- Coroutine oluşturmak için daha önce kullanılan IEnumerator değişkeni
- Manuel olarak oluşturulan Coroutine'i durdurmak için Coroutine
Not: Üç argümanı karıştırmayın. StartCoroutine'de argüman olarak bir string kullanılıyorsa, StopCoroutine'de de string kullanın. Benzer şekilde, hem StartCoroutine hem de StopCoroutine'de IEnumerator kullanın. Son olarak, oluşturma için kullanılan Coroutine ile StopCoroutine kullanın.
Aşağıdaki CS örneğinde, IEnumerator türü kullanılmıştır.
using UnityEngine;
using System.Collections;
public class Example : MonoBehaviour
{
// Çalışan komut dosyasının bir kopyasını saklayın
private IEnumerator coroutine;
// Başlatma için kullanın
void Start()
{
print("Starting " + Time.time);
coroutine = WaitAndPrint(3.0f);
StartCoroutine(coroutine);
print("Done " + Time.time);
}
// Her 3 saniyede bir konsola yazdır
// yield, WaitAndPrint'in her 3 saniyede bir duraklamasına neden olur
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);
}
}
}
Aşağıdaki cs örneği, StopCoroutine(Coroutine) kullanımını göstermektedir.
using System.Collections; using System.Collections.Generic; using UnityEngine; public class ExampleClass : MonoBehaviour { void Start() { StartCoroutine(coroutineA()); } IEnumerator coroutineA() { // 1 saniye bekle yield return new WaitForSeconds(1.0f); Debug.Log("coroutineA() started: " + Time.time); // 1 saniye daha bekle ve ardından b oluştur yield return new WaitForSeconds(1.0f); Coroutine b = StartCoroutine(coroutineB()); yield return new WaitForSeconds(2.0f); Debug.Log("coroutineA() finished " + Time.time); // B()'nin 10 saniye çalışması bekleniyordu // ancak burada 3.0f sonra kapatıldı 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; } // Bu coroutine'in çıkışını ele almak için tasarlandı. // Ancak coroutineA() coroutineB()'yi kapatıyor. Bu // demek ki aşağıdaki satırlar çağrılmıyor. float t = Time.time - start; Debug.Log("coroutineB() finished " + t); yield return null; } }
Yorumlar
Yorum Gönder