Is it possible to implement a Zenject-like GameObjectСontext using VContainer? #724
Replies: 2 comments
-
|
Ok, this kind of works, but it feels wrong. 😅 Are there any more elegant solutions? public override void Install(IContainerBuilder builder)
{
GameObject instance;
using (LifetimeScope.EnqueueParent(this))
instance = _dialogManagerPrefab.InstantiateAsync().WaitForCompletion();
builder.Register<IDialogManager>(_ =>
{
instance.GetComponentInChildren<LifetimeScope>().Build();
return instance.GetComponent<DialogManager>();
}, Lifetime.Scoped);
builder.RegisterBuildCallback(c =>
{
c.Resolve<IDialogManager>(); // prevent lazy initialization
var objectScope = instance.GetComponentInChildren<LifetimeScope>();
foreach (var child in instance.GetComponentsInChildren<Behaviour>().Where(x => x is not LifetimeScope))
objectScope.Container.Inject(child);
});
builder.RegisterDisposeCallback(_ =>
{
Addressables.ReleaseInstance(instance);
});
} |
Beta Was this translation helpful? Give feedback.
-
|
Yes — the Zenject 1. Cleanest: create a child scope from a LifetimeScope prefab Put a LifetimeScope on your prefab (next to DialogManager) and register its components inside that prefab scope. Scene scope spawns the prefab as a child This gives you Zenject‐style composition: the prefab’s scope can resolve from the scene scope, while its own registrations stay isolated. 2. Addressables route (when you can’t drag a prefab reference) If the prefab comes from Addressables, load → set the parent → Build() the child scope → (optionally) inject the hierarchy. Key points:
Why this is “the VContainer way”
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I have a
SceneScopeLifetimeScopeplaced in the scene and a prefab containingDialogManagerLifetimeScope.I'm trying to instantiate and register DialogManager in SceneScopeLifetimeScope and attach the local scope (which is already present in the prefab) to the container tree.
Here’s my latest attempt, but it doesn’t seem to work.
Any suggestions would be greatly appreciated. Thanks in advance!
Beta Was this translation helpful? Give feedback.
All reactions