Unity 5.X扩展编辑器之打包assetbundle

5.x的assetbundle与4.x以及之前的版本有些不同,不过本质是一样的,只不过5.x打包assetbundle更为简单和人性化了,总体来说只需要三个步骤:


第一步:创建打包资源

//这里是一个资源包数组,其中每一个资源包又可以包含多个小资源,所以一般情况下一个资源包就足够了
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文件夹内:

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);
    }
}



在编辑器中效果图如下:(其中每个资源小项可以是工程中的文件夹,如果是文件夹的话那么该文件夹内所有资源都会打包进去)




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

神码编程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值