Hi, proud community.
I am working with Addressables for some time and can’t quite get one thing.
Addressables provide AssetReference, AssetReferenceTexture, AssetReferenceSprite, and a bunch of other variations, but not AssetReferenceScene.
Why is that? Is there anything special that restricts using scenes only by name or raw AssetReference (without validation in Editor)?
Yes, for some odd reason, Scene
is a struct and doesn’t inherit from UnityEngine.Object
, which allows to fetch the GUID from the AssetDatabase.
Feel free to use this script to be able to assign scenes as references through the inspector:
using System;
using UnityEngine;
using UnityEngine.AddressableAssets;
#if UNITY_EDITOR
using UnityEditor;
#endif
[Serializable]
public class SceneReference : AssetReference
{
[SerializeField]
private string sceneName = string.Empty;
public string SceneName => sceneName;
#if UNITY_EDITOR
public SceneReference(SceneAsset scene)
: base(AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(scene)))
{
sceneName = scene.name;
}
public override bool ValidateAsset(string path)
{
return ValidateAsset(AssetDatabase.LoadAssetAtPath<SceneAsset>(path));
}
public override bool ValidateAsset(UnityEngine.Object obj)
{
return (obj != null) && (obj is SceneAsset);
}
public override bool SetEditorAsset(UnityEngine.Object value)
{
if (!base.SetEditorAsset(value))
{
return false;
}
if (value is SceneAsset scene)
{
sceneName = scene.name;
return true;
}
else
{
sceneName = string.Empty;
return false;
}
}
#endif
}
3 Likes