Created
December 24, 2020 00:59
-
-
Save kazukitanaka0611/4d152df2dba93557196ef0f2dfafe602 to your computer and use it in GitHub Desktop.
This is SceneLoader with Param by VContainer and UniTask
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
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