All Downloads are FREE. Search and download functionalities are using the official Maven repository.

csharp.libraries.unityWebRequest.UnityWebRequestAwaiterExtension.mustache Maven / Gradle / Ivy

The newest version!
{{>partial_header}}

using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using UnityEngine.Networking;

namespace {{packageName}}.Client
{
    /// 
    /// Provides an awaiter for UnityWebRequestAsyncOperation to allow `await request.SendWebRequest()`
    /// in Unity async methods. Unity's AsyncOperation types are not awaitable by default, so this
    /// extension bridges that gap by converting the operation into a Task.
    /// 
    public static class UnityWebRequestAwaiterExtensions
    {
        public static TaskAwaiter GetAwaiter(this UnityWebRequestAsyncOperation asyncOp)
        {
            var tcs = new TaskCompletionSource();
            asyncOp.completed += _ => tcs.SetResult(asyncOp);
            return tcs.Task.GetAwaiter();
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy