Forked from mattyellen/UnityAsyncOperationAwaiter.cs
Created
August 13, 2023 09:00
-
-
Save HajiyevEl/cbd275059011b0c9d73fa548953247d5 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 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