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.

Question How to get asset name/path from AssetReference?

Discussion in 'Addressables' started by Peter77, Oct 25, 2020.

  1. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,324
    I'm trying to integrate Addressables 1.8.5 in an older project that I updated to Unity 2019.4.9f1. Means there exists a lot of code assumptions that I try to get working with Addressables.

    Problem
    When the game loads an additive scene, it first checks if that scene is loaded already. In case it's loaded, it just skips the load call.
    Code (CSharp):
    1. Scene existingScene = SceneManager.GetSceneByName(sceneName);
    2. if (existingScene.IsValid() && existingScene.isLoaded)
    3.     continue;
    To get this code working with Addressables, I have to "convert" an
    AssetReference
    or
    Key
    to the name or path of that scene to use it with the SceneManager API.

    Question
    I have an AssetReference where I know it points to a scene. How can I get the actual scene name of that AssetReference? The RuntimeKey seems to be a GUID.
     
    Last edited: Oct 25, 2020
  2. LuGus-Jan

    LuGus-Jan

    Joined:
    Oct 3, 2016
    Posts:
    136
    Hi @Peter77

    As in the other question, you can also use the
    Addressables.LoadResourceLocationsAsync()
    here to load the location info about the asset, and in that information, either the result's
    PrimaryKey
    or
    InternalId
    will give you the address of the asset.

    Or, you can create a custom
    AssetReference
    script which takes a scene asset as a value to create it's asset reference and serializes the scene's name with it. As they derive from the
    AssetReference
    class, you can use these the same way as you normally would to load the asset through Addressables. We use this in our project for scene references:

    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3. using UnityEngine.AddressableAssets;
    4.  
    5. #if UNITY_EDITOR
    6. using UnityEditor;
    7. #endif
    8.  
    9. [Serializable]
    10. public class SceneReference : AssetReference
    11. {
    12.     [SerializeField]
    13.     private string sceneName = string.Empty;
    14.  
    15.     public string SceneName
    16.     {
    17.         get { return sceneName; }
    18.     }
    19.  
    20. #if UNITY_EDITOR
    21.     public SceneReference(SceneAsset scene)
    22.     : base(AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(scene)))
    23.     {
    24.         sceneName = scene.name;
    25.     }
    26.  
    27.     public override bool ValidateAsset(string path)
    28.     {
    29.         return ValidateAsset(AssetDatabase.LoadAssetAtPath<SceneAsset>(path));
    30.     }
    31.  
    32.     public override bool ValidateAsset(UnityEngine.Object obj)
    33.     {
    34.         return (obj != null) && (obj is SceneAsset);
    35.     }
    36.  
    37.     public override bool SetEditorAsset(UnityEngine.Object value)
    38.     {
    39.         if (!base.SetEditorAsset(value))
    40.         {
    41.             return false;
    42.         }
    43.  
    44.         if (value is SceneAsset scene)
    45.         {
    46.             sceneName = scene.name;
    47.             return true;
    48.         }
    49.         else
    50.         {
    51.             sceneName = string.Empty;
    52.             return false;
    53.         }
    54.     }
    55.  
    56. #endif
    57. }
    58.  
    Only downside of this is of course that, if your scene changes it's name, then the value is no longer valid. We're using some maintenance scripts that can check these fields and update them, but those are project dependent and I can't share these with you.

    Hope this helps. :)
     
    JokerDen likes this.