123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using UnityEditor;
- using UnityEngine;
- using YooAsset.Editor;
- namespace GFGEditor
- {
- public static class AssetBundleHelper
- {
- public static Action BuildHotUpdateDll;
- public const string RES_ROOT_DIR_PATH = "Assets/Res";
- public const int LimitByteSize = 1024*1024*4;
- private static List<string> excludeFileExtensions = new List<string>() { ".meta", ".moc3" , ".can3" };
- private static AssetBundleCollector assetBundleCollector;
- private static AssetBundleCollectRuler _ruler;
- private static AssetBundleCollectRuler ruler
- {
- get
- {
- if(_ruler == null)
- {
- _ruler = AssetBundleCollectRuler.GetData();
- }
- return _ruler;
- }
- }
- private static Dictionary<string, Dictionary<string, AssetBundleAssetListItem>> itemsSizeDicDic;
- //记录文件的bundle名
- private static Dictionary<string, string> fileBundleDic;
- public static void OnPreExecuteBuild()
- {
- EditorUtility.DisplayProgressBar("进度", "正在收集资源", 1);
- BuildHotUpdateDll();
- AssetDatabase.Refresh();
- itemsSizeDicDic = new Dictionary<string, Dictionary<string, AssetBundleAssetListItem>>();
- assetBundleCollector = AssetBundleCollector.GetData();
-
- foreach (var dir in ruler.PackBySeparately)
- {
- FileHelper.ForeachFileInDir(dir, null, (string filePath) =>
- {
- var path = filePath.Replace("\\", "/");
- var ext = Path.GetExtension(filePath);
- if(IsIgnoreFileExtension(ext))
- {
- return;
- }
- var dirPath = dir + "/";
- if (path.Contains(dirPath))
- {
- var indexLastDot = path.LastIndexOf('.');
- string key = path.Substring(0, indexLastDot);
- CollectDynamicFile(dir, key, path, true);
- }
- });
- }
- foreach (var dir in ruler.PackByTopDir)
- {
- FileHelper.ForeachFileInDir(dir, null, (string filePath) =>
- {
- var path = filePath.Replace("\\", "/");
- var ext = Path.GetExtension(filePath);
- if (IsIgnoreFileExtension(ext))
- {
- return;
- }
- var dirPath = dir + "/";
- if (path.Contains(dirPath))
- {
- var topPath = path.Replace(dir + "/", "");
- var arr = topPath.Split('/');
- string key = dir + "/" + arr[0];
- CollectDynamicFile(dir, key, path, true);
- }
- });
- }
- foreach (var dir in ruler.PackByFileName)
- {
- FileHelper.ForeachFileInDir(dir, null, (string filePath) =>
- {
- var path = filePath.Replace("\\", "/");
- var ext = Path.GetExtension(filePath);
- if (IsIgnoreFileExtension(ext))
- {
- return;
- }
- var dirPath = dir + "/";
- if (path.Contains(dirPath))
- {
- var indexLastDot = path.LastIndexOf('.');
- string key = path.Substring(0, indexLastDot);
- CollectDynamicFile(dir, key, path, false);
- }
- });
- }
- CollectNewItems();
- RecordFileBundle();
- EditorUtility.SetDirty(assetBundleCollector);
- AssetDatabase.SaveAssetIfDirty(assetBundleCollector);
- EditorUtility.ClearProgressBar();
- }
- public static void OnPostExecuteBuild(BuildResult buildResult)
- {
- if(fileBundleDic != null)
- {
- fileBundleDic.Clear();
- fileBundleDic = null;
- }
- if(itemsSizeDicDic != null)
- {
- itemsSizeDicDic.Clear();
- itemsSizeDicDic = null;
- }
- assetBundleCollector = null;
- _ruler = null;
- int index = buildResult.OutputPackageDirectory.LastIndexOf("/");
- string targetPath = buildResult.OutputPackageDirectory.Substring(0, index);
- Debug.Log($"sourcePath {buildResult.OutputPackageDirectory}");
- Debug.Log($"targetPath {targetPath}");
- CopyFilesTo(buildResult.OutputPackageDirectory, targetPath);
- Directory.Delete(buildResult.OutputPackageDirectory, true);
- }
- public static void CollectDynamicFile(string dirPath, string itemKey, string assetPath, bool autoMerge)
- {
- AssetBundleAssetList assetList;
- AssetBundleAssetListItem newItem;
- Dictionary<string, AssetBundleAssetListItem> itemsSizeDic;
- for (var i = 0; i < assetBundleCollector.assetBundleAssetLists.Count; i++)
- {
- assetList = assetBundleCollector.assetBundleAssetLists[i];
- if (assetList.dir == dirPath)
- {
- foreach(var item in assetList.list)
- {
- if(item.key == itemKey)
- {
- //新增文件直接加进去
- if (!item.list.Contains(assetPath))
- {
- item.list.Add(assetPath);
- var fileSize = GetFileSize(assetPath);
- item.bytes += fileSize;
- assetList.bytes += fileSize;
- if (assetList.bytes >= LimitByteSize)
- {
- assetList.status = 1;
- }
- }
- return;
- }
- }
- }
- }
- //先计算每个item的大小缓存起来(item指必须打在一起的若干文件)
- itemsSizeDicDic.TryGetValue(dirPath, out itemsSizeDic);
- if (itemsSizeDic == null)
- {
- itemsSizeDic = new Dictionary<string, AssetBundleAssetListItem>();
- itemsSizeDicDic[dirPath] = itemsSizeDic;
- }
- itemsSizeDic.TryGetValue(itemKey, out newItem);
- if (newItem == null)
- {
- newItem = new AssetBundleAssetListItem();
- newItem.key = itemKey;
- newItem.list = new List<string>();
- itemsSizeDic[itemKey] = newItem;
- }
- newItem.list.Add(assetPath);
- if(autoMerge)
- {
- newItem.bytes += GetFileSize(assetPath);
- }
- else
- {
- newItem.bytes = LimitByteSize;
- }
- }
- public static void CollectNewItems()
- {
- foreach(var t in itemsSizeDicDic)
- {
- CollectNewItems(t.Key, t.Value);
- }
- }
- public static void CollectNewItems(string dirPath, Dictionary<string, AssetBundleAssetListItem> itemsSizeDic)
- {
- foreach(var newItem in itemsSizeDic.Values)
- {
- bool toNew = true;
- AssetBundleAssetList assetList;
- //先找已存在的AB
- for (var i = 0; i < assetBundleCollector.assetBundleAssetLists.Count; i++)
- {
- assetList = assetBundleCollector.assetBundleAssetLists[i];
- if (assetList.dir == dirPath)
- {
- if (assetList.status <= 0)
- {
- if (assetList.bytes < LimitByteSize && newItem.bytes < LimitByteSize)
- {
- assetList.list.Add(newItem);
- assetList.bytes += newItem.bytes;
- if (assetList.bytes >= LimitByteSize)
- {
- assetList.status = 1;
- }
- toNew = false;
- }
- }
- }
- }
- if(toNew)
- {
- //新建一个AB
- assetList = new AssetBundleAssetList();
- assetList.dir = dirPath;
- assetList.list = new List<AssetBundleAssetListItem>();
- assetList.list.Add(newItem);
- assetList.bytes += newItem.bytes;
- if (assetList.bytes >= LimitByteSize)
- {
- assetList.status = 1;
- }
- assetBundleCollector.assetBundleAssetLists.Add(assetList);
- }
- }
- }
- private static void RecordFileBundle()
- {
- fileBundleDic = new Dictionary<string, string>();
- AssetBundleAssetList assetList;
- for (var i = 0; i < assetBundleCollector.assetBundleAssetLists.Count; i++)
- {
- assetList = assetBundleCollector.assetBundleAssetLists[i];
- foreach (var item in assetList.list)
- {
- foreach (var assetPath in item.list)
- {
- var bundleName = assetList.dir + "_" + i;
- fileBundleDic[assetPath] = bundleName;
- //UnityEngine.Debug.Log($"assetPath :{assetPath}");
- //UnityEngine.Debug.Log($"bundleName :{bundleName}");
- }
- }
- }
- }
- public static int GetFileSize(string filePath)
- {
- //var filePath = Path.Combine(Environment.CurrentDirectory, assetPath);
- //filePath = filePath.Replace("\\", "/");
- var ext = Path.GetExtension(filePath);
- byte[] bytes = File.ReadAllBytes(filePath);
- var fileSize = bytes.Length;
- if (ext == ".jpg")
- {
- //unity处理jpg后会变的很大
- fileSize *= 5;
- }
- return fileSize;
- }
- public static string GetBundleName(string assetPath)
- {
- string bundleName = null;
- if (fileBundleDic != null)
- {
- fileBundleDic.TryGetValue(assetPath, out bundleName);
- }
- return bundleName;
- }
- public static bool IsIgnoreFileExtension(string extension)
- {
- return excludeFileExtensions.Contains(extension);
- }
- public static List<string> CopyFilesTo(string sourcePath, string targetPath, string[] includeExtensionNames = null)
- {
- if(!sourcePath.EndsWith("/"))
- {
- sourcePath += "/";
- }
- if (!targetPath.EndsWith("/"))
- {
- targetPath += "/";
- }
- List<string> result = new List<string>();
- if (Directory.Exists(sourcePath))
- {
- var files = Directory.GetFiles(sourcePath);
- var dirs = Directory.GetDirectories(sourcePath);
- foreach (var dir in dirs)
- {
- List<string> tempResult = CopyFilesTo(dir, targetPath, includeExtensionNames);
- result.AddRange(tempResult);
- }
- foreach (var file in files)
- {
- var fileName = Path.GetFileName(file);
- string extensionName = Path.GetExtension(file);
- if (includeExtensionNames == null || Array.IndexOf(includeExtensionNames, extensionName) >= 0)
- {
- string targetFilePath = targetPath + fileName;
- if (!File.Exists(targetFilePath) || !Equals(file, targetFilePath))
- {
- File.Copy(file, targetFilePath, true);
- result.Add(file);
- }
- }
- }
- }
- return result;
- }
- }
- }
|