-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from coryleach/dev
Fix for Unity's YieldInstructions
- Loading branch information
Showing
6 changed files
with
114 additions
and
51 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
using System; | ||
using System.Collections; | ||
using UnityEngine; | ||
|
||
namespace Gameframe.Async.Coroutines | ||
{ | ||
public class CoroutineHost : MonoBehaviour | ||
{ | ||
private static CoroutineHost _instance; | ||
|
||
public static void Run(YieldInstruction yieldInstruction, Action onComplete = null) | ||
{ | ||
GetHost().StartCoroutine(RunYieldInstruction(yieldInstruction, onComplete));; | ||
} | ||
|
||
public static Coroutine RunCoroutine(YieldInstruction yieldInstruction, Action onComplete = null) | ||
{ | ||
return GetHost().StartCoroutine(RunYieldInstruction(yieldInstruction, onComplete));; | ||
} | ||
|
||
public static Coroutine RunCoroutine(IEnumerator routine, Action onComplete = null) | ||
{ | ||
return GetHost().StartCoroutine(RunRoutine(routine, onComplete));; | ||
} | ||
|
||
public static void KillCoroutine(Coroutine coroutine) | ||
{ | ||
GetHost().StopCoroutine(coroutine); | ||
} | ||
|
||
private static CoroutineHost GetHost() | ||
{ | ||
if (_instance != null) | ||
{ | ||
return _instance; | ||
} | ||
|
||
_instance = new GameObject("_CoroutineHost").AddComponent<CoroutineHost>(); | ||
_instance.gameObject.hideFlags = HideFlags.DontSave | HideFlags.HideInHierarchy; | ||
return _instance; | ||
} | ||
|
||
private static IEnumerator RunYieldInstruction(YieldInstruction instruction, Action onComplete) | ||
{ | ||
yield return instruction; | ||
onComplete?.Invoke(); | ||
} | ||
|
||
private static IEnumerator RunRoutine(IEnumerator routine, Action onComplete) | ||
{ | ||
yield return routine; | ||
onComplete?.Invoke(); | ||
} | ||
|
||
private void Awake() | ||
{ | ||
DontDestroyOnLoad(gameObject); | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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