Created
July 20, 2018 22:47
-
-
Save krzys-h/9062552e33dd7bd7fe4a6c12db109a1a to your computer and use it in GitHub Desktop.
[Unity] Use UnityWebRequest with async/await
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 class UnityWebRequestAwaiter : INotifyCompletion | |
{ | |
private UnityWebRequestAsyncOperation asyncOp; | |
private Action continuation; | |
public UnityWebRequestAwaiter(UnityWebRequestAsyncOperation asyncOp) | |
{ | |
this.asyncOp = asyncOp; | |
asyncOp.completed += OnRequestCompleted; | |
} | |
public bool IsCompleted { get { return asyncOp.isDone; } } | |
public void GetResult() { } | |
public void OnCompleted(Action continuation) | |
{ | |
this.continuation = continuation; | |
} | |
private void OnRequestCompleted(AsyncOperation obj) | |
{ | |
continuation(); | |
} | |
} | |
public static class ExtensionMethods | |
{ | |
public static UnityWebRequestAwaiter GetAwaiter(this UnityWebRequestAsyncOperation asyncOp) | |
{ | |
return new UnityWebRequestAwaiter(asyncOp); | |
} | |
} | |
/* | |
// Usage example: | |
UnityWebRequest www = new UnityWebRequest(); | |
// ... | |
await www.SendWebRequest(); | |
Debug.Log(req.downloadHandler.text); | |
*/ |
if continuation is null, it's possible it could be called while null, so OnRequestComplete should accommodate a nullcheck.
It is no possible, because subscription made after set of parameter in OnCompleted.
this.continuation = continuation;
asyncOp.completed += OnRequestCompleted;
After performance and allocation`s tests we made this minimalistic version:
[ DebuggerNonUserCode ]
public readonly struct AsyncOperationAwaiter : INotifyCompletion
{
private readonly AsyncOperation _asyncOperation;
public bool IsCompleted => _asyncOperation.isDone;
public AsyncOperationAwaiter( AsyncOperation asyncOperation ) => _asyncOperation = asyncOperation;
public void OnCompleted( Action continuation ) => _asyncOperation.completed += _ => continuation();
public void GetResult() { }
}
[ DebuggerNonUserCode ]
public readonly struct UnityWebRequestAwaiter : INotifyCompletion
{
private readonly UnityWebRequestAsyncOperation _asyncOperation;
public bool IsCompleted => _asyncOperation.isDone;
public UnityWebRequestAwaiter( UnityWebRequestAsyncOperation asyncOperation ) => _asyncOperation = asyncOperation;
public void OnCompleted( Action continuation ) => _asyncOperation.completed += _ => continuation();
public UnityWebRequest GetResult() => _asyncOperation.webRequest;
}
This code and other for async operations in unity could be found in https://github.com/CrazyPandaLimited/SimplePromises
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A couple errors there
continuation
isn't assigned,asyncOp.completed
can't be set tocontinuation
, parameter mismatch. It should be set toOnRequestCompleted
(unless changingAction continuation
toAction<AsyncOperation> continuation
, but that wicked limits the callbacks you'd be able to apply,continuation
is null, it's possible it could be called while null, soOnRequestComplete
should accommodate a nullcheckUpdated code (including library imports):