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..128938d5 100644 --- a/VContainer/Assets/VContainer/Runtime/Unity/ObjectResolverUnityExtensions.cs +++ b/VContainer/Assets/VContainer/Runtime/Unity/ObjectResolverUnityExtensions.cs @@ -30,7 +30,7 @@ public void Dispose() #endif } } - + public static void InjectGameObject(this IObjectResolver resolver, GameObject gameObject) { void InjectGameObjectRecursive(GameObject current) @@ -71,25 +71,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,25 +117,25 @@ 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) @@ -176,36 +177,36 @@ static T Instantiate(this LifetimeScope scope, T prefab, Vector3 position, Qu 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) + using (new PrefabDirtyScope(prefab)) { - 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); - } + prefab.SetActive(false); - SetName(instance, prefab); + 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); + } - try - { - scope.Container.InjectGameObject(instance); - } - finally - { - prefab.SetActive(wasActive); - instance.SetActive(wasActive); - } + SetName(instance, prefab); - return instance; + try + { + scope.Container.InjectGameObject(instance); + } + finally + { + prefab.SetActive(wasActive); + instance.SetActive(wasActive); + } + return instance; + } } public static GameObject Instantiate(this IObjectResolver resolver, GameObject prefab) @@ -216,23 +217,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 + using (new PrefabDirtyScope(prefab)) { - instance = UnityEngine.Object.Instantiate(prefab, parent, worldPositionStays); - SetName(instance, prefab); - resolver.InjectGameObject(instance); - } - finally - { - 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 +259,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); - } - finally - { - prefab.SetActive(wasActive); - instance.SetActive(wasActive); + return instance; } - - return instance; } static void SetName(UnityEngine.Object instance, UnityEngine.Object prefab)