hadashiA / VContainer

The extra fast, minimum code size, GC-free DI (Dependency Injection) library running on Unity Game Engine.
https://vcontainer.hadashikick.jp
MIT License
2.01k stars 176 forks source link

GetRuntimeParent called in Awake is inconvenient. #725

Open majesty77 opened 1 week ago

majesty77 commented 1 week ago

VContainer: v1.16.4

Even if autorun is set to false in Awake, GetRuntimeParent is called and the Parent is set. This is not convenient in the following situations

// With VContainerSettings.Instance.RootLifetimeScope set.
// Assuming the autorun of the LifetimeScope Prefab for this key's address is set to false
var handle = await Addressables.LoadAssetAsync<LifetimeScope>(key);
var result = handle.Result;

// Internal Awake sets Parent to VContainerSettings.Instance.RootLifetimeScope
var instance = Instantiate(assetLoadHandle.Result); 

// I call Build with parentScope as parent at another time, but it doesn't work as intended because Parent is already set
using (LifetimeScope.EnqueueParent(parentScope)) {
    instance.Build();
}

I think it would be better if the Parent is determined at Build time, since the Parent resolution process is also performed inside Build, so I think it would be better to remove the GetRuntimeParent that is called in Awake.