You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Unity Asset Store | [](https://assetstore.unity.com/packages/tools/asynchronous-operations-for-unity-96696)
8
+
Unity Asset Store | [](https://assetstore.unity.com/packages/tools/asynchronous-operations-for-unity-96696)
9
9
10
-
**Required Unity 5.4 or higher.**
10
+
**Requires Unity 5.4 or higher.**
11
11
12
12
**If you enjoy using the library - please, [rate and review](https://assetstore.unity.com/packages/tools/asynchronous-operations-for-unity-96696) it on the Asset Store!**
13
13
14
+
**Please ask any questions and leave feedback at the [Unity forums](https://forum.unity.com/threads/asynchronous-operations-for-unity-free.522989/).**
15
+
14
16
## Synopsis
15
17
16
18
*UnityFx.Async* introduces effective and portable asynchronous operations that can be used very much like [Tasks](https://docs.microsoft.com/en-us/dotnet/api/system.threading.tasks.task) in .NET or [Promises](https://developers.google.com/web/fundamentals/primers/promises) in JS. [AsyncResult](https://arvtesh.github.io/UnityFx.Async/api/netstandard2.0/UnityFx.Async.AsyncResult.html) class is an implementation of a generic asynchronous operation (aka `promise` or `future`). In many aspects it mimics [Task](https://docs.microsoft.com/en-us/dotnet/api/system.threading.tasks.task) (for example, it can be used with `async`/`await` operators, supports continuations and synchronization context capturing) while maintaining Unity/net35 compatibility. It is a great foundation toolset for any Unity project.
@@ -27,15 +29,17 @@ The table below summarizes differences berween *UnityFx.Async* and other popular
27
29
| Stat | UnityFx.Async |[C-Sharp-Promise](https://github.com/Real-Serious-Games/C-Sharp-Promise)|[TPL](https://docs.microsoft.com/en-us/dotnet/standard/parallel-programming/task-parallel-library-tpl)|
| Minimum operation data size for 32-bit systems (in bytes) | 32+ | 36+ | 40+ |
40
44
| Minimum number of allocations per continuation |~1 | 5+ | 2+ |
41
45
@@ -55,7 +59,7 @@ git submodule -q update --init
55
59
The binaries are available as a [NuGet package](https://www.nuget.org/packages/UnityFx.Async). See [here](http://docs.nuget.org/docs/start-here/using-the-package-manager-console) for instructions on installing a package via nuget. One can also download them directly from [Github releases](https://github.com/Arvtesh/UnityFx.Async/releases). Unity3d users can import corresponding [Unity Asset Store package](https://assetstore.unity.com/packages/tools/asynchronous-operations-for-unity-96696) using the editor.
56
60
57
61
### Unity dependencies
58
-
The library core (`UnityFx.Async.dll`) does not depend on Unity and can be used in any .NET projects (via assembly or [NuGet](https://www.nuget.org/packages/UnityFx.Async) reference). All Unity-specific stuff depends on the core and is included in [Unity Asset Store package](https://assetstore.unity.com/packages/tools/asynchronous-operations-for-unity-96696).
62
+
The library core (`UnityFx.Async.dll`) does not depend on Unity and can be used in any .NET project (via assembly or [NuGet](https://www.nuget.org/packages/UnityFx.Async) reference). All Unity-specific stuff depends on the core and is included in [Asset Store package](https://assetstore.unity.com/packages/tools/asynchronous-operations-for-unity-96696).
59
63
60
64
## Understanding the concepts
61
65
The topics below are just a quick summary of problems and the proposed solutions. For more details on the topic please see useful links at the end of this document.
@@ -439,7 +443,7 @@ Most common way of creating own asynchronous operation is instantiating `AsyncCo
439
443
*`AsyncResult`: an asynchronous operation without a result value.
440
444
*`AsyncResult<TResult>`: an asynchronous operation with a result value.
441
445
442
-
The sample code below demostrates creating a delay operation (in fact library provides one, this is just a simplified example):
446
+
The sample code below demostrates creating a delay operation (in fact the library provides one, this is just a simplified example):
443
447
```csharp
444
448
publicclassTimerDelayResult : AsyncResult
445
449
{
@@ -479,10 +483,9 @@ public class TimerDelayResult : AsyncResult
479
483
```
480
484
481
485
### Unity3d helpers
482
-
The library consists of 3 major parts:
486
+
As stated abovethe library include 2 main parts:
483
487
* Core tools (defined in `UnityFx.Async.dll` assembly, do not depend on Unity3d);
484
-
* Unity3d-specific tools (defined as a collection of C# scripts located in `Assets/Plugins/UnityFx.Async` if installed as an Asset Store package, require Unity3d to compile/execute).
485
-
* Unity3d samples (defined as a collection of C# scripts located in `Assets/UnityFx.Async` if installed as an Asset Store package, require Unity3d to compile/execute).
488
+
* Unity3d-specific tools (defined as a collection of C# scripts if installed as an Asset Store package, require Unity3d to compile/execute).
486
489
487
490
Everything described before (unless specified otherwise) does not require Unity and can be used in any application. The Unity-specific stuff is located in 3 classes:
488
491
*`AsyncUtility`. Defines helper methods for accessing main thread in Unity, running coroutines without actually using a `MonoBehaviour` and waiting for native Unity asynchronous operations outside of coroutines.
@@ -492,11 +495,11 @@ Everything described before (unless specified otherwise) does not require Unity
492
495
For example, one can throw a few lines of code to be executed on a main thread using:
493
496
```csharp
494
497
// Sends a delegate to the main thread and blocks calling thread until it is executed.
495
-
AsyncUtility.SendToMainThread(args=>Debug.Log("On the main thread."), null);
498
+
AsyncUtility.SendToMainThread(()=>Debug.Log("On the main thread."));
496
499
// Posts a delegate to the main thread and returns immediately. Returns an asynchronous operation that can be used to track the delegate execution.
497
-
AsyncUtility.PostToMainThread(args=>Debug.Log("On the main thread."), null);
500
+
AsyncUtility.PostToMainThread(()=>Debug.Log("On the main thread."));
498
501
// If calling thread is the main thread executes the delegate synchronously, otherwise posts it to the main thread. Returns an asynchronous operation that can be used to track the delegate execution.
499
-
AsyncUtility.InvokeOnMainThread(args=>Debug.Log("On the main thread."), null);
502
+
AsyncUtility.InvokeOnMainThread(()=>Debug.Log("On the main thread."));
500
503
```
501
504
502
505
## Comparison to .NET Tasks
@@ -516,15 +519,14 @@ TPL | UnityFx.Async | Notes
516
519
- | [IAsyncCancellable](https://arvtesh.github.io/UnityFx.Async/api/netstandard2.0/UnityFx.Async.IAsyncCancellable.html) | A cancellable operation.
517
520
- | [IAsyncContinuation](https://arvtesh.github.io/UnityFx.Async/api/netstandard2.0/UnityFx.Async.IAsyncContinuation.html) | A generic non-delegate operation continuation.
518
521
- | [IAsyncUpdatable](https://arvtesh.github.io/UnityFx.Async/api/netstandard2.0/UnityFx.Async.IAsyncUpdatable.html), [IAsyncUpdateSource](https://arvtesh.github.io/UnityFx.Async/api/netstandard2.0/UnityFx.Async.IAsyncUpdateSource.html) | A consumer and provider sides for frame update notifications.
519
-
- | [AsyncResultQueue<T>](https://arvtesh.github.io/UnityFx.Async/api/netstandard2.0/UnityFx.Async.AsyncResultQueue-1.html) | A FIFO queue of asynchronous operations executed sequentially.
520
522
521
523
Please note that the library is NOT a replacement for [Tasks](https://docs.microsoft.com/en-us/dotnet/api/system.threading.tasks.task) or [TPL](https://docs.microsoft.com/en-us/dotnet/standard/parallel-programming/task-parallel-library-tpl). As a general rule it is recommended to use [Tasks](https://docs.microsoft.com/en-us/dotnet/api/system.threading.tasks.task) and only switch to *UnityFx.Async* if one of the following applies:
522
524
- .NET 3.5/[Unity3d](https://unity3d.com) compatibility is required.
523
525
- Memory usage is a concern ([Tasks](https://docs.microsoft.com/en-us/dotnet/api/system.threading.tasks.task) tend to do quite a lot of allocations).
524
526
- An extendable [IAsyncResult](https://docs.microsoft.com/en-us/dotnet/api/system.iasyncresult) implementation is needed.
525
527
526
528
## Motivation
527
-
The project was initially created to help author with his [Unity3d](https://unity3d.com) projects. Unity's [AsyncOperation](https://docs.unity3d.com/ScriptReference/AsyncOperation.html) and similar can only be used in coroutines, cannot be extended and mostly do not return result or error information, .NET 3.5 does not provide much help either and even with .NET 4.6 support compatibility requirements often do not allow using [Tasks](https://docs.microsoft.com/en-us/dotnet/api/system.threading.tasks.task). When I caught myself writing the same asynchronous operation wrappers in each project I decided to share my experience to the best of human kind.
529
+
The project was initially created to help author with his [Unity3d](https://unity3d.com) projects. Unity's [AsyncOperation](https://docs.unity3d.com/ScriptReference/AsyncOperation.html) and similar can only be used in coroutines, cannot be extended and mostly do not return result or error information, .NET 3.5 does not provide much help either and even with .NET 4.6 support compatibility requirements often do not allow using [Tasks](https://docs.microsoft.com/en-us/dotnet/api/system.threading.tasks.task) (and they are quite expensive). When I caught myself writing the same asynchronous operation wrappers in each project I decided to share my experience to the best of human kind.
528
530
529
531
## Documentation
530
532
Please see the links below for extended information on the product:
@@ -554,7 +556,7 @@ The project uses [SemVer](https://semver.org/) versioning pattern. For the versi
554
556
Please see the [](LICENSE.md) for details.
555
557
556
558
## Acknowledgments
557
-
Working on this project is a great experience. Please see below list of sources of my inspiration (in no particular order):
559
+
Working on this project is a great experience. Please see below a list of my inspiration sources (in no particular order):
558
560
*[.NET reference source](https://referencesource.microsoft.com/mscorlib/System/threading/Tasks/Task.cs.html). A great source of knowledge and good programming practices.
559
561
*[C-Sharp-Promise](https://github.com/Real-Serious-Games/C-Sharp-Promise). Another great C# promise library with excellent documentation.
560
562
*[UniRx](https://github.com/neuecc/UniRx). A deeply reworked [Rx.NET](https://github.com/Reactive-Extensions/Rx.NET) port to Unity.
0 commit comments