How to inject across scene #807
-
|
I have DataPlayerManger initialize when open game, and user would begin a new game with a new scene, some component need playerdata in DataPlayerManger in the new scene, how to do that. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
Here’s a clean way to share a DataPlayerManager across scenes with VContainer.
Make a GameLifetimeScope (subclass of LifetimeScope) in your bootstrap scene and register the manager as a singleton. Check DontDestroyOnLoad on the LifetimeScope component.
In each gameplay scene, add a LifetimeScope. Set its Parent to your root (via the Inspector’s Parent Type), or parent it in code when loading the scene:
Any component in the new scene can get the same instance: If you need to inject into existing scene GameObjects (MonoBehaviours), either enable VContainerSettings → Auto Inject GameObjects, or call scope.Container.InjectGameObject(go) for specific objects. Why this works
Docs for reference: parent/child scopes via scene and EnqueueParent, and DontDestroyOnLoad registration in VContainer. |
Beta Was this translation helpful? Give feedback.
Here’s a clean way to share a DataPlayerManager across scenes with VContainer.
Make a GameLifetimeScope (subclass of LifetimeScope) in your bootstrap scene and register the manager as a singleton. Check DontDestroyOnLoad on the LifetimeScope component.