AddressableAssetsが存在するかテストする

ただの備忘録。

定義

public static class AddressableAssets
{
    public static class Bgm
    {
        public const string Test1 = "Bgm/TestBgm1";
        public const string Test2 = "Bgm/TestBgm2";
        public const string Test3 = "Bgm/TestBgm3";
    }
}

使うところ

Addressables.LoadAssetAsync<AudioClip>(AddressableAssets.Bgm.Test1);

テスト

using System.Collections;
using System.Linq;
using System.Reflection;
using NUnit.Framework;
using UnityEngine;

namespace Editor.Tests
{
    public class AddressableTest
    {
        public static IEnumerable BgmAddresses
        {
            get
            {
                return typeof(AddressableAssets.Bgm)
                    .GetFields(BindingFlags.Public | BindingFlags.Static)
                    .Select(x => (string) x.GetValue(null))
                    .Select(t => new TestCaseData(t));
            }
        }

        [TestCaseSource(nameof(BgmAddresses))]
        [Test]
        public void Bgmが存在するか(string address)
        {
            var font = EditorAddressables.LoadAsset<AudioClip>(address);
            Assert.IsNotNull(font);
        }
    }
}

public static class EditorAddressables
{
    public static AddressableAssetGroup[] GetGroups()
    {
        var settings = UnityEditor.AddressableAssets.AddressableAssetSettingsDefaultObject.Settings;
        var groups = settings.groups;
        return groups.ToArray();
    }

    public static T LoadAsset<T>(string address) where T : UnityEngine.Object
    {
        var entry = GetGroups().SelectMany(x => x.entries)
            .Single(x => x.address == address);
        return (T) entry.MainAsset;
    }
}