自动化创建 AssetBundle

自动创建AssetBundle,共分为以下三个脚本,分别来讨论:

1、“自动给资源文件添加标记”(AutoSetLabelToPrefabs.cs )
2、“打包资源且输出路径” (BuildAssetBundle.cs )
3、“删除路径中所有资源”(DeleteAssetBundle.cs)。

 (1)自动给资源文件添加标记(AutoSetLabelToPrefabs.cs )

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;

/*
    Description
        开发思路
            1:定义需要打包资源的文件夹根目录
            2:遍历每个"场景"文件夹(目录)
                A:遍历本场景目录下所有的目录或者文件
                  如果是目录,则继续“递归”访问里面的文件。直到定位到文件
                B:找到文件,则使用AssetImporter类,标记"报名"与"后缀"。
*/
namespace ABFW
{
    public class AutoSetLabels 
    {
        [MenuItem("AssetBundelTools/Set AB Label")]
        public static void SetABLabel()
        {
            //需要给AB做标记的根目录
            string strNeedSetLabelRoot = string.Empty;
            //目录信息(场景目录信息数组,表示所有的根目录下场景目录)
            DirectoryInfo[] dirScenesDIArray = null;
            //清空无用AB标记
            AssetDatabase.RemoveUnusedAssetBundleNames();
            //需要打包资源的文件夹根目录
            //strNeedSetLabelRoot = Application.dataPath + "/" + "AB_Res";
            strNeedSetLabelRoot = PathTools.GetABResourcesPath();
            DirectoryInfo dirTempInfo = new DirectoryInfo(strNeedSetLabelRoot);
            dirScenesDIArray = dirTempInfo.GetDirectories();
           
            //2:遍历每个“场景”文件夹(目录)
            foreach (DirectoryInfo currentDIR in dirScenesDIArray)
            {
                //遍历本场景目录下的目录或者文件,如果是目录,则继续"递归"访问里面的文件,直到定位到文件
                
                string tmpScenesDIR = strNeedSetLabelRoot + "/" + currentDIR.Name; //全路径
                Debug.Log(currentDIR.Name);
                DirectoryInfo tmpSceensDIRInfo = new DirectoryInfo(tmpScenesDIR);
                int tmpIndex = tmpScenesDIR.LastIndexOf("/");
                //场景名称
                string tmpScenesName = tmpScenesDIR.Substring(tmpIndex + 1);
                //递归调用方法,找到文件,使用AssetImporter类,标记"包名"与"后缀名”
                JudgeDIRorFileByRecursive(currentDIR,tmpScenesName);
               
            }
            //刷新
            AssetDatabase.Refresh();
            //提示信息,标记包名完成
            Debug.Log("AssetBundle 本次操作设置标记完成!");
        }

        /// <summary>
        /// 递归判断是否为目录与文件,修改AssettBundle的标记(lable)
        /// </summary>
        /// <param name="currentDIR">当前的文件信息(文件信息与目录信息可以转换)</param>
        /// <param name="scenesName">当前场景名称</param>
        private static void JudgeDIRorFileByRecursive(FileSystemInfo fileSysInfo, string scenesName)
        {
            //参数检查
            if (!fileSysInfo.Exists)
            {
                Debug.LogError("文件或者目录名称: " + fileSysInfo + " 不存在,请检查");
                return;
            }

            //得到当前目录下一级的文件信息集合
            DirectoryInfo dirInfoObj = fileSysInfo as DirectoryInfo;                         //文件信息转换为目录信息
            FileSystemInfo[] fileSysArray = dirInfoObj.GetFileSystemInfos();
            foreach (FileSystemInfo fileInfo in fileSysArray)
            {
                FileInfo fileinfoObj = fileInfo as FileInfo;
                //文件类型
                if (fileinfoObj != null)
                {
                    //修改此文件的AssetBundle标签
                    SetFileABLabel(fileinfoObj, scenesName);
                }
                //目录类型
                else
                {
                    //如果是目录则递归调用
                    JudgeDIRorFileByRecursive(fileInfo, scenesName);
                }
            }
        }

        /// <summary>
        /// 对指定的文件 设置AB包名名称
        /// </summary>
        /// <param name="fileinfoObj">文件信息(包含文件绝对路径)</param>
        /// <param name="scenesName">场景名称</param>
        private static void SetFileABLabel(FileInfo fileinfoObj,string scenesName)
        {
           Debug.Log("fileinfoObj.FullName= " + fileinfoObj.FullName);

            //AssetBundle包的名称
            string strAbName = string.Empty;
            //文件的路径(相对路径)
            string strAssetFilePath = string.Empty;
            //参数检查
            if (fileinfoObj.Extension == ".meta") return;
            //得到AB包的名称
            strAbName = GetABName(fileinfoObj,scenesName);
            //获取资源文件的相对路径
            int tmpIndex = fileinfoObj.FullName.IndexOf("Assets");
            //得到文件相对路径
            strAssetFilePath = fileinfoObj.FullName.Substring(tmpIndex);
            //给资源文件设置AB名称以及后缀
            AssetImporter tmpImporterObj = AssetImporter.GetAtPath(strAssetFilePath);
            tmpImporterObj.assetBundleName = strAbName;

            if (fileinfoObj.Extension == ".unity")
                //定义AB包的扩展名
                tmpImporterObj.assetBundleVariant = "u3d";
            else
                tmpImporterObj.assetBundleVariant = "ab";
        }


        /// <summary>
        /// 获取AB包的名称
        /// </summary>
        /// <param name="fileinfoObj">文件信息</param>
        /// <param name="scenesName">场景名称名称</param>
        /// <returns></returns>
        private static string GetABName(FileInfo fileinfoObj,string scenesName)
        {
            //返回AB包的名称
            string strABName = string.Empty;
            //win路径
            string tmpWinPath = fileinfoObj.FullName;
            //Unity路径
            string tmpUnityPath = tmpWinPath.Replace("\\", "/");//替换为Unity字符串分割符
            //定位"场景名称" 后面字符位置
            int tmpSceneNamePosition = tmpUnityPath.IndexOf(scenesName)+scenesName.Length;
            //AB包中类型名称 所在区域
            string strABFileNameArea = tmpUnityPath.Substring(tmpSceneNamePosition+1);
            Debug.Log("@@@strABFileNameArea:  " + strABFileNameArea);
            if (strABFileNameArea.Contains("/"))
            {
                string[] tempStrArray = strABFileNameArea.Split('/');
                //AB包名称正式形成
                strABName = scenesName + "/" + tempStrArray[0];
                Debug.Log("@######strABFileNameArea:  " + tempStrArray[0]);
            }
            else
            {
                //定义*.Unity文件形成的特殊AB包名称
                strABName = scenesName + "/" + scenesName;
            }

            return strABName;
        }
    
    }//class end
}

2、打包资源且输出路径” (BuildAssetBundle.cs )

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;


namespace ABFW
{
    public class BuildAssetBundle
    {
        /// <summary>
        /// 打包生成所有的AssetBundles(包)
        /// </summary>
        [MenuItem("AssetBundelTools/BuildAllAssetBundles")]
        public static void BuildAllAB()
        {
            //打包AB输出路径
            string strABOutPathDIR = string.Empty;

            //获取"StreamingAssets"数值
            strABOutPathDIR = PathTools.GetABOutPath();

            //判断生成输出目录文件夹
            if (!Directory.Exists(strABOutPathDIR))
            {
                Directory.CreateDirectory(strABOutPathDIR);
            }
            //打包生成
            BuildPipeline.BuildAssetBundles(strABOutPathDIR, BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows64);
        }
    }
}

1、路径管理

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/*
    Description
        开发思路
 *      包含所有的路径常量,路径方法
*/
namespace ABFW
{
    public class PathTools
    {
        /*路径常量*/
        public const string AB_RESOURCES = "AB_Resources";

        /// <summary>
        /// 得到AB资源的输出目录
        /// </summary>
        /// <returns></returns>
        public static string GetABResourcesPath()
        {
            return Application.dataPath + "/" + AB_RESOURCES;
        }

        /// <summary>
        /// 获取AB输出路径
        /// 1、平台(Pc/移动端)路径
        /// 2、平台的名称
        /// </summary>
        public static string GetABOutPath()
        {
            return GetPlatformPath() + "/" + GetPlatformName();
        }

        /// <summary>
        /// 获取平台的路径
        /// </summary>
        /// <returns></returns>
        private static string GetPlatformPath()
        {
            string strReturnPlatformPAth = string.Empty;

            switch (Application.platform)
            {
                case RuntimePlatform.Android:
                case RuntimePlatform.IPhonePlayer:
                    strReturnPlatformPAth = Application.persistentDataPath;
                    break;
                case RuntimePlatform.WindowsEditor:
                case RuntimePlatform.WindowsPlayer:
                    strReturnPlatformPAth = Application.streamingAssetsPath;
                    break;

                default:
                    break;
            }
            return strReturnPlatformPAth;
        }

        /// <summary>
        /// 获取平台名称
        /// </summary>
        /// <returns></returns>
        private static string GetPlatformName()
        {
            string strReturnformName = string.Empty;
            switch (Application.platform)
            {
                case RuntimePlatform.Android:
                    strReturnformName = "Android";
                    break;
                case RuntimePlatform.IPhonePlayer:
                    strReturnformName = "Iphone";
                    break;
                case RuntimePlatform.WindowsEditor:
                case RuntimePlatform.WindowsPlayer:
                    strReturnformName = "Windows";
                    break;

                default:
                    break;
            }

            return strReturnformName;

        }
    }
}

3、删除路径中所有资源”(DeleteAssetBundle.cs)。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;
/*
    Description
        开发思路
        删除指定目录下所有文件
*/
namespace ABFW
{
    public class DeleteAssetBundle 
    {
        [MenuItem("AssetBundelTools/DeleteAllAssetBundles")]
        public static void DelAssetBundle()
        {
            //删除AB包输出目录
            string strNeedDaleteDIR = PathTools.GetABOutPath();
            if (!string.IsNullOrEmpty(strNeedDaleteDIR))
            {
                //主要:这里"true"表示可以删除非空目录
                Directory.Delete(strNeedDaleteDIR, true);
                //去出删除警告
                File.Delete(strNeedDaleteDIR + ".meta");
                //刷新
                AssetDatabase.Refresh();
            }
        }
    
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值