Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save smkplus/14195435d2c19bcfecba373a88ff461d to your computer and use it in GitHub Desktop.
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
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