1
This commit is contained in:
46
Unity/Scripts/Utility/UnityWebRequestAwaiter.cs
Normal file
46
Unity/Scripts/Utility/UnityWebRequestAwaiter.cs
Normal file
@@ -0,0 +1,46 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.Networking;
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
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)
|
||||
{
|
||||
if (continuation != null)
|
||||
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);
|
||||
*/
|
Reference in New Issue
Block a user