Unity 5.X擴展編輯器之打包assetbundle

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

最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末绍移,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子讥电,更是在濱河造成了極大的恐慌蹂窖,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,651評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件恩敌,死亡現(xiàn)場離奇詭異瞬测,居然都是意外死亡,警方通過查閱死者的電腦和手機纠炮,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,468評論 3 392
  • 文/潘曉璐 我一進店門涣楷,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人抗碰,你說我怎么就攤上這事狮斗。” “怎么了弧蝇?”我有些...
    開封第一講書人閱讀 162,931評論 0 353
  • 文/不壞的土叔 我叫張陵碳褒,是天一觀的道長。 經(jīng)常有香客問我看疗,道長沙峻,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,218評論 1 292
  • 正文 為了忘掉前任两芳,我火速辦了婚禮摔寨,結果婚禮上,老公的妹妹穿的比我還像新娘怖辆。我一直安慰自己是复,他們只是感情好,可當我...
    茶點故事閱讀 67,234評論 6 388
  • 文/花漫 我一把揭開白布竖螃。 她就那樣靜靜地躺著淑廊,像睡著了一般。 火紅的嫁衣襯著肌膚如雪特咆。 梳的紋絲不亂的頭發(fā)上季惩,一...
    開封第一講書人閱讀 51,198評論 1 299
  • 那天,我揣著相機與錄音腻格,去河邊找鬼画拾。 笑死,一個胖子當著我的面吹牛菜职,可吹牛的內(nèi)容都是我干的青抛。 我是一名探鬼主播,決...
    沈念sama閱讀 40,084評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼些楣,長吁一口氣:“原來是場噩夢啊……” “哼脂凶!你這毒婦竟也來了宪睹?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 38,926評論 0 274
  • 序言:老撾萬榮一對情侶失蹤蚕钦,失蹤者是張志新(化名)和其女友劉穎亭病,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體嘶居,經(jīng)...
    沈念sama閱讀 45,341評論 1 311
  • 正文 獨居荒郊野嶺守林人離奇死亡罪帖,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,563評論 2 333
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了邮屁。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片整袁。...
    茶點故事閱讀 39,731評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖佑吝,靈堂內(nèi)的尸體忽然破棺而出坐昙,到底是詐尸還是另有隱情,我是刑警寧澤芋忿,帶...
    沈念sama閱讀 35,430評論 5 343
  • 正文 年R本政府宣布炸客,位于F島的核電站,受9級特大地震影響戈钢,放射性物質發(fā)生泄漏痹仙。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,036評論 3 326
  • 文/蒙蒙 一殉了、第九天 我趴在偏房一處隱蔽的房頂上張望开仰。 院中可真熱鬧,春花似錦薪铜、人聲如沸众弓。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,676評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽田轧。三九已至暴匠,卻和暖如春鞍恢,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背每窖。 一陣腳步聲響...
    開封第一講書人閱讀 32,829評論 1 269
  • 我被黑心中介騙來泰國打工帮掉, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人窒典。 一個月前我還...
    沈念sama閱讀 47,743評論 2 368
  • 正文 我出身青樓蟆炊,卻偏偏與公主長得像,于是被迫代替她去往敵國和親瀑志。 傳聞我的和親對象是個殘疾皇子涩搓,可洞房花燭夜當晚...
    茶點故事閱讀 44,629評論 2 354

推薦閱讀更多精彩內(nèi)容