Skip to content

Latest commit

 

History

History
133 lines (115 loc) · 5.94 KB

README.md

File metadata and controls

133 lines (115 loc) · 5.94 KB

UnityFx.Tasks

Channel UnityFx.Tasks
Github GitHub release
Npm Npm release
Unity Asset Store Task extensions for Unity

Requires Unity 2017.2 or higher.

Synopsis

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.

Getting Started

Prerequisites

You may need the following software installed in order to use the library:

  • Unity3d (the minimum supported version is 2017.2).

Getting the code

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

Getting binaries

The Unity Asset Store package can be installed using the editor. One can also download it directly from Github releases.

Npm package

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"
  }
} 

Using the library

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");

Motivation

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.

Documentation

Please see the links below for extended information on the product:

Useful links

Contributing

Please see contributing guide for details.

Versioning

The project uses SemVer versioning pattern. For the versions available, see tags in this repository.

License

Please see the license for details.

Acknowledgments

Working on this project is a great experience. Please see below a list of my inspiration sources (in no particular order):