Forked from mattyellen/UnityAsyncOperationAwaiter.cs
Created
October 2, 2020 06:45
-
-
Save smkplus/14195435d2c19bcfecba373a88ff461d 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
I wish it did support Cancellation Token somehow