From 75b922afa3920398f979dd4227c5813c63817cc5 Mon Sep 17 00:00:00 2001 From: hadashiA Date: Fri, 29 Mar 2024 08:44:16 +0900 Subject: [PATCH] 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()