Skip to content

Instantly share code, notes, and snippets.

@krzys-h
Created July 20, 2018 22:47
Show Gist options
  • Star 53 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save krzys-h/9062552e33dd7bd7fe4a6c12db109a1a to your computer and use it in GitHub Desktop.
Save krzys-h/9062552e33dd7bd7fe4a6c12db109a1a to your computer and use it in GitHub Desktop.
[Unity] Use UnityWebRequest with async/await
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);
*/
@Alexees
Copy link

Alexees commented Sep 5, 2019

@krzys-h the code fails in situations where the UnityWebRequestAsyncOperation is immediately successful, which is the case when you try to load a file from StreamingAssets on Android. Then OnRequestCompleted is called without OnCompleted being called first and therefore continuation is null. The whole thing never returns.

@HelloKitty
Copy link

HelloKitty commented Jan 27, 2020

This is likely true based on documentation claiming the "completed" event: If a handler is registered after the operation has completed and has already invoked the complete event, the handler will be called synchronously.

@zakrzus
Copy link

zakrzus commented Apr 20, 2020

Hi guys,
Do you know how to track progress of downloading with IProgress interface?

@mattyellen
Copy link

Here's an improved version.

  • Simpler code.
  • Handles the case where the operation has already succeeded and the handler is called synchronously.
  • Supports anything that's a Unity AsyncOperation, not just UnityWebRequest

@sergiregi88
Copy link

Hi, I'm trying to make a call from Unity with UnityWebRequest to server php.
In Jetbrains Raider with async and await and when I click the play button and send the request Unity gets stuck, with no response flag in task administration window in windows 10. I don't know if I doing something wrong or what?

image
Help Please

@eviathan
Copy link

eviathan commented Jan 29, 2021

Yep the above code just makes Unity hang. It's too late for me to look at why but shouldn't the GetResult method be implemented?

@aadi3124
Copy link

Hi, I'm trying to make a call from Unity with UnityWebRequest to server php.
In Jetbrains Raider with async and await and when I click the play button and send the request Unity gets stuck, with no response flag in task administration window in windows 10. I don't know if I doing something wrong or what?

image
Help Please

Hi, Did you find the solution to this, I am getting the same issue. Please let me know.

@streeter12
Copy link

streeter12 commented Jun 18, 2021

public struct UnityWebRequestAwaiter : INotifyCompletion
{
	private UnityWebRequestAsyncOperation asyncOp;
	private Action continuation;

	public UnityWebRequestAwaiter(UnityWebRequestAsyncOperation asyncOp)
	{
		this.asyncOp = asyncOp;
	}

	public bool IsCompleted { get { return asyncOp.isDone; } }

	public void GetResult() { }

	public void OnCompleted(Action continuation)
	{
                this.continuation = continuation;
		asyncOp.completed += continuation;
	}

	private void OnRequestCompleted(AsyncOperation obj)
	{
		continuation();
	}
}

public static class ExtensionMethods
{
	public static UnityWebRequestAwaiter GetAwaiter(this UnityWebRequestAsyncOperation asyncOp)
	{
		return new UnityWebRequestAwaiter(asyncOp);
	}
}

To reduce allocations i suggest to make WebRequestAwaiter as struct and asyncOp.completed += continuation subscribtion only in OnCompleted call. If IsCompleted = true, when asyc state machine will not call OnCompleted but just get the result.
Order of operators in OnCompleted is important, because struct subscribtion make copy of struct in delegate field.

@nickyonge
Copy link

nickyonge commented Aug 20, 2021

To reduce allocations i suggest to make WebRequestAwaiter as struct and asyncOp.completed += continuation subscribtion only in OnCompleted call. If IsCompleted = true, when asyc state machine will not call OnCompleted but just get the result.
Order of operators in OnCompleted is important, because struct subscribtion make copy of struct in delegate field.

A couple errors there

  1. struct can't compile if continuation isn't assigned,
  2. asyncOp.completed can't be set to continuation, parameter mismatch. It should be set to OnRequestCompleted (unless changing Action continuation to Action<AsyncOperation> continuation, but that wicked limits the callbacks you'd be able to apply,
  3. if continuation is null, it's possible it could be called while null, so OnRequestComplete should accommodate a nullcheck

Updated code (including library imports):

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

/// <summary>
/// Async awaitable UnityWebRequest <br/><br/>
/// Usage example: <br/><br/>
/// UnityWebRequest www = new UnityWebRequest(); <br/>
/// // do unitywebrequest setup here here... <br/>
/// await www.SendWebRequest(); <br/>
/// Debug.Log(req.downloadHandler.text); <br/>
/// </summary>
public struct UnityWebRequestAwaiter : INotifyCompletion
{
	private UnityWebRequestAsyncOperation asyncOp;
	private Action continuation;

	public UnityWebRequestAwaiter(UnityWebRequestAsyncOperation asyncOp)
	{
		this.asyncOp = asyncOp;
		continuation = null;
	}

	public bool IsCompleted { get { return asyncOp.isDone; } }

	public void GetResult() { }

	public void OnCompleted(Action continuation)
	{
		this.continuation = continuation;
		asyncOp.completed += OnRequestCompleted;
	}

	private void OnRequestCompleted(AsyncOperation obj)
	{
		continuation?.Invoke();
	}
}

public static class ExtensionMethods
{
	public static UnityWebRequestAwaiter GetAwaiter(this UnityWebRequestAsyncOperation asyncOp)
	{
		return new UnityWebRequestAwaiter(asyncOp);
	}
}

@streeter12
Copy link

streeter12 commented Aug 20, 2021

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