How to implement Zenjectautoinject in VContainer.
Hello, I am working on a multiplayer game (using Unity's new Netcode for Gameobjects NGO) where we want to use VContainer for our DI.
I understand that we can use IObjectResolver.Instantiate to inject into dynamically-generated MonoBehaviours at runtime. However, spawning in NGO is handled by the server, which also automatically replicates the spawn to the client. (So I cannot call IObjectResolver.Instantiate myself on the client).
To give a clear example, in Zenject, we have the Zenjectautoinject to solve this problem. https://github.com/modesttree/Zenject#zenautoinjecter
I was wondering if there is anything similar in VContainer?
In VContainer, there can be multiple sibling DI Containers on the hierarchy. Therefore, there is no uniform way to identify the DI Container that MonoBehaviour will be injected.
If necessary, the following can be implemented in a way that suits your project.
class AutoInjector : MonoBehaviour
{
void Awake()
{
LookupContainer().InjectGameObject(gameObject);
}
IObjectResolver LookupContainer()
{
// Here which LifetimeScope you want to target.
return GetComponentInParent<LifetimeScope>().Container;
}
}