コガネブログ

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

【Unity】アセットバンドル名を設定せずにアセットバンドルをビルドする方法

概要

using System.IO;
using UnityEditor;

public static class Example
{
    [MenuItem( "Tools/Build Asset Bundles" )]
    private static void BuildAssetBundles()
    {
        var outputPath = "AssetBundles";

        Directory.CreateDirectory( outputPath );

        var materialBundle = new AssetBundleBuild
        {
            assetBundleName = "materials.bundle",
            assetNames = new[]
            {
                "Assets/material 1.mat",
                "Assets/material 2.mat",
                "Assets/material 3.mat",
            },
        };

        var sceneBundle = new AssetBundleBuild
        {
            assetBundleName = "scenes.bundle",
            assetNames = new[]
            {
                "Assets/scene 1.unity",
                "Assets/scene 2.unity",
                "Assets/scene 3.unity",
            },
        };

        var builds = new[]
        {
            materialBundle,
            sceneBundle,
        };

        BuildPipeline.BuildAssetBundles
        (
            outputPath: outputPath,
            builds: builds,
            assetBundleOptions: BuildAssetBundleOptions.ChunkBasedCompression,
            targetPlatform: BuildTarget.StandaloneWindows
        );
    }
}

BuildPipeline.BuildAssetBundles の引数に AssetBundleBuild の配列を渡すことで
アセットバンドル名やそのアセットバンドルに含まれるアセットをコードから設定できます