Skip to content

Instantly share code, notes, and snippets.

@shana
Created November 20, 2020 15:21
Show Gist options
  • Save shana/2eaaf2f21796c258b05ba794d28207d0 to your computer and use it in GitHub Desktop.
Save shana/2eaaf2f21796c258b05ba794d28207d0 to your computer and use it in GitHub Desktop.
Implementation of AssetReferenceT for referencing addressables scene with a nice selector UI in the Unity Inspector.
using System;
using UnityEditor;
using UnityEngine.AddressableAssets;
[Serializable]
public class AssetReferenceScene : AssetReferenceT<SceneReference>
{
/// <summary>
/// Constructs a new reference to a GameObject.
/// </summary>
/// <param name="guid">The object guid.</param>
public AssetReferenceScene(string guid) : base(guid)
{
}
public override bool ValidateAsset(string path)
{
#if UNITY_EDITOR
var type = AssetDatabase.GetMainAssetTypeAtPath(path);
return typeof(SceneAsset).IsAssignableFrom(type);
#else
return false;
#endif
}
#if UNITY_EDITOR
public new SceneAsset editorAsset
{
get
{
if (CachedAsset != null || string.IsNullOrEmpty(AssetGUID))
return CachedAsset as SceneAsset;
var assetPath = AssetDatabase.GUIDToAssetPath(AssetGUID);
var main = AssetDatabase.LoadMainAssetAtPath(assetPath) as SceneAsset;
if (main != null)
CachedAsset = main;
return main;
}
}
#endif
}
[Serializable]
public class SceneReference : UnityEngine.Object
{
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment