using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class TestBunildAssetB {
//在Unity編輯器中添加菜單
[MenuItem("BuildAssetBundles/BuildSelectAssetbundle")]
static void BuildAssetbundleSelect()
{
// 打開保存面板,選擇路徑並保存
string outputPath = EditorUtility.SaveFolderPanel("Build Select Assetbundle", "", "");
if (outputPath.Length != 0)
{
BuildTarget activeTarget = EditorUserBuildSettings.activeBuildTarget;
AssetBundleBuild[] buildMap = new AssetBundleBuild[Selection.assetGUIDs.Length];
for (int i = 0; i < Selection.assetGUIDs.Length; i++)
{
string guid = Selection.assetGUIDs[i];
//將GUID(全局唯一標識符)轉換爲對應的資源路徑。
//所有的路徑都是相對於工程目錄文件。例如” Assets/MyTextures/hello.png”
string assetPath = AssetDatabase.GUIDToAssetPath(guid);
//報告指定 Unicode 字符在此實例中的最後一個匹配項的索引位置。
int startindex = assetPath.LastIndexOf('/') + 1;
int length = assetPath.LastIndexOf('.') - startindex;
//此處是打包後的文件名;根據guid命名,並且自定義後綴名
buildMap[i].assetBundleName = assetPath.Substring(startindex, length) + ".assetbundle";
buildMap[i].assetNames = new string[1];
buildMap[i].assetNames[0] = assetPath;
}
//打包
BuildPipeline.BuildAssetBundles(outputPath, buildMap, BuildAssetBundleOptions.None, activeTarget);
}
}
}