46 lines
1.1 KiB
C#
46 lines
1.1 KiB
C#
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);
|
|
*/ |