Created
July 8, 2015 07:28
-
-
Save miguel12345/78189fb3c53128948c45 to your computer and use it in GitHub Desktop.
MonoBehaviour's CancelRoutine() that doesn't just pause a routine like StopRoutine() does
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using UnityEngine; | |
//Now Debug.Log ("End printing") will be shown when you cancel the inner coroutine | |
public class CancellabeEnumeratorTest : MonoBehaviour { | |
static int a = 0; | |
IEnumerator printRoutine = null; | |
void Start() { | |
StartCoroutine (main ()); | |
StartCoroutine (endPrinterAfter (4.0f)); | |
} | |
IEnumerator endPrinterAfter(float seconds) { | |
yield return new WaitForSeconds (seconds); | |
this.CancelRoutine (printRoutine); | |
} | |
IEnumerator main() { | |
Debug.Log ("Begin printing"); | |
printRoutine = printer ().Cancellable(); | |
yield return StartCoroutine (printRoutine); | |
Debug.Log ("End printing"); | |
} | |
IEnumerator printer() { | |
while (true) { | |
Debug.Log("Printing "+(a++)); | |
yield return new WaitForSeconds(0.5f); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
public interface ICancellabeEnumerator : IEnumerator { | |
void Cancel(); | |
bool IsCancelled(); | |
} | |
class CancellableEnumerator : ICancellabeEnumerator { | |
private IEnumerator _wrappedEnumerator; | |
private bool _cancelled = false; | |
public CancellableEnumerator(IEnumerator originalEnumerator) { | |
_wrappedEnumerator = originalEnumerator; | |
} | |
public bool MoveNext() | |
{ | |
return !_cancelled && _wrappedEnumerator.MoveNext (); | |
} | |
public object Current | |
{ | |
get | |
{ | |
return _wrappedEnumerator.Current; | |
} | |
} | |
public void Reset() | |
{ | |
_wrappedEnumerator.Reset (); | |
_cancelled = false; | |
} | |
public void Reset(IEnumerator enumerator) { | |
_cancelled = false; | |
_wrappedEnumerator = enumerator; | |
} | |
public void Cancel() { | |
_cancelled = true; | |
} | |
public bool IsCancelled() { | |
return _cancelled; | |
} | |
} | |
public static class ICancellableEnumeratorExtensions { | |
public static ICancellabeEnumerator Cancellable(this IEnumerator enumerator) { | |
return new CancellableEnumerator(enumerator); | |
} | |
public static bool Cancel(this IEnumerator enumerator,bool ignoreNull = false) { | |
if (ignoreNull && enumerator == null) | |
return false; | |
ICancellabeEnumerator cancellableEnumerator = enumerator as ICancellabeEnumerator; | |
if (cancellableEnumerator == null) { | |
Debug.LogError ("Trying to cancel a non-cancellable enumerator. Make sure you are using a cancellable enumerator by calling Cancellable()"); | |
return false; | |
} else { | |
cancellableEnumerator.Cancel (); | |
return true; | |
} | |
} | |
} | |
public static class MonoBehaviourCancelRoutineExtensions { | |
public static bool CancelRoutine(this MonoBehaviour mono, IEnumerator enumerator, bool ignoreNull = false) { | |
return enumerator.Cancel (); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment