Is it possible to implement a Zenject-like GameObjectСontext using VContainer? #724
Unanswered
finch-bird
asked this question in
Q&A
Replies: 1 comment
-
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.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I have a
SceneScopeLifetimeScope
placed 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