ChuangLi_BuildAB类
[MenuItem("Build AssetBundle/Taptap/1-Build AssetBundles")]
public static void BuildABS()
{
BuildAssetBundle();
AssetDatabase.Refresh();
Debug.Log("BuildABS OK");
CopyFilesToABsFile();
}
BuildAssetBundles类
protected static void BuildAssetBundle()
{
Platform pf = GetPlatform();
//删除增量文件夹 删除备份文件夹
ClearIncrements(pf);
ClearTemp(pf);
CheckPath(pf.abPath);
//将当前的asset bundle备份一份,
CopyDirectory(pf.abPath, pf.CopyabPath);
// 打包assetbundle ,并使用 LZ4 压缩
BuildPipeline.BuildAssetBundles(pf.abPath, BuildAssetBundleOptions.ChunkBasedCompression | BuildAssetBundleOptions.DeterministicAssetBundle, pf.target);
CreateSizeFile(pf.abPath);
//比较最新的asset bundle和备份的assetbundle ,并复制到新的增量文件夹
CopyChangeFile(pf.abPath, pf.CopyabPath, pf);
//删除备份文件夹
ClearTemp(pf);
}
protected static Platform GetPlatform()
{
Platform pf = new Platform();
pf.abFilesDic = new Dictionary<string, string>();
pf.CopyabFilesDic = new Dictionary<string, string>();
pf.NeedDownFiles = new List<string>();
pf.ABsFilePath = Application.dataPath + "/StreamingAssets/ABs/";
string tag = "Unkonwn";
#if UNITY_ANDROID
tag = "Android";
pf.target = BuildTarget.Android;
#endif
#if UNITY_IPHONE
tag = "IOS";
pf.target = BuildTarget.iOS;
#endif
#if UNITY_STANDALONE_WIN
tag = "Win";
pf.target = BuildTarget.StandaloneWindows64;
#endif
#if UNITY_WSA
tag = "WSA";
pf.target = BuildTarget.WSAPlayer;
#endif
pf.abPath = Application.dataPath + "/../ABS_" + tag + "/AssetBundles/";
pf.CopyabPath = Application.dataPath + "/../ABS_" + tag + "/OldAssetBundles/";
pf.name = "AssetBundles";
pf.changeFilePath = Application.dataPath + "/../ABS_" + tag + "/" + tag + System.DateTime.Now.ToString("_yyyyMMdd_HHmmss") + "/";
return pf;
}
static void ClearIncrements(Platform pf)
{
if (Directory.Exists(pf.changeFilePath))
{
Directory.Delete(pf.changeFilePath, true);
}
}
static void ClearTemp(Platform pf)
{
if (Directory.Exists(pf.CopyabPath))
{
Directory.Delete(pf.CopyabPath, true);
}
}
protected static void CheckPath(string path)
{
if (!System.IO.Directory.Exists(path))
{
System.IO.Directory.CreateDirectory(path);
}
}
//将当前的asset bundle备份一份,
public static void CopyDirectory(string srcPath, string destPath)
{
try
{
DirectoryInfo dir = new DirectoryInfo(srcPath);
FileSystemInfo[] fileinfo = dir.GetFileSystemInfos(); //获取目录下(不包含子目录)的文件和子目录
foreach (FileSystemInfo i in fileinfo)
{
if (i is DirectoryInfo) //判断是否文件夹
{
if (!Directory.Exists(destPath + "\\" + i.Name))
{
Directory.CreateDirectory(destPath + "\\" + i.Name); //目标目录下不存在此文件夹即创建子文件夹
}
CopyDirectory(i.FullName, destPath + "\\" + i.Name); //递归调用复制子文件夹
}
else
{
File.Copy(i.FullName, destPath + "\\" + i.Name, true); //不是文件夹即复制文件,true表示可以覆盖同名文件
}
}
}
catch (System.Exception e)
{
throw;
}
}
static void CreateSizeFile(string path)
{
using (FileStream t_file = new FileStream(path + "_FileSize.txt", FileMode.Create, FileAccess.Write))
{
using (StreamWriter t_sw = new StreamWriter(t_file))
{
DirectoryInfo dir = new DirectoryInfo(path);
string perStr = dir.FullName;
System.Text.StringBuilder sb = new System.Text.StringBuilder();
Ddir(path, sb, perStr);
t_sw.Write(sb.ToString());
}
}
index = 0;
using (FileStream t_file = new FileStream(path + "ResList.txt", FileMode.Create, FileAccess.Write))
{
using (StreamWriter t_sw = new StreamWriter(t_file))
{
DirectoryInfo dir = new DirectoryInfo(path);
string perStr = dir.FullName;
System.Text.StringBuilder sb = new System.Text.StringBuilder();
Ddir2(path, sb, perStr);
Ddir2End(sb);
t_sw.Write(sb.ToString());
}
}
}
//比较最新的asset bundle和备份的assetbundle ,并复制到新的增量文件夹
static void CopyChangeFile(string path, string copiedPath, Platform pf)
{
DirectoryInfo newPath = new DirectoryInfo(path);
DirectoryInfo oldPath = new DirectoryInfo(copiedPath);
List<FileInfo> list = new List<FileInfo>();
GetChangedAbs(path, newPath.FullName, oldPath.FullName, ref list);
DirectoryInfo changePath = new DirectoryInfo(pf.changeFilePath);
if (changePath.Exists)
{
changePath.Delete();
}
changePath.Create();
foreach (var file in list)
{
FileInfo fi = new FileInfo(file.FullName.Replace(newPath.FullName, changePath.FullName));
if (!Directory.Exists(fi.Directory.FullName))
{
fi.Directory.Create();
}
File.Copy(file.FullName, fi.FullName);
}
}
static void ClearTemp(Platform pf)
{
if (Directory.Exists(pf.CopyabPath))
{
Directory.Delete(pf.CopyabPath, true);
}
}
将新增资源拷贝到ABs文件夹中
/// <summary>
/// 将新增资源拷贝到ABs文件夹中
/// </summary>
//[MenuItem("Build AssetBundle/Taptap/2-Copy AssetBundles To StreamingAssets")]
public static void CopyFilesToABsFile()
{
Platform pf = GetPlatform();
CheckPath(pf.ABsFilePath);
DeleteFiles(pf.ABsFilePath);
CheckPath(pf.ABsFilePath);
CopyDirectory(pf.abPath, pf.ABsFilePath);
AssetDatabase.Refresh();
Debug.Log("CopyFilesToABsFile OK");
}