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()