Important
Disclaimer: This plugin is an open source service and is not an official service provided by VContainer.
Adds a LifetimeScope
to VContainer that is initialized (Configured) only
once.
- Since initialization only happens once, it persists across scene transitions
- Can be placed in multiple scenes. If duplicates exist, they are removed and only the first initialized one is kept
- Useful when you want to implement processes that should only be initialized once
- When initialization is costly, performing it only once can improve performance
- Recommended for performance-sensitive environments like mobile devices
- Since DontDestroyOnLoad is applied to the object, carefully consider which scripts to attach
- When setting a
PersistentLifetimeScope
as a parent, always usePersistentChildLifetimeScope
For example, you can create an AppLifetimeScope
to be used throughout the entire game. By initializing it in a lightweight scene, such as the title screen, you can complete heavy initialization processes in a less resource-intensive environment. Additionally, since this AppLifetimeScope
persists across scene transitions, the heavy initialization won't run again after changing scenes.
Please place it in all scenes. This ensures that AppLifetimeScope
initialization occurs regardless of which scene you
start from during development. There's no need to worry about duplication. Only the first initialized instance is used,
and others are destroyed.
After installation, import from Package Manager and try running the scene.
"Unity Editor : Window > Package Manager > Add package from git URL...".
URL: https://github.com/IShix-g/VContainer-Extensions.git?path=Packages/PersistentLifetimeScope
- Implement
PersistentLifetimeScope<T>
- Create an object in the scene and attach the implemented script to it
Since this object uses DontDestroyOnLoad, carefully consider which script you attach to it.
using VContainer;
using VContainer.Unity.Extensions;
public sealed class AppLifetimeScope : PersistentLifetimeScope<SamplePersistentLifetimeScope>
{
protected override void OnInitialize()
{
// This block is executed only once and is executed before the initialization of the LifetimeScope.
}
protected override void OnEveryAwake(AppLifetimeScope instance)
{
// This block is executed every time Awake() is called and runs before the initialization of the LifetimeScope.
}
protected override void Configure(IContainerBuilder builder)
{
// This is executed only once.
builder.Register<AdsService>(Lifetime.Singleton);
builder.Register<IDataRepository, PersistentDataRepository>(Lifetime.Singleton);
}
}
- Implement
PersistentChildLifetimeScope
or a generic version of it - Create an object in the scene and attach the implemented script to it
- Create a prefab and place it in all scenes
- Use
PersistentChildLifetimeScope
for anyLifetimeScope
that you want configured as a child of aPersistentLifetimeScope
. - Note that
PersistentChildLifetimeScope
behaves like a normalLifetimeScope
and does not utilize DontDestroyOnLoad.
using VContainer;
using VContainer.Unity.Extensions;
public sealed class TestSceneLifetimeScope : PersistentChildLifetimeScope
{
protected override void Configure(IContainerBuilder builder)
{
}
}
You can specify the parent PersistentLifetimeScope
using a Generic.
using VContainer;
using VContainer.Unity.Extensions;
public sealed class TestSceneLifetimeScope : PersistentChildLifetimeScope<AppLifetimeScope>
{
protected override void Configure(IContainerBuilder builder)
{
}
}