diff --git a/VContainer/Assets/VContainer/Runtime/Annotations/IAsyncStartable.cs b/VContainer/Assets/VContainer/Runtime/Annotations/IAsyncStartable.cs index bd9fdbac..ef2a2b0a 100644 --- a/VContainer/Assets/VContainer/Runtime/Annotations/IAsyncStartable.cs +++ b/VContainer/Assets/VContainer/Runtime/Annotations/IAsyncStartable.cs @@ -9,4 +9,38 @@ public interface IAsyncStartable UniTask StartAsync(CancellationToken cancellation); } } +#elif UNITY_2023_1_OR_NEWER +using System; +using System.Threading; +using UnityEngine; + +namespace VContainer.Unity +{ + public interface IAsyncStartable + { + Awaitable StartAsync(CancellationToken cancellation); + } + + static class AwaitableHelper + { + public static async Awaitable Forget(Awaitable awaitable, EntryPointExceptionHandler exceptionHandler) + { + try + { + await awaitable; + } + catch (Exception ex) + { + if (exceptionHandler != null) + { + exceptionHandler.Publish(ex); + } + else + { + throw; + } + } + } + } +} #endif diff --git a/VContainer/Assets/VContainer/Runtime/Unity/PlayerLoopItem.cs b/VContainer/Assets/VContainer/Runtime/Unity/PlayerLoopItem.cs index d8dab74d..f9b6ecdd 100644 --- a/VContainer/Assets/VContainer/Runtime/Unity/PlayerLoopItem.cs +++ b/VContainer/Assets/VContainer/Runtime/Unity/PlayerLoopItem.cs @@ -1,7 +1,7 @@ using System; using System.Collections.Generic; -#if VCONTAINER_UNITASK_INTEGRATION using System.Threading; +#if VCONTAINER_UNITASK_INTEGRATION using Cysharp.Threading.Tasks; #endif @@ -293,7 +293,7 @@ public bool MoveNext() public void Dispose() => disposed = true; } -#if VCONTAINER_UNITASK_INTEGRATION +#if VCONTAINER_UNITASK_INTEGRATION || UNITY_2023_1_OR_NEWER sealed class AsyncStartableLoopItem : IPlayerLoopItem, IDisposable { readonly IEnumerable entries; @@ -315,10 +315,14 @@ public bool MoveNext() foreach (var x in entries) { var task = x.StartAsync(cts.Token); +#if VCONTAINER_UNITASK_INTEGRATION if (exceptionHandler != null) task.Forget(ex => exceptionHandler.Publish(ex)); else task.Forget(); +#else + var _ = AwaitableHelper.Forget(task, exceptionHandler); +#endif } return false; } diff --git a/website/docs/integrations/entrypoint.mdx b/website/docs/integrations/entrypoint.mdx index 9101fbcd..93d036e6 100644 --- a/website/docs/integrations/entrypoint.mdx +++ b/website/docs/integrations/entrypoint.mdx @@ -53,6 +53,14 @@ And [Unity - Manual: Order of Execution for Event Functions](https://docs.unity3d.com/Manual/ExecutionOrder.html) ::: +### async + +`IAsyncStartable` is available as a variant of IStartable. +It has the same timing as IStartable, but `async Awaitable StartAsync()` is available. + +If you are a UniTask user, you can also choose the UniTask version of StartAsync. +[UniTask integration](../integrations/unitask) + ## Handle of the exception that was not caught On the application side, exceptions thrown in processes such as Start() and Tick() cannot be caught outside.