Channel | UnityFx.Tasks |
---|---|
Github | |
Npm | |
Unity Asset Store |
Requires Unity 2017.2 or higher.
At this moment Unity3d does not provide support neither for Tasks nor for async/await. The goal of the library is closing this gap and make async/await usage in Unity3d a viable option.
You may need the following software installed in order to use the library:
- Unity3d (the minimum supported version is 2017.2).
You can get the code by cloning the github repository using your preffered git client UI or you can do it from command line as follows:
git clone https://github.com/Arvtesh/UnityFx.Tasks.git
git submodule -q update --init
The Unity Asset Store package can be installed using the editor. One can also download it directly from Github releases.
Npm package is available at npmjs.com. To use it, add the following line to dependencies section of your manifest.json
. Unity should download and link the package automatically:
{
"scopedRegistries": [
{
"name": "Arvtesh",
"url": "https://registry.npmjs.org/",
"scopes": [
"com.unityfx"
]
}
],
"dependencies": {
"com.unityfx.tasks": "0.2.0"
}
}
The library tools are locates in a single namespace:
using UnityFx.Tasks;
An essential part of the code is implementation of awaiters for built-in Unity async operations:
await new WaitForSeconds(1);
await UnityWebRequestAssetBundle.GetAssetBundle(url);
await StartCoroutine(SomeCoroutine());
await SceneManager.LoadSceneAsync("myScene");
There are also Task conversions for standard operations:
var task = UnityWebRequestAssetBundle.GetAssetBundle(url).ToTask<AssetBundle>();
var assetBundle = await task;
There are a number of ConfigureAwait
extensions that allow for await
configuration. It is more effective to use them instead of ToTask
conversions when all you need is an awaitable object:
public static async Task<AssetBundle> LoadAssetBundleAsync(string url)
{
using (var www = UnityWebRequestAssetBundle.GetAssetBundle(url))
{
return await www.SendWebRequest().ConfigureAwait<AssetBundle>();
}
}
There are a lot of utility methods provided. For instance, the following sample demonstrates loading a scene packed in an asset bundle:
using System;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.Networking;
using UnityFx.Tasks;
public static async Task<Scene> LoadSceneFromAssetBundleAsync(string url)
{
using (var www = UnityWebRequestAssetBundle.GetAssetBundle(url))
{
var assetBundle = await www.SendWebRequest().ConfigureAwait<AssetBundle>();
try
{
return await assetBundle.LoadSceneTaskAsync(LoadSceneMode.Single);
}
finally
{
assetBundle.Unload(false);
}
}
}
A scene can be loaded like this:
var scene = await TaskUtility.LoadSceneAsync("myScene");
The project was initially created to help author with his Unity3d projects. Unity doesn't adapt their APIs to the async/await programming and that requires additional effors to make it usable. Having experience with that kind of stuff with UnityFx.Async I decided to make a very minimal set of tools just for this purpose.
Please see the links below for extended information on the product:
- Task-based Asynchronous Pattern (TAP).
- Asynchronous programming with async and await (C#).
- .NET Task reference source.
Please see contributing guide for details.
The project uses SemVer versioning pattern. For the versions available, see tags in this repository.
Working on this project is a great experience. Please see below a list of my inspiration sources (in no particular order):
- .NET reference source. A great source of knowledge and good programming practices.
- Another Unity async/await helpers project.
- Everyone who ever commented or left any feedback on the project. It's always very helpful.