子スコープの生成 (シーン/Prefab)
How to make an Additive scene a child
How to set the parent when loading a scene
You can parent it by specifying a LifetimeScope
object before loading the scene.
class SceneLoader
{
readonly LifetimeScope parent;
public SceneLoader(LifetimeScope lifetimeScope)
{
parent = lifetimeScope; // Inject the LifetimeScope to which this class belongs
}
IEnumerator LoadSceneAsync()
{
// LifetimeScope generated in this block will be parented by `this.lifetimeScope`
using (LifetimeScope.EnqueueParent(parent))
{
// If this scene has a LifetimeScope, its parent will be `parent`.
var loading = SceneManager.LoadSceneAsync("...", LoadSceneMode.Additive);
while (!loading.isDone)
{
yield return null;
}
}
}
// UniTask example
async UniTask LoadSceneAsync()
{
using (LifetimeScope.EnqueueParent(parent))
{
await SceneManager.LoadSceneAsync("...", LoadSceneMode.Additive);
}
}
}
LifetimeScope is a GameObject, so you can also search from the scene.
var parent = LifetimeScope.Find<BaseLifetimeScope>();
How to add additional registers to the next scene
Often, you may want to add additional Registers to the loaded scenes.
For example, when context is finalized after assets are loaded asynchronously.
In that case you could use:
// LifetimeScopes generated during this block will be additionally Registered.
using (LifetimeScope.Enqueue(builder =>
{
// Register for the next scene not yet loaded
builder.RegisterInstance(extraInstance);
}))
{
// Loading the scene..
}