Skip to content

Commit

Permalink
Merge pull request #418 from hadashiA/ku/fix-root-configure-timing
Browse files Browse the repository at this point in the history
Fix root startup timing due to undefined behavior before scene load
  • Loading branch information
hadashiA committed Sep 26, 2022
2 parents 364b502 + ee4fc3e commit 030c13e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
Expand Up @@ -48,4 +48,4 @@ public object SpawnInstance(IObjectResolver resolver)
return component;
}
}
}
}
31 changes: 25 additions & 6 deletions VContainer/Assets/VContainer/Runtime/Unity/VContainerSettings.cs
@@ -1,5 +1,6 @@
using System.Linq;
using UnityEngine;
using UnityEngine.SceneManagement;

namespace VContainer.Unity
{
Expand Down Expand Up @@ -71,24 +72,42 @@ void OnEnable()
if (RootLifetimeScope != null)
{
RootLifetimeScope.IsRoot = true;
if (RootLifetimeScope.Container == null && RootLifetimeScope.autoRun)
{
RootLifetimeScope.Build();
}
}

Instance = this;

var activeScene = SceneManager.GetActiveScene();
if (activeScene.isLoaded)
{
OnFirstSceneLoaded(activeScene, default);
}
else
{
SceneManager.sceneLoaded -= OnFirstSceneLoaded;
SceneManager.sceneLoaded += OnFirstSceneLoaded;
}

Application.quitting -= OnApplicationQuit;
Application.quitting += OnApplicationQuit;
}
}

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

void OnApplicationQuit()
{
if (RootLifetimeScope != null)
{
var container = RootLifetimeScope.Container;
if (container != null)
if (RootLifetimeScope.Container != null)
{
// Execute Dispose once at the slowest possible time.
// However, the GameObject may be destroyed at that time.
Expand Down

1 comment on commit 030c13e

@vercel
Copy link

@vercel vercel bot commented on 030c13e Sep 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.