Skip to content

Commit

Permalink
Skip injection for Component no inherit MonoBehaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
hadashiA committed Jan 28, 2021
1 parent 5091d39 commit 97e29bc
Showing 1 changed file with 16 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,23 @@ public static RegistrationBuilder RegisterEntryPoint<T>(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<TInterface>(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;
}

Expand All @@ -106,8 +112,11 @@ public static RegistrationBuilder RegisterComponentInHierarchy<T>(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;
}

Expand Down

0 comments on commit 97e29bc

Please sign in to comment.