-
-
Save Sov3rain/ec186d0f053cb5bc675bf3357df92e8a to your computer and use it in GitHub Desktop.
Allows the use of async/await (instead of yield) with any Unity AsyncOperation
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
public static class ExtensionMethods | |
{ | |
public static TaskAwaiter GetAwaiter(this AsyncOperation asyncOp) | |
{ | |
var tcs = new TaskCompletionSource<object>(); | |
asyncOp.completed += obj => { tcs.SetResult(null); }; | |
return ((Task)tcs.Task).GetAwaiter(); | |
} | |
} | |
/* Example: | |
var getRequest = UnityWebRequest.Get("http://www.google.com"); | |
await getRequest.SendWebRequest(); | |
var result = getRequest.downloadHandler.text; | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment