Skip to content

Instantly share code, notes, and snippets.

@kazukitanaka0611
Created December 24, 2020 00:59
Show Gist options
  • Save kazukitanaka0611/4d152df2dba93557196ef0f2dfafe602 to your computer and use it in GitHub Desktop.
Save kazukitanaka0611/4d152df2dba93557196ef0f2dfafe602 to your computer and use it in GitHub Desktop.
This is SceneLoader with Param by VContainer and UniTask
using System.Threading;
using Cysharp.Threading.Tasks;
using UnityEngine.SceneManagement;
using VContainer.Unity;
public class SceneLoader
{
public static async UniTask LoadSceneAsync<T, U>(string secneName, U param, LoadSceneMode mode = LoadSceneMode.Single)
where T : LifetimeScope
{
CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
try
{
// string sceneName = UnityEngine.SceneManagement.SceneManager.GetSceneAt(i).name;
await SceneManager.LoadSceneAsync(secneName, mode);
cancellationTokenSource.Token.ThrowIfCancellationRequested();
T scope = LifetimeScope.Find<T>() as T;
if (param != null)
{
LifetimeScope.Enqueue(builder =>
{
builder.RegisterInstance(param);
});
}
scope.Build();
}
catch (System.OperationCanceledException)
{
cancellationTokenSource.Cancel();
throw;
}
finally
{
cancellationTokenSource.Dispose();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment