From 94ac6209582a4b5ceffccd44b92dc4a2f05777f1 Mon Sep 17 00:00:00 2001 From: hadashi Date: Sat, 13 Feb 2021 21:58:22 +0900 Subject: [PATCH 1/3] Support domain reload disable mode --- .../Runtime/Unity/LifetimeScope.AwakeScheduler.cs | 4 ++++ .../Assets/VContainer/Runtime/Unity/LifetimeScope.cs | 2 ++ .../VContainer/Runtime/Unity/PlayerLoopHelper.cs | 11 ++++------- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/VContainer/Assets/VContainer/Runtime/Unity/LifetimeScope.AwakeScheduler.cs b/VContainer/Assets/VContainer/Runtime/Unity/LifetimeScope.AwakeScheduler.cs index 2496b5de..f1012886 100644 --- a/VContainer/Assets/VContainer/Runtime/Unity/LifetimeScope.AwakeScheduler.cs +++ b/VContainer/Assets/VContainer/Runtime/Unity/LifetimeScope.AwakeScheduler.cs @@ -22,7 +22,11 @@ partial class LifetimeScope static readonly List<(LifetimeScope, VContainerParentTypeReferenceNotFound)> WaitingList = new List<(LifetimeScope, VContainerParentTypeReferenceNotFound)>(); +#if UNITY_2019_3_OR_NEWER + [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)] +#else [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)] +#endif static void SubscribeSceneEvents() { SceneManager.sceneLoaded -= AwakeWaitingChildren; diff --git a/VContainer/Assets/VContainer/Runtime/Unity/LifetimeScope.cs b/VContainer/Assets/VContainer/Runtime/Unity/LifetimeScope.cs index 6cb596e1..7a8ec7e3 100644 --- a/VContainer/Assets/VContainer/Runtime/Unity/LifetimeScope.cs +++ b/VContainer/Assets/VContainer/Runtime/Unity/LifetimeScope.cs @@ -256,6 +256,7 @@ LifetimeScope GetRuntimeParent() if (parentReference.Object != null) return parentReference.Object; + // Find in scene via type if (parentReference.Type != null && parentReference.Type != GetType()) { var found = Find(parentReference.Type); @@ -268,6 +269,7 @@ LifetimeScope GetRuntimeParent() $"{name} could not found parent reference of type : {parentReference.Type}"); } + // Find root from settings if (VContainerSettings.Instance is VContainerSettings settings) { if (settings.RootLifetimeScope != null) diff --git a/VContainer/Assets/VContainer/Runtime/Unity/PlayerLoopHelper.cs b/VContainer/Assets/VContainer/Runtime/Unity/PlayerLoopHelper.cs index 8374ea37..653689db 100644 --- a/VContainer/Assets/VContainer/Runtime/Unity/PlayerLoopHelper.cs +++ b/VContainer/Assets/VContainer/Runtime/Unity/PlayerLoopHelper.cs @@ -1,5 +1,5 @@ using System; -using UnityEngine; +using System.Threading; #if UNITY_2019_3_OR_NEWER using UnityEngine.LowLevel; using UnityEngine.PlayerLoop; @@ -42,16 +42,13 @@ enum PlayerLoopTiming static class PlayerLoopHelper { static readonly PlayerLoopRunner[] Runners = new PlayerLoopRunner[10]; - static bool initialized; + static long initialized; // [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)] public static void Initialize() { - lock (Runners) - { - if (initialized) return; - initialized = true; - } + if (Interlocked.CompareExchange(ref initialized, 1, 0) != 0) + return; for (var i = 0; i < Runners.Length; i++) { From 7e2759d61b1d9d20e2f406aee9e4ec31e538af3d Mon Sep 17 00:00:00 2001 From: hadashi Date: Sun, 14 Feb 2021 00:58:26 +0900 Subject: [PATCH 2/3] Add error message when domain reload is disabled and preload assets field is missing --- .../VContainer/Runtime/Unity/LifetimeScope.cs | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/VContainer/Assets/VContainer/Runtime/Unity/LifetimeScope.cs b/VContainer/Assets/VContainer/Runtime/Unity/LifetimeScope.cs index 7a8ec7e3..3a6e2897 100644 --- a/VContainer/Assets/VContainer/Runtime/Unity/LifetimeScope.cs +++ b/VContainer/Assets/VContainer/Runtime/Unity/LifetimeScope.cs @@ -272,13 +272,24 @@ LifetimeScope GetRuntimeParent() // Find root from settings if (VContainerSettings.Instance is VContainerSettings settings) { - if (settings.RootLifetimeScope != null) + var rootLifetimeScope = settings.RootLifetimeScope; +#if UNITY_EDITOR + var disableDomainReloading = UnityEditor.EditorSettings.enterPlayModeOptionsEnabled && + (UnityEditor.EditorSettings.enterPlayModeOptions & + UnityEditor.EnterPlayModeOptions.DisableDomainReload) > 0; + if (rootLifetimeScope == null && disableDomainReloading) { - if (settings.RootLifetimeScope.Container == null) + var path = UnityEditor.AssetDatabase.GetAssetPath(rootLifetimeScope); + UnityEngine.Debug.LogError($"VContainerSettings.RootLifetimeScope is missing : {path}. Please try to re-compile C# scripts just once."); + } +#endif + if (rootLifetimeScope != null) + { + if (rootLifetimeScope.Container == null) { - settings.RootLifetimeScope.Build(); + rootLifetimeScope.Build(); } - return settings.RootLifetimeScope; + return rootLifetimeScope; } } return null; From e549b6d130e0c497b3d55736bca0c50fce313a21 Mon Sep 17 00:00:00 2001 From: hadashiA Date: Sun, 14 Feb 2021 01:01:28 +0900 Subject: [PATCH 3/3] Fix error message --- VContainer/Assets/VContainer/Runtime/Unity/LifetimeScope.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/VContainer/Assets/VContainer/Runtime/Unity/LifetimeScope.cs b/VContainer/Assets/VContainer/Runtime/Unity/LifetimeScope.cs index 3a6e2897..74568919 100644 --- a/VContainer/Assets/VContainer/Runtime/Unity/LifetimeScope.cs +++ b/VContainer/Assets/VContainer/Runtime/Unity/LifetimeScope.cs @@ -279,8 +279,7 @@ LifetimeScope GetRuntimeParent() UnityEditor.EnterPlayModeOptions.DisableDomainReload) > 0; if (rootLifetimeScope == null && disableDomainReloading) { - var path = UnityEditor.AssetDatabase.GetAssetPath(rootLifetimeScope); - UnityEngine.Debug.LogError($"VContainerSettings.RootLifetimeScope is missing : {path}. Please try to re-compile C# scripts just once."); + UnityEngine.Debug.LogError("VContainerSettings.RootLifetimeScope is missing. Please try to re-compile C# scripts just once."); } #endif if (rootLifetimeScope != null)