Use of UniTask

If you have installed UniTask, the ToUniTask method will be available.

Supported versions

UniTask Ver.2.0.32 or later

Preparation for use

When imported from Package Manager

It will be activated automatically.
No special preparation is required.

When importing unity package

Add ARBOR_SUPPORT_UNITASK to Scripting Define Symbols in Player Settings.

Assembly definitions

If you are partitioning your assembly using Assembly definitions, you need to add a reference to Arbor.UniTask.asmdef.

If necessary, add a reference to “Assets/Plugins/Arbor/External/UniTask/Arbor.UniTask.asmdef” to the list of Assembly Definition References.

See Assembly definitions for details on how to configure.

See also here for the basic assembly definition of Arbor.

ToUniTask(YieldAwaitable)

public static UniTask ToUniTask(this YieldAwaitable awaitable) ;

Description

Convert YieldAwaitable to UniTask type. You also need to add using Arbor.Threading.Tasks; to use it.

Parameters

Parameter Name Description
awaitable YieldAwaitable to convert

Returns

UniTask of the conversion result.

Example of use

This is an example of implementing async / await usage example in UniTask.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
using UnityEngine;
using Cysharp.Threading.Tasks;
using Arbor;
using Arbor.Threading.Tasks;

[AddComponentMenu("")]
public class ExampleAwaitBehaviourUniTask : StateBehaviour
{
	public StateLink nextState;

	async UniTask WaitFrame(int frameCount)
	{
		try
		{
			int startFrame = Time.frameCount;

			for (int i = 0; i < frameCount; i++)
			{
				await Yield().ToUniTask();
			}

			Debug.Log($"{Time.frameCount - startFrame}");

			Transition(nextState);
		}
		catch (System.Exception ex)
		{
			if (ex is System.OperationCanceledException)
			{
				return;
			}

			Debug.LogException(ex);
		}
	}

	public override void OnStateBegin() 
	{
		WaitFrame(10).Forget();
	}
}