-
-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[website] Add docs about UniTask, ExceptionHandler
- Loading branch information
Showing
24 changed files
with
294 additions
and
466 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes
2 changes: 1 addition & 1 deletion
2
website/docs/integration/ecs.mdx → website/docs/integrations/ecs.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
--- | ||
title: UniRx | ||
--- | ||
|
||
[UniRx](https://github.com/neuecc/UniRx) is an Rx (Reactive Extension) library optimized for Unity. | ||
|
||
Here is an example of combining VContainer entry points with UniRx. | ||
|
||
```csharp | ||
public class FooController : IStartable, IDisposable | ||
{ | ||
readonly CompositeDisposable disposable = new CompositeDisposable; | ||
|
||
void IStartable.Start() | ||
{ | ||
fooObservable | ||
.Subscribe(_ => /* Do something */) | ||
.AddTo(disposable) | ||
} | ||
|
||
void IDisposable.Dispose() => disposable.Dispose(); | ||
} | ||
``` | ||
|
||
If you want to create a short hand like `AddTo(this)`, you can create an extension method like this: | ||
```csharp | ||
public class ControllerBase : IDisposable | ||
{ | ||
readonly CompositeDisposable disposable = new CompositeDisposable; | ||
|
||
void IDisposable.Dispose() => disposable.Dispose(); | ||
|
||
public void AddDisposable(IDisposable disposable) | ||
{ | ||
disposables.Add(disposable); | ||
} | ||
} | ||
|
||
public static class DisposableExtensions | ||
{ | ||
public static T AddTo<T>(this T disposable, Heipu.Presentation.Controllers.ControllerBase controller) | ||
where T: IDisposable | ||
{ | ||
controller.AddDisposable(disposable); | ||
return disposable; | ||
} | ||
} | ||
``` | ||
|
||
This can be used as follows: | ||
|
||
```csharp | ||
public class FooController : ControllerBase, IStartable | ||
{ | ||
void IStartable.Start() | ||
{ | ||
someObservable | ||
.Subscribe(...) | ||
.AddTo(this); | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
--- | ||
title: UniTask | ||
--- | ||
|
||
[UniTask](https://github.com/Cysharp/UniTask) is a library that brings fast and powerful async/await to the Unity world. | ||
|
||
If you have the `com.cysharp.unitask` package installed in your project, the following features will be enabled. | ||
|
||
## IAsyncStartable | ||
|
||
We can create an entry point for the async UniTask. | ||
|
||
```csharp | ||
public class FooController : IAsyncStartable | ||
{ | ||
public async UniTask StartAsync(CancellationToken cancellation) | ||
{ | ||
await LoadSomethingAsync(cancellation); | ||
await ... | ||
... | ||
} | ||
} | ||
``` | ||
|
||
By creating a class like the one above and Registering it, the UniTask of StartAsync will be executed automatically. | ||
|
||
```csharp | ||
builder.RegisterEntryPoint<FooController>(Lifetime.Scoped); | ||
``` | ||
|
||
For more information about RegisterEntryPoint, please check [here](../integrations/entrypoint). | ||
|
||
### StartAsync() timing | ||
|
||
The only async interface provided by VContainer is IAsyncStartable. | ||
UniTask can control the execution timing within the async method. | ||
|
||
If you want to schedule the process at different times, you can use UniTask's PlayerLoopTiming. | ||
|
||
```csharp | ||
await UniTask.Yield(PlayerLoopTiming.FixedUpdate); | ||
``` | ||
|
||
See https://github.com/Cysharp/UniTask#playerloop | ||
|
||
### Exception Handling | ||
|
||
- Within the async method, try/catch can be used. | ||
- We can use `UniTaskScheduler.UnobservedTaskException` event. | ||
- Alternatively, if you want to register a separate error handler for each LifetimeScope, use: | ||
- `builder.RegisterEntryPointExceptionHandler(ex => ..)` | ||
- See [Entry point](./entrypoint) section for more information. | ||
|
||
### CancellationToken | ||
|
||
The argument of StartAsync will be a cancellationToken. | ||
|
||
This cancellationToken will be canceled when the LifetimeScope is destroyed. | ||
|
||
### Async asset loading | ||
|
||
UniTask supports the ability to load assets and scenes asynchronously. | ||
This is useful when used with LifetimeScope.Enqueue* in VContainer. | ||
|
||
```csharp | ||
var extraAsset = await Addressables.LoadAssetAsync<ExtraAsset>(key); | ||
|
||
using (LifetimeScope.EnqueueParent(parentScope)) | ||
using (LifetimeScope.Enqueue(builder => builder.RegisterInstance(extraAsset)) | ||
{ | ||
await SceneManager.LoadSceneAsync("AdditiveScene"); | ||
} | ||
``` | ||
|
||
See [Scoping](../scoping/generate-child-via-scene) secion for more information. |
File renamed without changes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.