Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Is it possible to get the original scene name of a scene IResourceLocation?

Discussion in 'Addressables' started by R1PFake, Aug 24, 2022.

  1. R1PFake

    R1PFake

    Joined:
    Aug 7, 2015
    Posts:
    507
    Im using a catalog IResourceLocator to iterate over it's keys and then use the keys for the IResourceLocator.Locate method using typeof(SceneInstance) to find the scene IResourceLocations.

    The scene IResourceLocation(s)s are found and returned by the Locate method.

    My questions is, is there any way to get the original scene name, before loading the scene, with this information (IResourceLocation)?

    I saw the PrimaryKey property of the IResourceLocation, but it looks like this is the key / name which was set in the Addressable menu, which does not have to match with the original asset name.
     
  2. R1PFake

    R1PFake

    Joined:
    Aug 7, 2015
    Posts:
    507
    Some more info and a "workaround" for my updated use case:

    I checked more documentation page and Google searches but found nothing.
    But I also learnt that I can set a whole folder as Addressable instead of every scene by hand.

    After doing that the scenes show up as children inside the Addressables group, but their name can't be changed (gray read only state).

    Their default name is the asset path so "Assets/SomePath/MyScene.unity"

    In this use case the IResourceLocation.PrimaryKey returns the asset path, so I changed my code with a workaround to check if the key ends with ".unity" and then call System.IO.Path.GetFileNameWithoutExtension to extract the name "MyScene".

    It's not 100% correct, because it's still only a extracted path name and the actual scene name could be different if you mark the scene itself as Addressable and give it a "wrong" name, but I don't have to worry about these edge cases, because I will use this new workflow.

    Hint: If you rename the scene it will not updated the path name, but you can simply toggle the scene folder Addressable checkbox, then they will be refreshed with the new path. Im not sure if this is a bug or not, but it's a easy workaround.
     
  3. andymilsom

    andymilsom

    Unity Technologies

    Joined:
    Mar 2, 2016
    Posts:
    294
    There is no sure fire way to do this, we do not store any data that is needed for the loading processes. Though you can do it by foregoing some other functionality.

    The internalId of the Scene location will be dependant on the setting of the Group -> Internal Asset Naming Mode. If this is Full Path or Filename, then you can get what you need. But it is ideal to have this as dynamic to have smaller paths for runtime where possible. Chances are particularly if just for a scenes group, you do not have thousands of them, and the paths are not very long. Then it'll be fine to use Fullpath (Filename would be small too)
     
    R1PFake likes this.
  4. R1PFake

    R1PFake

    Joined:
    Aug 7, 2015
    Posts:
    507
    Thank you for the hint. I checked my asset export project and it looks like the FullPath mode was the default setting. I prefer this method over the PrimaryKey and adopted my code to use the InternalId.