diff --git a/VContainer/Assets/VContainer/Runtime/Registry.cs b/VContainer/Assets/VContainer/Runtime/Registry.cs index 7117a6a6..f348ff8f 100644 --- a/VContainer/Assets/VContainer/Runtime/Registry.cs +++ b/VContainer/Assets/VContainer/Runtime/Registry.cs @@ -46,7 +46,7 @@ public static Registry Build(Registration[] registrations) static void AddToBuildBuffer(IDictionary buf, Type service, Registration registration) { - if (buf.TryGetValue(service, out var exists)) + if (buf.TryGetValue(service, out var exists) && exists != null) { CollectionInstanceProvider collection; if (buf.TryGetValue(RuntimeTypeCache.EnumerableTypeOf(service), out var found) && diff --git a/VContainer/Assets/VContainer/Runtime/Unity/InstanceProviders/PrefabComponentProvider.cs b/VContainer/Assets/VContainer/Runtime/Unity/InstanceProviders/PrefabComponentProvider.cs index 8a4e0422..b6620316 100644 --- a/VContainer/Assets/VContainer/Runtime/Unity/InstanceProviders/PrefabComponentProvider.cs +++ b/VContainer/Assets/VContainer/Runtime/Unity/InstanceProviders/PrefabComponentProvider.cs @@ -27,37 +27,37 @@ public object SpawnInstance(IObjectResolver resolver) { var prefab = prefabFinder(resolver); var parent = destination.GetParent(resolver); - + var wasActive = prefab.gameObject.activeSelf; - using var dirtyScope = new ObjectResolverUnityExtensions.PrefabDirtyScope(prefab.gameObject); - - if (wasActive) + using (new ObjectResolverUnityExtensions.PrefabDirtyScope(prefab.gameObject)) { - prefab.gameObject.SetActive(false); - } - - var component = parent != null - ? UnityEngine.Object.Instantiate(prefab, parent) - : UnityEngine.Object.Instantiate(prefab); + if (wasActive) + { + prefab.gameObject.SetActive(false); + } - if (VContainerSettings.Instance != null && VContainerSettings.Instance.RemoveClonePostfix) - component.name = prefab.name; + var component = parent != null + ? UnityEngine.Object.Instantiate(prefab, parent) + : UnityEngine.Object.Instantiate(prefab); - try - { - injector.Inject(component, resolver, customParameters); - destination.ApplyDontDestroyOnLoadIfNeeded(component); - } - finally - { - if (wasActive) + if (VContainerSettings.Instance != null && VContainerSettings.Instance.RemoveClonePostfix) + component.name = prefab.name; + + try { - prefab.gameObject.SetActive(true); - component.gameObject.SetActive(true); + injector.Inject(component, resolver, customParameters); + destination.ApplyDontDestroyOnLoadIfNeeded(component); } + finally + { + if (wasActive) + { + prefab.gameObject.SetActive(true); + component.gameObject.SetActive(true); + } + } + return component; } - - return component; } } } \ No newline at end of file diff --git a/VContainer/Assets/VContainer/Runtime/Unity/LifetimeScope.cs b/VContainer/Assets/VContainer/Runtime/Unity/LifetimeScope.cs index f321625b..47bb80f4 100644 --- a/VContainer/Assets/VContainer/Runtime/Unity/LifetimeScope.cs +++ b/VContainer/Assets/VContainer/Runtime/Unity/LifetimeScope.cs @@ -263,24 +263,25 @@ public TScope CreateChildFromPrefab(TScope prefab, IInstaller installer where TScope : LifetimeScope { var wasActive = prefab.gameObject.activeSelf; - using var dirtyScope = new ObjectResolverUnityExtensions.PrefabDirtyScope(prefab.gameObject); - - if (wasActive) - { - prefab.gameObject.SetActive(false); - } - var child = Instantiate(prefab, transform, false); - if (installer != null) - { - child.localExtraInstallers.Add(installer); - } - child.parentReference.Object = this; - if (wasActive) + using (new ObjectResolverUnityExtensions.PrefabDirtyScope(prefab.gameObject)) { - prefab.gameObject.SetActive(true); - child.gameObject.SetActive(true); + if (wasActive) + { + prefab.gameObject.SetActive(false); + } + var child = Instantiate(prefab, transform, false); + if (installer != null) + { + child.localExtraInstallers.Add(installer); + } + child.parentReference.Object = this; + if (wasActive) + { + prefab.gameObject.SetActive(true); + child.gameObject.SetActive(true); + } + return child; } - return child; } public TScope CreateChildFromPrefab(TScope prefab, Action installation) @@ -317,7 +318,7 @@ LifetimeScope GetRuntimeParent() if (parentReference.Object != null) return parentReference.Object; - + // Find via implementation var implParent = FindParent(); if (implParent != null) diff --git a/VContainer/Assets/VContainer/Runtime/Unity/ObjectResolverUnityExtensions.cs b/VContainer/Assets/VContainer/Runtime/Unity/ObjectResolverUnityExtensions.cs index 337a6bb4..821c0f5b 100644 --- a/VContainer/Assets/VContainer/Runtime/Unity/ObjectResolverUnityExtensions.cs +++ b/VContainer/Assets/VContainer/Runtime/Unity/ObjectResolverUnityExtensions.cs @@ -8,8 +8,8 @@ public static class ObjectResolverUnityExtensions { public readonly struct PrefabDirtyScope : IDisposable { - private readonly GameObject _prefab; - private readonly bool _madeDirty; + readonly GameObject _prefab; + readonly bool _madeDirty; public PrefabDirtyScope(GameObject prefab) { @@ -17,6 +17,8 @@ public PrefabDirtyScope(GameObject prefab) #if UNITY_EDITOR && UNITY_2020_1_OR_NEWER _madeDirty = prefab.activeSelf && !UnityEditor.EditorUtility.IsDirty(_prefab); +#else + _madeDirty = false; #endif } @@ -30,7 +32,7 @@ public void Dispose() #endif } } - + public static void InjectGameObject(this IObjectResolver resolver, GameObject gameObject) { void InjectGameObjectRecursive(GameObject current) @@ -71,25 +73,26 @@ public static T Instantiate(this IObjectResolver resolver, T prefab, Transfor where T : Component { var wasActive = prefab.gameObject.activeSelf; - using var dirtyScope = new PrefabDirtyScope(prefab.gameObject); - - prefab.gameObject.SetActive(false); + using (new PrefabDirtyScope(prefab.gameObject)) + { + prefab.gameObject.SetActive(false); - var instance = UnityEngine.Object.Instantiate(prefab, parent, worldPositionStays); + var instance = UnityEngine.Object.Instantiate(prefab, parent, worldPositionStays); - SetName(instance, prefab); + SetName(instance, prefab); - try - { - resolver.InjectGameObject(instance.gameObject); - } - finally - { - prefab.gameObject.SetActive(wasActive); - instance.gameObject.SetActive(wasActive); - } + try + { + resolver.InjectGameObject(instance.gameObject); + } + finally + { + prefab.gameObject.SetActive(wasActive); + instance.gameObject.SetActive(wasActive); + } - return instance; + return instance; + } } public static T Instantiate( @@ -116,96 +119,96 @@ public static T Instantiate( where T : Component { var wasActive = prefab.gameObject.activeSelf; - using var dirtyScope = new PrefabDirtyScope(prefab.gameObject); - - prefab.gameObject.SetActive(false); + using (new PrefabDirtyScope(prefab.gameObject)) + { + prefab.gameObject.SetActive(false); - var instance = UnityEngine.Object.Instantiate(prefab, position, rotation, parent); + var instance = UnityEngine.Object.Instantiate(prefab, position, rotation, parent); - SetName(instance, prefab); + SetName(instance, prefab); - try - { - resolver.InjectGameObject(instance.gameObject); - } - finally - { - prefab.gameObject.SetActive(wasActive); - instance.gameObject.SetActive(wasActive); + try + { + resolver.InjectGameObject(instance.gameObject); + } + finally + { + prefab.gameObject.SetActive(wasActive); + instance.gameObject.SetActive(wasActive); + } + return instance; } - - return instance; } static T Instantiate(this LifetimeScope scope, T prefab, Vector3 position, Quaternion rotation) where T : Component { var wasActive = prefab.gameObject.activeSelf; - using var dirtyScope = new PrefabDirtyScope(prefab.gameObject); - - prefab.gameObject.SetActive(false); - - T instance; - if (scope.IsRoot) - { - instance = UnityEngine.Object.Instantiate(prefab, position, rotation); - UnityEngine.Object.DontDestroyOnLoad(instance); - } - else + using (new PrefabDirtyScope(prefab.gameObject)) { - // Into the same scene as LifetimeScope - instance = UnityEngine.Object.Instantiate(prefab, position, rotation, scope.transform); - instance.transform.SetParent(null); - } + prefab.gameObject.SetActive(false); - SetName(instance, prefab); + T instance; + if (scope.IsRoot) + { + instance = UnityEngine.Object.Instantiate(prefab, position, rotation); + UnityEngine.Object.DontDestroyOnLoad(instance); + } + else + { + // Into the same scene as LifetimeScope + instance = UnityEngine.Object.Instantiate(prefab, position, rotation, scope.transform); + instance.transform.SetParent(null); + } - try - { - scope.Container.InjectGameObject(instance.gameObject); - } - finally - { - prefab.gameObject.SetActive(wasActive); - instance.gameObject.SetActive(wasActive); - } + SetName(instance, prefab); - return instance; + try + { + scope.Container.InjectGameObject(instance.gameObject); + } + finally + { + prefab.gameObject.SetActive(wasActive); + instance.gameObject.SetActive(wasActive); + } + return instance; + } } static GameObject Instantiate(this LifetimeScope scope, GameObject prefab, Vector3 position, Quaternion rotation) { var wasActive = prefab.activeSelf; - using var dirtyScope = new PrefabDirtyScope(prefab); - - prefab.SetActive(false); - - GameObject instance; - if (scope.IsRoot) - { - instance = UnityEngine.Object.Instantiate(prefab, position, rotation); - UnityEngine.Object.DontDestroyOnLoad(instance); - } - else + using (new PrefabDirtyScope(prefab)) { - // Into the same scene as LifetimeScope - instance = UnityEngine.Object.Instantiate(prefab, position, rotation, scope.transform); - instance.transform.SetParent(null); - } + prefab.SetActive(false); + + GameObject instance; + if (scope.IsRoot) + { + instance = UnityEngine.Object.Instantiate(prefab, position, rotation); + UnityEngine.Object.DontDestroyOnLoad(instance); + } + else + { + // Into the same scene as LifetimeScope + instance = UnityEngine.Object.Instantiate(prefab, position, rotation, scope.transform); + instance.transform.SetParent(null); + } - SetName(instance, prefab); + SetName(instance, prefab); - try - { - scope.Container.InjectGameObject(instance); - } - finally - { - prefab.SetActive(wasActive); - instance.SetActive(wasActive); + try + { + scope.Container.InjectGameObject(instance); + } + finally + { + prefab.SetActive(wasActive); + instance.SetActive(wasActive); + } + return instance; } - - return instance; } public static GameObject Instantiate(this IObjectResolver resolver, GameObject prefab) @@ -216,23 +219,24 @@ public static GameObject Instantiate(this IObjectResolver resolver, GameObject p public static GameObject Instantiate(this IObjectResolver resolver, GameObject prefab, Transform parent, bool worldPositionStays = false) { var wasActive = prefab.activeSelf; - using var dirtyScope = new PrefabDirtyScope(prefab); - - prefab.SetActive(false); - - GameObject instance = null; - try - { - instance = UnityEngine.Object.Instantiate(prefab, parent, worldPositionStays); - SetName(instance, prefab); - resolver.InjectGameObject(instance); - } - finally + using (new PrefabDirtyScope(prefab)) { - prefab.SetActive(wasActive); - instance?.SetActive(wasActive); + prefab.SetActive(false); + + GameObject instance = null; + try + { + instance = UnityEngine.Object.Instantiate(prefab, parent, worldPositionStays); + SetName(instance, prefab); + resolver.InjectGameObject(instance); + } + finally + { + prefab.SetActive(wasActive); + instance?.SetActive(wasActive); + } + return instance; } - return instance; } public static GameObject Instantiate( @@ -257,25 +261,26 @@ public static GameObject Instantiate( Transform parent) { var wasActive = prefab.activeSelf; - using var dirtyScope = new PrefabDirtyScope(prefab); + using (new PrefabDirtyScope(prefab)) + { + prefab.SetActive(false); - prefab.SetActive(false); + var instance = UnityEngine.Object.Instantiate(prefab, position, rotation, parent); - var instance = UnityEngine.Object.Instantiate(prefab, position, rotation, parent); + SetName(instance, prefab); - SetName(instance, prefab); + try + { + resolver.InjectGameObject(instance); + } + finally + { + prefab.SetActive(wasActive); + instance.SetActive(wasActive); + } - try - { - resolver.InjectGameObject(instance); + return instance; } - finally - { - prefab.SetActive(wasActive); - instance.SetActive(wasActive); - } - - return instance; } static void SetName(UnityEngine.Object instance, UnityEngine.Object prefab)