Skip to content

Commit

Permalink
Merge pull request #654 from hadashiA/ku/fix-root-in-editor2
Browse files Browse the repository at this point in the history
Fix RootLifetimeScope
  • Loading branch information
hadashiA authored Mar 31, 2024
2 parents 634d03a + a75f817 commit d6d8633
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion VContainer/Assets/VContainer/Runtime/Unity/VContainerSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,23 @@ 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)
{
instance.OnDisable();
instance.OnEnable();
}
}

[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
static void RuntimeInitialize()
{
// For editor, we need to load the Preload asset manually.
LoadInstanceFromPreloadAssets();
}
#endif

public LifetimeScope GetOrCreateRootLifetimeScopeInstance()
Expand Down Expand Up @@ -88,13 +105,18 @@ void OnEnable()
}
}

void OnDisable()
{
Instance = null;
}

void OnFirstSceneLoaded(Scene scene, LoadSceneMode mode)
{
if (RootLifetimeScope != null &&
RootLifetimeScope.autoRun &&
(rootLifetimeScopeInstance == null || rootLifetimeScopeInstance.Container == null))
{
GetOrCreateRootLifetimeScopeInstance().Build();
GetOrCreateRootLifetimeScopeInstance();
}
SceneManager.sceneLoaded -= OnFirstSceneLoaded;
}
Expand Down

0 comments on commit d6d8633

Please sign in to comment.