5.x的assetbundle與4.x以及之前的版本有些不同,不過本質是一樣的躏尉,只不過5.x打包assetbundle更為簡單和人性化了蚯根,總體來說只需要三個步驟:
//這里是一個資源包數(shù)組,其中每一個資源包又可以包含多個小資源胀糜,所以一般情況下一個資源包就足夠了
AssetBundleBuild[] _ABbuild = new AssetBundleBuild[1];
第二步:給資源命名以及指定需要打包的資源
//資源包的名稱
_ABbuild[0].assetBundleName = "打包assetbundle出來之后的文件名";
//資源包下的資源名稱颅拦,一個資源包可以包含多個資源,資源由從Assets開始的路徑組成且包含自身后綴名
string[] _allassetname = new string[3];
_allassetname[0] = "Assets/1.png";
_allassetname[1] = "Assets/2.prefab";
_allassetname[2] = "Assets/3.FBX";
_ABbuild[0].assetNames = _allassetname;
第三步:開始打包
//打包到路徑E:/test/mytest
BuildPipeline.BuildAssetBundles("E:/test/mytest", _ABbuild);
將之封裝成擴展編輯器之后教藻,把如下腳本放在Editor文件夾內(nèi):
using UnityEngine;
using UnityEditor;
using System.Diagnostics;
using System.IO;
public class ExportAssetBundles : EditorWindow
{
[@MenuItem("AssetBundles/Build AssetBundles")]
static void main()
{
EditorWindow.GetWindow<ExportAssetBundles>(false,"AssetBundles");
}
private Vector2 scrollVec2;
private string rootPath = "Assets";
private string _assetBundleName = "";
private string _assetBundlePath = "Assets/StreamingAssets";
private int _assetBundleElementNum = 4;
private string[] _assetBundleElement = new string[4];
void OnGUI()
{
scrollVec2 = EditorGUILayout.BeginScrollView(scrollVec2, GUILayout.Width(position.width), GUILayout.Height(position.height));
EditorGUILayout.BeginHorizontal();
GUILayout.Label("資源包名稱:");
_assetBundleName = EditorGUILayout.TextField(_assetBundleName);
if (GUILayout.Button("清空"))
EditorApplication.delayCall += Delete;
EditorGUILayout.EndHorizontal();
EditorGUILayout.BeginHorizontal();
GUILayout.Label("資源包路徑:");
_assetBundlePath = EditorGUILayout.TextField(_assetBundlePath);
if (GUILayout.Button("瀏覽"))
EditorApplication.delayCall += Save;
EditorGUILayout.EndHorizontal();
EditorGUILayout.BeginHorizontal();
GUILayout.Label("資源包容量:");
_assetBundleElementNum = EditorGUILayout.IntField(_assetBundleElementNum);
if (GUILayout.Button("增加"))
EditorApplication.delayCall += Add;
EditorGUILayout.EndHorizontal();
if (_assetBundleElementNum > 0)
{
if (_assetBundleElement.Length != _assetBundleElementNum)
{
string[] temp = _assetBundleElement;
_assetBundleElement = new string[_assetBundleElementNum];
for (int i = 0; i < temp.Length; i++)
{
if (i < _assetBundleElement.Length)
_assetBundleElement[i] = temp[i];
}
}
for (int i = 0; i < _assetBundleElementNum; i++)
{
EditorGUILayout.BeginHorizontal();
GUILayout.Label("資源" + (i + 1) + ":");
_assetBundleElement[i] = EditorGUILayout.TextField(_assetBundleElement[i]);
if (GUILayout.Button("瀏覽"))
Browse(i);
EditorGUILayout.EndHorizontal();
}
}
if (GUILayout.Button("打包"))
{
if (_assetBundleName == "")
{
//打開一個通知欄
this.ShowNotification(new GUIContent("資源包名稱不可為空"));
return;
}
if (_assetBundlePath == "C:/" || _assetBundlePath == "D:/" || _assetBundlePath == "E:/" || _assetBundlePath == "F:/")
{
//打開一個通知欄
this.ShowNotification(new GUIContent("資源包路徑不可為根目錄"));
return;
}
if (_assetBundleElementNum <= 0)
{
//打開一個通知欄
this.ShowNotification(new GUIContent("資源包容量必須大于0"));
return;
}
for (int i = 0; i < _assetBundleElement.Length; i++)
{
if (_assetBundleElement[i] == null || _assetBundleElement[i] == "")
{
//打開一個通知欄
this.ShowNotification(new GUIContent("資源"+(i+1)+"路徑為空"));
return;
}
}
EditorApplication.delayCall += Build;
}
EditorGUILayout.EndScrollView();
}
/// <summary>
/// 清空資源包名稱
/// </summary>
void Delete()
{
_assetBundleName = "";
//轉移焦點至主窗口
EditorUtility.FocusProjectWindow();
}
/// <summary>
/// 選擇資源存儲路徑
/// </summary>
void Save()
{
string path = EditorUtility.OpenFolderPanel("選擇要存儲的路徑", "", "");
if (path.Length != 0)
{
_assetBundlePath = path;
EditorUtility.FocusProjectWindow();
}
}
/// <summary>
/// 資源包容量增加
/// </summary>
void Add()
{
_assetBundleElementNum += 1;
EditorUtility.FocusProjectWindow();
}
/// <summary>
/// 選擇單個打包資源
/// </summary>
/// <param name="i">資源序號</param>
void Browse(int i)
{
string path = EditorUtility.OpenFilePanel("選擇要打包的資源", @"E:\hutao\Unity Project5.2\Course Cloud Platform\Assets", "*");
if (path.Length != 0)
{
if (path.IndexOf(rootPath) >= 0)
{
//如果選中的資源是dll文件距帅,則自動改后綴名為.bytes
if (path.EndsWith(".dll"))
{
string newpath = path.Substring(0, path.LastIndexOf('.')) + ".bytes";
File.Move(path, newpath);
_assetBundleElement[i] = newpath.Substring(newpath.IndexOf(rootPath));
AssetDatabase.Refresh();
}
else
{
_assetBundleElement[i] = path.Substring(path.IndexOf(rootPath));
}
}
}
}
/// <summary>
/// 打包資源
/// </summary>
void Build()
{
//需要打包的資源(可打包成多個)
AssetBundleBuild[] buildMap = new AssetBundleBuild[1];
//資源包的名稱
buildMap[0].assetBundleName = _assetBundleName;
//資源包下的資源名稱,一個資源包可以包含多個資源括堤,資源由從Assets開始的路徑組成且包含自身后綴名
buildMap[0].assetNames = _assetBundleElement;
BuildPipeline.BuildAssetBundles(_assetBundlePath, buildMap);
}
}
在編輯器中效果圖如下:(其中每個資源小項可以是工程中的文件夾碌秸,如果是文件夾的話那么該文件夾內(nèi)所有資源都會打包進去)文章轉自:http://blog.csdn.net/qq992817263/article/details/50730467