LoginSignup
24
23

More than 5 years have passed since last update.

選択したフォルダの階層構造を保ったままAssetBundleにする

Last updated at Posted at 2014-05-20

Unity5では階層構造を保ったままAssetBundleにできるらしく、このような記事も「今更かよ....」とか思われそうではありますが、AssetBundle関連の記事は少ないように思いましたので投稿させて頂きました。

以下、コードになります。

BuildAssetBundle

public class BuildAssetBundles
{

    [ MenuItem("Assets/Build AssetBundle/WebPlayer ExplicitAssetNames")]
    static void BuildAssetBundle_ExplicitAssetNames_WebPlayer ()
    {

        //AssetBundleにするフォルダを選択.
        string selectPath = EditorUtility.OpenFolderPanel( "フォルダを選択して下さい", Application.dataPath, "FolderName");
        if( selectPath.Length == 0 )
        {
            Debug.Log( "フォルダ選択エラー" );
            return;
        }

        //選択したフォルダはプロジェクトに存在するかチェック.
        if( selectPath.IndexOf( Application.dataPath ) < 0 )
        {
            Debug.Log("選択したフォルダは作業中のプロジェクトに存在しません");
            return;
        }

        //プロジェクト名を取得.
        string prjName = Application.dataPath.Replace("/Assets", "");
        prjName = prjName.Substring(prjName.LastIndexOf("/") + 1);

        //出力パスを指定.
        string outPath = EditorUtility.SaveFilePanel("Save Resources", "", prjName, "unity3d");
        if( outPath.Length == 0 )
        {
            return;
        }

        //オブジェクト名のみになるように整形.
        string[] names = Directory.GetFiles( selectPath, "*.*", SearchOption.AllDirectories);
        for( int i = 0 ; i < names.Length ; i++ )
        {
            string path = names[ i ];
            path = path.Replace( "\\", "/" );
            path = path.Replace( selectPath + "/", "" );
            path = path.Substring( 0, path.LastIndexOf( "." ) );
            names[ i ] = path;
        }

        //Resourcesからのパスに整形.
        string resPath = selectPath.Replace( Application.dataPath + "/Resources/", "" );
        resPath += "/";

        //Assetの配列を作成.
        Object[] assets = new Object[names.Length];
        for( int i = 0 ; i < names.Length ; i++ )
        {
            assets[i] = Resources.Load( resPath + names[i] );
        }

        //AssetBundleを出力.
        BuildPipeline.BuildAssetBundleExplicitAssetNames( assets, names, outPath,
                                                BuildAssetBundleOptions.CollectDependencies | 
                                                BuildAssetBundleOptions.CompleteAssets |
                                                BuildAssetBundleOptions.DeterministicAssetBundle
                                             );

        //終了ログ.
        Debug.Log("Build is finished!");
    }

}

http://blog.douga-kan.com/lagoon/blog/2012/11/07/%E3%80%90unity%E3%80%91android%E3%82%A2%E3%83%97%E3%83%AA%E9%96%8B%E7%99%BA-%E5%82%99%E5%BF%98%E9%8C%B2%E2%91%B1%E3%80%80assetbundle%E3%81%A7%E3%83%95%E3%82%A9%E3%83%AB%E3%83%80%E6%A7%8B%E9%80%A0/
こちらをやる上で参考にさせて頂いた記事になります。
記事をお書きになったmush様、大変参考になりました。この場を借りてお礼申し上げます。

24
23
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
24
23