From e2355d69c7d49e26405678b62b2abbe997243eea Mon Sep 17 00:00:00 2001 From: hadashiA Date: Fri, 29 Mar 2024 08:42:11 +0900 Subject: [PATCH 1/3] Fix a bug that LifetimeScope configure twice --- .../Assets/VContainer/Runtime/Unity/VContainerSettings.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/VContainer/Assets/VContainer/Runtime/Unity/VContainerSettings.cs b/VContainer/Assets/VContainer/Runtime/Unity/VContainerSettings.cs index 71bcf085..9da75cfd 100644 --- a/VContainer/Assets/VContainer/Runtime/Unity/VContainerSettings.cs +++ b/VContainer/Assets/VContainer/Runtime/Unity/VContainerSettings.cs @@ -94,7 +94,7 @@ void OnFirstSceneLoaded(Scene scene, LoadSceneMode mode) RootLifetimeScope.autoRun && (rootLifetimeScopeInstance == null || rootLifetimeScopeInstance.Container == null)) { - GetOrCreateRootLifetimeScopeInstance().Build(); + GetOrCreateRootLifetimeScopeInstance(); } SceneManager.sceneLoaded -= OnFirstSceneLoaded; } From 75b922afa3920398f979dd4227c5813c63817cc5 Mon Sep 17 00:00:00 2001 From: hadashiA Date: Fri, 29 Mar 2024 08:44:16 +0900 Subject: [PATCH 2/3] Revert "Merge pull request #650 from hadashiA/ku/fix-root-in-editor" This reverts commit 5bea2259c33eaa9eb72515e8f411410a1162fe7a, reversing changes made to d64a49a0a2a31b60e84caf0122155200b3c0e9d4. --- .../Runtime/Unity/VContainerSettings.cs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/VContainer/Assets/VContainer/Runtime/Unity/VContainerSettings.cs b/VContainer/Assets/VContainer/Runtime/Unity/VContainerSettings.cs index 9da75cfd..ca3b48a4 100644 --- a/VContainer/Assets/VContainer/Runtime/Unity/VContainerSettings.cs +++ b/VContainer/Assets/VContainer/Runtime/Unity/VContainerSettings.cs @@ -48,6 +48,24 @@ public static void CreateAsset() preloadedAssets.Add(newSettings); UnityEditor.PlayerSettings.SetPreloadedAssets(preloadedAssets.ToArray()); } + + public static void LoadInstanceFromPreloadAssets() + { + var preloadAsset = UnityEditor.PlayerSettings.GetPreloadedAssets().FirstOrDefault(x => x is VContainerSettings); + if (preloadAsset is VContainerSettings instance) + { + if (instance.RootLifetimeScope != null) + instance.RootLifetimeScope.DisposeCore(); + instance.OnEnable(); + } + } + + [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)] + static void RuntimeInitialize() + { + // For editor, we need to load the Preload asset manually. + LoadInstanceFromPreloadAssets(); + } #endif public LifetimeScope GetOrCreateRootLifetimeScopeInstance() From a75f817803cf4e4ae669f36a8686a69f7f7c2001 Mon Sep 17 00:00:00 2001 From: hadashiA Date: Fri, 29 Mar 2024 08:57:22 +0900 Subject: [PATCH 3/3] Fix condition when Preload asset is enabled --- .../Assets/VContainer/Runtime/Unity/VContainerSettings.cs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/VContainer/Assets/VContainer/Runtime/Unity/VContainerSettings.cs b/VContainer/Assets/VContainer/Runtime/Unity/VContainerSettings.cs index ca3b48a4..42ca3481 100644 --- a/VContainer/Assets/VContainer/Runtime/Unity/VContainerSettings.cs +++ b/VContainer/Assets/VContainer/Runtime/Unity/VContainerSettings.cs @@ -54,8 +54,7 @@ public static void LoadInstanceFromPreloadAssets() var preloadAsset = UnityEditor.PlayerSettings.GetPreloadedAssets().FirstOrDefault(x => x is VContainerSettings); if (preloadAsset is VContainerSettings instance) { - if (instance.RootLifetimeScope != null) - instance.RootLifetimeScope.DisposeCore(); + instance.OnDisable(); instance.OnEnable(); } } @@ -106,6 +105,11 @@ void OnEnable() } } + void OnDisable() + { + Instance = null; + } + void OnFirstSceneLoaded(Scene scene, LoadSceneMode mode) { if (RootLifetimeScope != null &&