コガネブログ

平日更新を目標に Unity や C#、Visual Studio、ReSharper などのゲーム開発アレコレを書いていきます

【Unity】AssetBundleManifest を JSON 形式で出力するための構造体

ソースコード

using System;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using JetBrains.Annotations;
using UnityEngine;

[Serializable]
[SuppressMessage( "ReSharper", "InconsistentNaming" )]
public struct JsonAssetBundleManifest
{
    [SerializeField][UsedImplicitly] private JsonAssetBundleBuild[] builds;

    public JsonAssetBundleManifest( AssetBundleManifest manifest )
    {
        builds = manifest
                .GetAllAssetBundles()
                .Select( x => new JsonAssetBundleBuild( manifest, x ) )
                .ToArray()
            ;
    }

    public override string ToString()
    {
        return JsonUtility.ToJson( this, true );
    }
}

[Serializable]
[SuppressMessage( "ReSharper", "InconsistentNaming" )]
public struct JsonAssetBundleBuild
{
    [SerializeField][UsedImplicitly] private string   assetBundleName;
    [SerializeField][UsedImplicitly] private string[] assetNames;

    public JsonAssetBundleBuild
    (
        AssetBundleManifest manifest,
        string              assetBundleName
    )
    {
        this.assetBundleName = assetBundleName;
        assetNames           = manifest.GetDirectDependencies( assetBundleName );
    }

    public override string ToString()
    {
        return JsonUtility.ToJson( this, true );
    }
}

使用例

using UnityEngine;

public sealed class Example : MonoBehaviour
{
    private void Start()
    {
        var assetBundle = AssetBundle.LoadFromFile( "AssetBundle/Android" );
        var manifest    = assetBundle.LoadAsset<AssetBundleManifest>( "AssetBundleManifest" );

        var json = new JsonAssetBundleManifest( manifest ).ToString();

        Debug.Log( json );
        GUIUtility.systemCopyBuffer = json;

        assetBundle.Unload( false );
    }
}