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
10
## Synopsis
11
11
12
-
*UnityFx.Async*is a set of classes and interfaces that extend [Unity3d](https://unity3d.com)asynchronous operations and can be used very much like [Task-based Asynchronous Pattern (TAP)](https://docs.microsoft.com/en-us/dotnet/standard/parallel-programming/task-based-asynchronous-programming) in .NET or [Promises](https://developers.google.com/web/fundamentals/primers/promises) in Javascript. The library at its core defines a container ([AsyncResult](https://arvtesh.github.io/UnityFx.Async/api/netstandard2.0/UnityFx.Async.AsyncResult.html)) for state and result value of an 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).
12
+
*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.
13
13
14
14
Library is designed as a lightweight [Unity3d](https://unity3d.com)-compatible [Tasks](https://docs.microsoft.com/en-us/dotnet/api/system.threading.tasks.task) alternative (not a replacement though). Main design goals are:
15
15
- Minimum object size and number of allocations.
16
-
- Extensibility. The library operations are designed to be inherited.
16
+
- Extensibility. The library entities are designed to be easily extensible.
17
17
- Thread-safe. The library classes can be safely used from different threads (unless explicitly stated otherwise).
18
18
-[Task](https://docs.microsoft.com/en-us/dotnet/api/system.threading.tasks.task)-like interface and behaviour. In many cases library classes can be used much like corresponding [TPL](https://docs.microsoft.com/en-us/dotnet/standard/parallel-programming/task-parallel-library-tpl) entities.
19
-
-[Unity3d](https://unity3d.com)compatibility. This includes possibility to <c>yield</c> operations in coroutines and net35-compilance.
19
+
-[Unity3d](https://unity3d.com)-specific features and compatibility. This includes possibility to <c>yield</c> operations in coroutines, `net35`-compilance, extensions of Unity asynchronous operations etc.
20
20
21
21
The table below summarizes differences berween *UnityFx.Async* and other popular asynchronous operation frameworks:
22
22
@@ -33,7 +33,7 @@ The table below summarizes differences berween *UnityFx.Async* and other popular
33
33
| Supports progress reporting | ✔️ | ✔️ | ✔️ |
34
34
| Supports child operations | - | - | ✔️ |
35
35
| Minimum operation data size for 32-bit systems (in bytes) | 28+ | 36+ | 40+ |
36
-
| Minimum number of allocations per continuation |1+| 5+ | 2+ |
36
+
| Minimum number of allocations per continuation |~1| 5+ | 2+ |
37
37
38
38
## Getting Started
39
39
### Prerequisites
@@ -166,9 +166,9 @@ Create an operation instance like this:
166
166
```csharp
167
167
varop=newAsyncCompletionSource<string>();
168
168
```
169
-
The type of the operation should reflect its result type. In this case we have created a special kind of operation - a completion source that incapsulated both producer and consumer interfaces (consumer side is represented via `IAsyncOperation` / `IAsyncOperation<TResult>` interfaces and producer side is `IAsyncCompletionSource` / `IAsyncCompletionSource<TResult>`, `AsyncCompletionSource` implements both of the interfaces).
169
+
The type of the operation should reflect its result type. In this case we create a special kind of operation - a completion source, that incapsulates both producer and consumer interfaces (consumer side is represented via `IAsyncOperation` / `IAsyncOperation<TResult>` interfaces and producer side is `IAsyncCompletionSource` / `IAsyncCompletionSource<TResult>`, `AsyncCompletionSource` implements both of the interfaces).
170
170
171
-
While operation is running its progress can be set as:
171
+
While operation is running its progress can be set like this:
If the [token](https://docs.microsoft.com/en-us/dotnet/api/system.threading.cancellationtoken) passed to `WithCancellation()` is cancelled the target operation is cancelled as well (and that means cancelling all chained operations) as soon as possible. Cancellation might not be instant (depends on specific operation implementation). Also, please note that not all operations might support cancellation; in this case `Cancel()` will throw `NotSupportedException`.
335
335
336
336
### Progress reporting
337
-
Library operations support progress reporting via exposing `IAsyncOperation.Progress` property:
337
+
Library operations support progress reporting via exposing `IAsyncOperation.Progress` property and progress reporting events:
338
338
```csharp
339
339
varprogress=op.Progess; // gets an operation progress as a float value in range [0, 1]
340
+
341
+
// subscribe to progress changed event
342
+
op.ProgressChanged+= (sender, args) =>
343
+
{
344
+
Debug.Log("Progress = "+args.ProgressPercentage);
345
+
}
346
+
347
+
// add progress changed delegate
348
+
op.AddProgressCallback(op =>
349
+
{
350
+
Debug.Log("Progress = "+op.Progress);
351
+
});
340
352
```
341
353
There is `AsyncResult.GetProgress()` virtual method that is called when a progress values is requested. Finally there are producer-side methods like `AsyncCompletionSource.TrySetProgress()` that can set the progress value.
342
354
@@ -355,7 +367,7 @@ Completion callbacks are basicly low-level continuations. Just like continuation
355
367
```csharp
356
368
varop=DownloadTextAsync("http://www.google.com");
357
369
op.Completed+=o=>Debug.Log("1");
358
-
op.AddContinuation(o=>Debug.Log("2"));
370
+
op.AddCompletionCallback(o=>Debug.Log("2"));
359
371
```
360
372
That said, unlike `ContinueWith()`-like stuff completion callbacks cannot be chained and do not handle exceptions automatically. Throwing an exception from a completion callback results in unspecified behavior.
361
373
@@ -369,7 +381,7 @@ class MyContinuation : IAsyncContinuation
0 commit comments