Last active
January 18, 2023 08:08
-
-
Save klaszlo8207/b5faa0e88089aef0c0eb505065579650 to your computer and use it in GitHub Desktop.
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; | |
using UnityEngine.Events; | |
namespace _MyScripts.Utils | |
{ | |
public static class CoroutineExtensions | |
{ | |
public static IEnumerator PostCoroutine(this MonoBehaviour mono, IEnumerator coroutine) | |
{ | |
mono.StartCoroutine(coroutine); | |
return coroutine; | |
} | |
public static IEnumerator PostDelayed(this MonoBehaviour mono, UnityAction action, float delay) | |
{ | |
var x = ExecuteAction(delay, null, action); | |
mono.StartCoroutine(x); | |
return x; | |
} | |
public static IEnumerator PostDelayed(this MonoBehaviour mono, UnityAction actionPre, UnityAction actionPost, | |
float delay) | |
{ | |
var x = ExecuteAction(delay, actionPre, actionPost); | |
mono.StartCoroutine(x); | |
return x; | |
} | |
private static IEnumerator ExecuteAction(float delay, UnityAction actionPre, UnityAction actionPost) | |
{ | |
actionPre?.Invoke(); | |
yield return new WaitForSecondsRealtime(delay); | |
actionPost.Invoke(); | |
} | |
private static IEnumerator ExecuteAction(UnityAction action) | |
{ | |
action?.Invoke(); | |
yield return null; | |
} | |
public static void StopPostDelayed(this MonoBehaviour mono, IEnumerator x) | |
{ | |
mono.StopCoroutine(x); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment