jamesplease
jamesplease

Reputation: 12869

How to retrieve AssetReference from Scene?

I have a Scene marked as Addressable. Given the Scene itself, is it possible to get the AssetReference for that scene?

For example, in the following code snippet, what would ?? need to be?

Scene activeScene = SceneManager.GetActiveScene();

// What can I call here to get the AssetReference?
AssetReference activeSceneReference = ??;

Upvotes: 0

Views: 3376

Answers (2)

Vlad Moldovanov
Vlad Moldovanov

Reputation: 1

My solution for geting (in Editor) of the addressable AssetReferense (for active scene):

    [Serializable]
    public class AssetReferenceScene : AssetReference
    {    
        public AssetReferenceScene(string guid) : base(guid)
        {
        }    
        public override bool ValidateAsset(string path)
        {
            return path.EndsWith(".unity");
        }
    }
    ...
    public AssetReferenceScene _sceneAfterBootstap;
    ...
#if UNITY_EDITOR
    // get the path from active Scene-structure 
    string path = UnityEngine.SceneManagement.SceneManager.GetActiveScene().path;
    
    // get an asset by path
    var activeSceneAsset = UnityEditor.AssetDatabase.LoadAssetAtPath<UnityEditor.SceneAsset>(path);
            
    if (UnityEditor.AssetDatabase.TryGetGUIDAndLocalFileIdentifier(activeSceneAsset, out var guid, out var file))
    {
        // create for the current scene the AssetReference from given GUID 
        _sceneAfterBootstap = new AssetReferenceScene(guid);
    }
#endif

@derHugo: Thanks a lot for your idea!

Upvotes: 0

derHugo
derHugo

Reputation: 90639

If this is possible at all I honetsly can't tell since I never worked with Addressables so far.

However, following the APIs this might be possible but requires multiple steps:

Since the activeScene is a loaded scene and the same scene can be loaded multiple times via additive loading it is not actually an asset anymore and therefore doesn't have an asset reference at all.


So the first step is actually

Get the asset for a loaded scene

Having your loaded scene you can still get its original asset path via Scene.path.

var activeScene = SceneManager.GetActiveScene();
var activeScenePath = activeScene.path;

And then use AssetDatabase.LoadAssetAtPath to load the original asset

var activeSceneAsset = AssetDataBase.LoadAssetAtPath<Scene>(activeScenePath);

Now that we have the actual asset of the loaded scene we can

Get the GUID of an Asset

Having already the asset activeSceneAsset you can use AssetDatabase.TryGetGUIDAndLocalFileIdentifier in order to optain the GUID we need in the last step:

if (AssetDatabase.TryGetGUIDAndLocalFileIdentifier(activeSceneAsset, out var guid, out var file))
{
    // TODO see below
}

and now having that guid we can use it in order to

Get the AssetReference via the GUID

The reason why we wanted to get the GUID is because we can now use it for the AssetReference constructor taking the GUID as parameter:

var activeSceneReference = new AssetReference(guid);

NOTE: All this is ofcourse only possible in the Unity Editor itself since AssetDataBase does not exist outside of it.

Upvotes: 1

Related Questions