一:目录结构:
二:打包代码:
/****************************************************
文件:ABEditor.cs
作者:赵深圳
邮箱: 1329346609@qq.com
日期:2022/5/7 15:54:51
功能:AB包编辑器
*****************************************************/
using UnityEngine;
using UnityEditor;
using System.IO;
using System.Collections.Generic;
/// <summary>
/// 生成AB包编辑器工具
/// </summary>
public class ABEditor
{
/// <summary>
/// 需要打包的资源所在目标
/// </summary>
public static string rootPath = Application.dataPath + "/1SimpleABManager/Download";
/// <summary>
/// AB包输出路径
/// </summary>
public static string outABPath=Application.streamingAssetsPath;
/// <summary>
/// 记录每个资源所有在的AB包文件
/// key为路径
/// value为AB包名字
/// </summary>
public static Dictionary<string, string> m_Asset2BundleDic = new Dictionary<string, string>();
/// <summary>
/// 所有需要打包的资源对应的AssetBundleBuild
/// 一个文件对应一个AssetBundleBuild
/// </summary>
public static List<AssetBundleBuild> m_AssetBundleBuildLst = new List<AssetBundleBuild>();
/// <summary>
/// 打包AB包
/// </summary>
[MenuItem("ZSTools/BuildABTools")]
public static void BuildAssetBundle()
{
Log.ZSZLog(LogCategory.Normal,"开始打AB包!!!");
if (Directory.Exists(outABPath))
{
Directory.Delete(outABPath,true);
}
//打包所有的文件夹
DirectoryInfo rootDir = new DirectoryInfo(rootPath);
//获取资源路径下所有的文件夹
//这里获取的文件夹,不包括自身,所有在资源所在文件(Application.dataPath + "/1SimpleABManager/Download")下不要放打包资源,会打包不到
//放在资源所在文件所在的下一级文件,如果需要放在资源文件夹中,请把自身加入到数组中
DirectoryInfo[] dirs = rootDir.GetDirectories();
foreach (DirectoryInfo moduleDir in dirs)
{
string moduleName = moduleDir.Name;
m_AssetBundleBuildLst.Clear();
m_Asset2BundleDic.Clear();
ScanChildDireations(moduleDir);
AssetDatabase.Refresh();
string moduleOutPath = outABPath + "/" + moduleName;
if (Directory.Exists(moduleOutPath))
{
Directory.Delete(moduleOutPath, true);
}
Directory.CreateDirectory(moduleOutPath);
//打包
BuildPipeline.BuildAssetBundles(moduleOutPath,m_AssetBundleBuildLst.ToArray(),BuildAssetBundleOptions.None,EditorUserBuildSettings.activeBuildTarget);
AssetDatabase.Refresh();
}
Log.ZSZLog(LogCategory.Normal,"打包AB包结束!!!");
}
/// <summary>
/// 根据指定的文件夹
/// 将这个文件夹下所有的一级子文件夹打成一个AB包
/// 递归这个文件夹下所有的子文件夹
/// </summary>
/// <param name="directoryInfo"></param>
private static void ScanChildDireations(DirectoryInfo directoryInfo)
{
if (directoryInfo.Name.EndsWith("CSProject")|| directoryInfo.Name.EndsWith(".cs"))
{
return;
}
ScanCurrDirectory(directoryInfo);
//递归当前路径下的所有文件夹
//使所有的一级文件夹为一个模块
DirectoryInfo[] dirs = directoryInfo.GetDirectories();
foreach (var item in dirs)
{
ScanChildDireations(item);
}
}
/// <summary>
/// 遍历当前路径下的文件,并打成一个AB包
/// </summary>
/// <param name="directoryInfo"></param>
private static void ScanCurrDirectory(DirectoryInfo directoryInfo)
{
List<string> assetNameLst = new List<string>();
//获取指定文件夹中所有的文件
FileInfo[] fileInfoArr = directoryInfo.GetFiles();
foreach (FileInfo fileInfo in fileInfoArr)
{
if (fileInfo.FullName.EndsWith(".meta"))
{
continue;
}
//assetName的格式类似 "Assets/GAssets/Launch/Sphere.prefab"
string assetName = fileInfo.FullName.Substring(Application.dataPath.Length-"Assets".Length).Replace('\\','/');
assetNameLst.Add(assetName);
}
if (assetNameLst.Count > 0)
{
string assetBundleName = directoryInfo.FullName.Substring(Application.dataPath.Length + 1).Replace('\\','_').ToLower();
AssetBundleBuild build = new AssetBundleBuild();
build.assetBundleName = assetBundleName;
build.assetNames = new string[assetNameLst.Count];
for (int i = 0; i < assetNameLst.Count; i++)
{
build.assetNames[i] = assetNameLst[i];
m_Asset2BundleDic.Add(assetNameLst[i],assetBundleName);
}
m_AssetBundleBuildLst.Add(build);
}
}
}