I have Main menu provider:
class MainMenuProvider
{
private static SceneInstance _cachedScene;
private static bool _isLoaded;
public async Task<SceneInstance> Load()
{
if (!_isLoaded)
{
var mainMenuLoadHandle =
Addressables.LoadSceneAsync("Assets/Bundles/UI/Scenes/MainMenu.unity", LoadSceneMode.Single, false);
await mainMenuLoadHandle.Task;
_cachedScene = mainMenuLoadHandle.Result;
_isLoaded = true;
}
return _cachedScene;
}
}
When I firstly invoke Load and then scene.ActivateSync it works perfect, but when I invoke Load and ActivateAsync the second time and my scene is cached, nothing happens.
_cachedScene.m_Operation.isDone == true
You set
_isLoadedstaticso this whole thing is definitely only executed exactly once the entire application session lifecycle.However, if you then ever happen to use
LoadSceneMode.Singlefor any other scene at any time, then this_cachedSceneis unloaded and doesn't exist anymore.=>
_cachedScenewill then contain an invalid scene which isn't loaded anymore and will never be loaded again.You would need to decouple the loading of the addressable and the actual loading of the scene. The
Addressables.LoadSceneAsyncalready contains directly also the call toSceneManagement.SceneManager.LoadSceneAsync. You want to split that and only use e.g. onlyAddressables.LoadAssetAsyncfor your caching.And then only load and unload this scene via the mentioned
SceneManagement.SceneManager.LoadSceneAsyncsomewhat like e.g.This would to certain extent reduce the advantage of the addressable of course since one of them is less memory usage by unloading the scene while it isn't used.