Skip to content

Commit

Permalink
Merge pull request #576 from AlonTalmi/ComponentRegistrationResolverE…
Browse files Browse the repository at this point in the history
…xposed

Exposed IObjectResolver to prefab and parent finders in ComponentRegistrationBuilder
  • Loading branch information
hadashiA authored Dec 18, 2023
2 parents 144f8bf + 87a435f commit 198a954
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ namespace VContainer.Unity
struct ComponentDestination
{
public Transform Parent;
public Func<Transform> ParentFinder;
public Func<IObjectResolver, Transform> ParentFinder;
public bool DontDestroyOnLoad;

public Transform GetParent()
public Transform GetParent(IObjectResolver resolver)
{
if (Parent != null)
return Parent;
if (ParentFinder != null)
return ParentFinder();
return ParentFinder(resolver);
return null;
}

Expand All @@ -32,7 +32,7 @@ public void ApplyDontDestroyOnLoadIfNeeded(Component component)
public sealed class ComponentRegistrationBuilder : RegistrationBuilder
{
readonly object instance;
readonly Component prefab;
readonly Func<IObjectResolver, Component> prefabFinder;
readonly string gameObjectName;

ComponentDestination destination;
Expand All @@ -51,12 +51,12 @@ internal ComponentRegistrationBuilder(in Scene scene, Type implementationType)
}

internal ComponentRegistrationBuilder(
Component prefab,
Func<IObjectResolver, Component> prefabFinder,
Type implementationType,
Lifetime lifetime)
: base(implementationType, lifetime)
{
this.prefab = prefab;
this.prefabFinder = prefabFinder;
}

internal ComponentRegistrationBuilder(
Expand All @@ -81,10 +81,10 @@ public override Registration Build()
{
provider = new FindComponentProvider(ImplementationType, Parameters, in scene, in destination);
}
else if (prefab != null)
else if (prefabFinder != null)
{
var injector = InjectorCache.GetOrBuild(prefab.GetType());
provider = new PrefabComponentProvider(prefab, injector, Parameters, in destination);
var injector = InjectorCache.GetOrBuild(ImplementationType);
provider = new PrefabComponentProvider(prefabFinder, injector, Parameters, in destination);
}
else
{
Expand All @@ -101,6 +101,12 @@ public ComponentRegistrationBuilder UnderTransform(Transform parent)
}

public ComponentRegistrationBuilder UnderTransform(Func<Transform> parentFinder)
{
destination.ParentFinder = _ => parentFinder();
return this;
}

public ComponentRegistrationBuilder UnderTransform(Func<IObjectResolver, Transform> parentFinder)
{
destination.ParentFinder = parentFinder;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,14 +168,15 @@ public static ComponentRegistrationBuilder RegisterComponentOnNewGameObject<T>(
return builder.RegisterComponentOnNewGameObject(typeof(T), lifetime, newGameObjectName);
}

public static ComponentRegistrationBuilder RegisterComponentInNewPrefab<T>(
public static ComponentRegistrationBuilder RegisterComponentInNewPrefab(
this IContainerBuilder builder,
Type type,
T prefab,
Type interfaceType,
Component prefab,
Lifetime lifetime)
where T : Component
{
return builder.Register(new ComponentRegistrationBuilder(prefab, type, lifetime));
var componentRegistrationBuilder = builder.Register(new ComponentRegistrationBuilder(_ => prefab, prefab.GetType(), lifetime));
componentRegistrationBuilder.As(interfaceType);
return componentRegistrationBuilder;
}

public static ComponentRegistrationBuilder RegisterComponentInNewPrefab<T>(
Expand All @@ -186,6 +187,26 @@ public static ComponentRegistrationBuilder RegisterComponentInNewPrefab<T>(
{
return builder.RegisterComponentInNewPrefab(typeof(T), prefab, lifetime);
}

public static ComponentRegistrationBuilder RegisterComponentInNewPrefab<T>(
this IContainerBuilder builder,
Func<IObjectResolver, T> prefab,
Lifetime lifetime)
where T : Component
{
return builder.Register(new ComponentRegistrationBuilder(prefab, typeof(T), lifetime));
}

public static ComponentRegistrationBuilder RegisterComponentInNewPrefab<TInterface, TImplement>(
this IContainerBuilder builder,
Func<IObjectResolver, TImplement> prefab,
Lifetime lifetime)
where TImplement : Component, TInterface
{
var componentRegistrationBuilder = builder.Register(new ComponentRegistrationBuilder(prefab, typeof(TImplement), lifetime));
componentRegistrationBuilder.As<TInterface>();
return componentRegistrationBuilder;
}

#if VCONTAINER_ECS_INTEGRATION
public readonly struct NewWorldBuilder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public object SpawnInstance(IObjectResolver resolver)
{
var component = default(Component);

var parent = destination.GetParent();
var parent = destination.GetParent(resolver);
if (parent != null)
{
component = parent.GetComponentInChildren(componentType, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public object SpawnInstance(IObjectResolver resolver)
var gameObject = new GameObject(name);
gameObject.SetActive(false);

var parent = destination.GetParent();
var parent = destination.GetParent(resolver);
if (parent != null)
{
gameObject.transform.SetParent(parent);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using UnityEngine;

Expand All @@ -7,30 +8,32 @@ sealed class PrefabComponentProvider : IInstanceProvider
{
readonly IInjector injector;
readonly IReadOnlyList<IInjectParameter> customParameters;
readonly Component prefab;
readonly Func<IObjectResolver, Component> prefabFinder;
ComponentDestination destination;

public PrefabComponentProvider(
Component prefab,
Func<IObjectResolver, Component> prefabFinder,
IInjector injector,
IReadOnlyList<IInjectParameter> customParameters,
in ComponentDestination destination)
{
this.injector = injector;
this.customParameters = customParameters;
this.prefab = prefab;
this.prefabFinder = prefabFinder;
this.destination = destination;
}

public object SpawnInstance(IObjectResolver resolver)
{
var prefab = prefabFinder(resolver);
var parent = destination.GetParent(resolver);

var wasActive = prefab.gameObject.activeSelf;
if (wasActive)
{
prefab.gameObject.SetActive(false);
}

var parent = destination.GetParent();

var component = parent != null
? UnityEngine.Object.Instantiate(prefab, parent)
: UnityEngine.Object.Instantiate(prefab);
Expand Down

1 comment on commit 198a954

@vercel
Copy link

@vercel vercel bot commented on 198a954 Dec 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.