From 97e29bc1dc2d6e7c38d9c92f6dc1be9f8c1fd354 Mon Sep 17 00:00:00 2001 From: hadashiA Date: Fri, 29 Jan 2021 01:59:32 +0900 Subject: [PATCH] Skip injection for Component no inherit MonoBehaviour --- .../Unity/ContainerBuilderUnityExtensions.cs | 23 +++++++++++++------ 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/VContainer/Assets/VContainer/Runtime/Unity/ContainerBuilderUnityExtensions.cs b/VContainer/Assets/VContainer/Runtime/Unity/ContainerBuilderUnityExtensions.cs index d7cf31ed..20933883 100644 --- a/VContainer/Assets/VContainer/Runtime/Unity/ContainerBuilderUnityExtensions.cs +++ b/VContainer/Assets/VContainer/Runtime/Unity/ContainerBuilderUnityExtensions.cs @@ -73,17 +73,23 @@ public static RegistrationBuilder RegisterEntryPoint(this IContainerBuilder b return registrationBuilder.AsImplementedInterfaces(); } - public static RegistrationBuilder RegisterComponent(this IContainerBuilder builder, MonoBehaviour component) + public static RegistrationBuilder RegisterComponent(this IContainerBuilder builder, Component component) { - var registrationBuilder = builder.RegisterInstance(component).As(typeof(MonoBehaviour), component.GetType()); - builder.RegisterBuildCallback(container => container.Inject(component)); + var registrationBuilder = builder.RegisterInstance(component); + if (component is MonoBehaviour monoBehaviour) + { + builder.RegisterBuildCallback(container => container.Inject(monoBehaviour)); + } return registrationBuilder; } public static RegistrationBuilder RegisterComponent(this IContainerBuilder builder, TInterface component) { - var registrationBuilder = builder.RegisterInstance(component).As(typeof(MonoBehaviour), typeof(TInterface)); - builder.RegisterBuildCallback(container => container.Inject(component)); + var registrationBuilder = builder.RegisterInstance(component); + if (component is MonoBehaviour monoBehaviour) + { + builder.RegisterBuildCallback(container => container.Inject(monoBehaviour)); + } return registrationBuilder; } @@ -106,8 +112,11 @@ public static RegistrationBuilder RegisterComponentInHierarchy(this IContaine throw new VContainerException(typeof(T), $"Component {typeof(T)} is not in this scene {scene.path}"); } - var registrationBuilder = builder.RegisterInstance(component).As(typeof(MonoBehaviour), typeof(T)); - builder.RegisterBuildCallback(container => container.Inject(component)); + var registrationBuilder = builder.RegisterInstance(component).As(typeof(T)); + if (component is MonoBehaviour monoBehaviour) + { + builder.RegisterBuildCallback(container => container.Inject(monoBehaviour)); + } return registrationBuilder; }