diff --git a/VContainer/Assets/VContainer/Runtime/Unity/ComponentRegistration.cs b/VContainer/Assets/VContainer/Runtime/Unity/ComponentRegistration.cs index deb8454e..e9035d47 100644 --- a/VContainer/Assets/VContainer/Runtime/Unity/ComponentRegistration.cs +++ b/VContainer/Assets/VContainer/Runtime/Unity/ComponentRegistration.cs @@ -115,7 +115,7 @@ Component InstantiatePrefab(IObjectResolver resolver) { var parent = destination.GetParent(); var wasActive = destination.Prefab.gameObject.activeSelf; - if (destination.Prefab.gameObject.activeSelf) + if (wasActive) { destination.Prefab.gameObject.SetActive(false); } @@ -124,9 +124,13 @@ Component InstantiatePrefab(IObjectResolver resolver) ? UnityEngine.Object.Instantiate(destination.Prefab, parent) : UnityEngine.Object.Instantiate(destination.Prefab); - destination.Prefab.gameObject.SetActive(wasActive); injector.Inject(component, resolver, Parameters); - component.gameObject.SetActive(true); + + if (wasActive) + { + destination.Prefab.gameObject.SetActive(true); + component.gameObject.SetActive(true); + } return component; } diff --git a/VContainer/Assets/VContainer/Runtime/Unity/LifetimeScope.cs b/VContainer/Assets/VContainer/Runtime/Unity/LifetimeScope.cs index 71c184ee..6cb596e1 100644 --- a/VContainer/Assets/VContainer/Runtime/Unity/LifetimeScope.cs +++ b/VContainer/Assets/VContainer/Runtime/Unity/LifetimeScope.cs @@ -204,20 +204,19 @@ public LifetimeScope CreateChild(Action installation) public LifetimeScope CreateChildFromPrefab(LifetimeScope prefab, IInstaller installer = null) { var wasActive = prefab.gameObject.activeSelf; - if (prefab.gameObject.activeSelf && prefab.autoRun) + if (wasActive) { prefab.gameObject.SetActive(false); } - var child = Instantiate(prefab, transform, false); - prefab.gameObject.SetActive(wasActive); if (installer != null) { child.extraInstallers.Add(installer); } child.parentReference.Object = this; - if (!child.gameObject.activeSelf) + if (wasActive) { + prefab.gameObject.SetActive(true); child.gameObject.SetActive(true); } return child; diff --git a/VContainer/Assets/VContainer/Tests/Unity/Fixtures/SampleMonoBehaviour.cs b/VContainer/Assets/VContainer/Tests/Unity/Fixtures/SampleMonoBehaviour.cs index 8f7f19b1..912eb7a4 100644 --- a/VContainer/Assets/VContainer/Tests/Unity/Fixtures/SampleMonoBehaviour.cs +++ b/VContainer/Assets/VContainer/Tests/Unity/Fixtures/SampleMonoBehaviour.cs @@ -13,12 +13,18 @@ public class ServiceA public sealed class SampleMonoBehaviour : MonoBehaviour, IComponent { public ServiceA ServiceA; + public ServiceA ServiceAInAwake; public bool StartCalled; public int UpdateCalls; void Start() => StartCalled = true; void Update() => UpdateCalls += 1; + void Awake() + { + ServiceAInAwake = ServiceA; + } + [Inject] public void Construct(ServiceA serviceA) { diff --git a/VContainer/Assets/VContainer/Tests/Unity/LifetimeScopeTest.cs b/VContainer/Assets/VContainer/Tests/Unity/LifetimeScopeTest.cs index 067136a7..c5dde411 100644 --- a/VContainer/Assets/VContainer/Tests/Unity/LifetimeScopeTest.cs +++ b/VContainer/Assets/VContainer/Tests/Unity/LifetimeScopeTest.cs @@ -95,6 +95,52 @@ public IEnumerator CreateChild() Assert.That(parentLifetimeScope.transform.childCount, Is.Zero); } + [Test] + public void CreateChildFromPrefab() + { + var parent = LifetimeScope.Create(builder => + { + builder.RegisterEntryPoint(Lifetime.Singleton).AsSelf(); + }); + + var childPrefab = new GameObject("Child").AddComponent(); + + var child = parent.CreateChildFromPrefab(childPrefab); + + var parentResolved = parent.Container.Resolve(); + var childResolved = child.Container.Resolve(); + Assert.That(parentResolved, Is.InstanceOf()); + Assert.That(childResolved, Is.InstanceOf()); + Assert.That(childResolved, Is.EqualTo(parentResolved)); + Assert.That(child.parentReference.Object, Is.EqualTo(parent)); + Assert.That(childPrefab.gameObject.activeSelf, Is.True); + } + + [Test] + public void CreateChildFromPrefabWithInActive() + { + var parent = LifetimeScope.Create(builder => + { + builder.RegisterEntryPoint(Lifetime.Singleton).AsSelf(); + }); + + var childPrefab = new GameObject("Child").AddComponent(); + childPrefab.gameObject.SetActive(false); + + var child = parent.CreateChildFromPrefab(childPrefab); + Assert.That(childPrefab.gameObject.activeSelf, Is.False); + Assert.That(child.gameObject.activeSelf, Is.False); + + child.Build(); + + var parentResolved = parent.Container.Resolve(); + var childResolved = child.Container.Resolve(); + Assert.That(parentResolved, Is.InstanceOf()); + Assert.That(childResolved, Is.InstanceOf()); + Assert.That(childResolved, Is.EqualTo(parentResolved)); + Assert.That(child.parentReference.Object, Is.EqualTo(parent)); + } + [Test] public void CreateScopeWithSingleton() { diff --git a/VContainer/Assets/VContainer/Tests/Unity/UnityContainerBuilderTest.cs b/VContainer/Assets/VContainer/Tests/Unity/UnityContainerBuilderTest.cs index a40a7128..74c8ff47 100644 --- a/VContainer/Assets/VContainer/Tests/Unity/UnityContainerBuilderTest.cs +++ b/VContainer/Assets/VContainer/Tests/Unity/UnityContainerBuilderTest.cs @@ -181,50 +181,88 @@ public void RegisterComponentOnNewGameObjectUnderTransform() public void RegisterComponentInNewPrefab() { var prefab = new GameObject("Sample").AddComponent(); - { - var go1 = new GameObject("Parent"); - var builder = new ContainerBuilder(); - builder.Register(Lifetime.Transient); - builder.RegisterComponentInNewPrefab(prefab, Lifetime.Transient) - .UnderTransform(go1.transform); - var container = builder.Build(); - var resolved1 = container.Resolve(); - var resolved2 = container.Resolve(); - Assert.That(resolved1.gameObject.name, Is.EqualTo("Sample(Clone)")); - Assert.That(resolved2.gameObject.name, Is.EqualTo("Sample(Clone)")); - Assert.That(resolved1, Is.InstanceOf()); - Assert.That(resolved2, Is.InstanceOf()); - Assert.That(resolved1.transform.parent, Is.EqualTo(go1.transform)); - Assert.That(resolved2.transform.parent, Is.EqualTo(go1.transform)); - Assert.That(resolved1, Is.Not.EqualTo(resolved2)); - } + var go1 = new GameObject("Parent"); + var builder = new ContainerBuilder(); + builder.Register(Lifetime.Transient); + builder.RegisterComponentInNewPrefab(prefab, Lifetime.Transient) + .UnderTransform(go1.transform); - { - var builder = new ContainerBuilder(); - builder.Register(Lifetime.Transient); - builder.RegisterComponentInNewPrefab(prefab, Lifetime.Scoped); + var container = builder.Build(); + var resolved1 = container.Resolve(); + var resolved2 = container.Resolve(); + Assert.That(resolved1.gameObject.name, Is.EqualTo("Sample(Clone)")); + Assert.That(resolved2.gameObject.name, Is.EqualTo("Sample(Clone)")); + Assert.That(resolved1, Is.InstanceOf()); + Assert.That(resolved2, Is.InstanceOf()); + Assert.That(resolved1.transform.parent, Is.EqualTo(go1.transform)); + Assert.That(resolved2.transform.parent, Is.EqualTo(go1.transform)); + Assert.That(resolved1, Is.Not.EqualTo(resolved2)); + Assert.That(prefab.gameObject.activeSelf, Is.True); + } - var container = builder.Build(); - var resolved1 = container.Resolve(); - var resolved2 = container.Resolve(); - Assert.That(resolved1.gameObject.name, Is.EqualTo("Sample(Clone)")); - Assert.That(resolved2.gameObject.name, Is.EqualTo("Sample(Clone)")); - Assert.That(resolved1, Is.InstanceOf()); - Assert.That(resolved2, Is.InstanceOf()); - Assert.That(resolved1.transform.parent, Is.Null); - Assert.That(resolved2.transform.parent, Is.Null); - Assert.That(resolved1, Is.EqualTo(resolved2)); - } + [Test] + public void RegisterComponentInNewPrefabScoped() + { + var prefab = new GameObject("Sample").AddComponent(); - { - var builder = new ContainerBuilder(); - builder.Register(Lifetime.Transient); - builder.RegisterComponentInNewPrefab(prefab, Lifetime.Scoped); + var builder = new ContainerBuilder(); + builder.Register(Lifetime.Transient); + builder.RegisterComponentInNewPrefab(prefab, Lifetime.Scoped); - var container = builder.Build(); - Assert.That(container.Resolve(), Is.InstanceOf()); - } + var container = builder.Build(); + var resolved1 = container.Resolve(); + var resolved2 = container.Resolve(); + Assert.That(resolved1.gameObject.name, Is.EqualTo("Sample(Clone)")); + Assert.That(resolved2.gameObject.name, Is.EqualTo("Sample(Clone)")); + Assert.That(resolved1, Is.InstanceOf()); + Assert.That(resolved2, Is.InstanceOf()); + Assert.That(resolved1.transform.parent, Is.Null); + Assert.That(resolved2.transform.parent, Is.Null); + Assert.That(resolved1, Is.EqualTo(resolved2)); + Assert.That(prefab.gameObject.activeSelf, Is.True); + } + + [Test] + public void RegisterComponentInNewPrefabWithAwake() + { + var prefab = new GameObject("Sample").AddComponent(); + + var builder = new ContainerBuilder(); + builder.Register(Lifetime.Transient); + builder.RegisterComponentInNewPrefab(prefab, Lifetime.Singleton); + + var container = builder.Build(); + var resolved1 = container.Resolve(); + Assert.That(resolved1.gameObject.name, Is.EqualTo("Sample(Clone)")); + Assert.That(resolved1, Is.InstanceOf()); + Assert.That(resolved1.transform.parent, Is.Null); + Assert.That(prefab.gameObject.activeSelf, Is.True); + Assert.That(resolved1.gameObject.activeSelf, Is.True); + Assert.That(resolved1.ServiceA, Is.InstanceOf()); + Assert.That(resolved1.ServiceAInAwake, Is.InstanceOf()); + } + + [Test] + public void RegisterComponentInNewPrefabWithInActive() + { + var prefab = new GameObject("Sample").AddComponent(); + prefab.gameObject.SetActive(false); + + var builder = new ContainerBuilder(); + builder.Register(Lifetime.Transient); + builder.RegisterComponentInNewPrefab(prefab, Lifetime.Singleton); + + var container = builder.Build(); + var resolved = container.Resolve(); + Assert.That(resolved.gameObject.name, Is.EqualTo("Sample(Clone)")); + Assert.That(resolved, Is.InstanceOf()); + Assert.That(resolved.transform.parent, Is.Null); + + Assert.That(prefab.gameObject.activeSelf, Is.False); + Assert.That(resolved.gameObject.activeSelf, Is.False); + Assert.That(resolved.ServiceA, Is.InstanceOf()); + Assert.That(resolved.ServiceAInAwake, Is.Null); } [Test]