Hey! We have an AssetReference for a scene, and we want to check if the scene is already loaded before calling Addressables.LoadSceneAsync.
As far as I can tell, this simply isn’t possible because
RuntimeKey in AssetReference is a GUID and there’s no way to get the scene’s name from it.
Is this right?
If so, it seems that the best approach is to use the Addressable Name as declared in the editor, and make sure it matches the file name - which removes one of the big advantages of using the system.
Hey there! I’ll flag this for the team, and share any guidance they have!
Hey, so this is an interesting use case. The best way to go about it would be to load the IResourceLocation from the AssetReference and then use the InternalId of the location of the scene to ask the scene manager if the scene is loaded. Something like this (disclaimer this has only been tested in exactly one scenario and appeared to work):
public AssetReference sceneReference;
public IEnumerator Start()
{
var loc = Addressables.LoadResourceLocationsAsync(sceneReference);
yield return loc;
var result = loc.Result;
Debug.Log(SceneManager.GetSceneByPath(result[0].InternalId).isLoaded);
}
Hopefully this, or something very like it, will work for you.
This seems to work for me, thanks! Here’s the code I used to load a scene if it wasn’t already loaded.
[SerializeField] private AssetReference _scene = default;
[SerializeField] private LoadSceneMode _loadMode = default;
public void LoadSceneIfNotLoaded()
{
Addressables.LoadResourceLocationsAsync(_scene).Completed += (loc) =>
{
var isSceneLoaded = SceneManager.GetSceneByPath(loc.Result[0].InternalId).isLoaded;
if (!isSceneLoaded)
{
_scene.LoadSceneAsync(_loadMode);
}
};
}
Was looking for exactly this and this answer worked. Thanks.
Hi! For the sake of understanding, what does InternalId return here, and why does it work as a scene path even though it’s not in the build settings?
Would this work with IResourceLocation.PrimaryKey as well?