| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333 |
- using System;
- using UnityEngine;
- using UnityEditor;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Security.Cryptography;
- using Game.Config;
- /// <summary>
- /// 构建ab包
- /// </summary>
- public class AssetBundleMap : EditorWindow
- {
- /// <summary>
- /// shader都打入这个包
- /// </summary>
- private const string ShaderAbName = "shader.unity3d";
- /// <summary>
- /// 材质球都打入这个包
- /// </summary>
- private const string MatAbName = "mat.unity3d";
- /// <summary>
- /// 临时ab生成目录
- /// </summary>
- private static readonly string TempAssetBundlePath = Application.dataPath + "/../assetbundle";
- private static readonly string TempAssetBundleBakPath = Application.dataPath + "/../assetbundleBak";
- private static readonly string DefaultBakDirPath = "Default";
- private static readonly string ManifestFileExtension = ".manifest";
- /// <summary>
- /// 使用lz4压缩
- /// </summary>
- private static readonly BuildAssetBundleOptions BuildOptions = BuildAssetBundleOptions.DeterministicAssetBundle |
- BuildAssetBundleOptions.ChunkBasedCompression;
- /// <summary>
- /// 构建全部资源
- /// </summary>
- [MenuItem("AssetBundle/BuildMap/BuildAllAssetBundles")]
- public static void BuildAllAssetBundles(VersionCode resVersionCode)
- {
- Debug.Log("<color=green>================ BuildAllAssetBundles Start================</color>");
- AssetDatabase.Refresh();
- copyBakAssetsToAssetBundle();
- AssetBundleUtil.CleanUnusedAB();
- var buildMap = getAllBuildMap(true);
- var abm = BuildPipeline.BuildAssetBundles(TempAssetBundlePath, buildMap, BuildOptions,
- EditorUserBuildSettings.activeBuildTarget);
- if (abm != null)
- {
- AssetBundleUtil.GetAllAssetsNameInAssetBundle(abm, resVersionCode);
- copyAssetBundleToBakAssets();
- copyBakAssetsToStreamingAssets();
- Debug.Log("<color=green>================ BuildAllAssetBundles Success================</color>");
- }
- else
- {
- Debug.Log("<color=green>================ BuildAllAssetBundles Fail================</color>");
- throw new Pack.PackException("BuildAssetBundles 失败");
- }
- }
- /// <summary>
- /// 只打包lua相关的
- /// 在只修改了lua代码,但是未修改资源的情况下使用
- /// </summary>
- [MenuItem("AssetBundle/Build Map/Build Lua AssetBundles")]
- public static void BuildLuaAssetBundles(VersionCode resVersionCode)
- {
- AssetDatabase.Refresh();
- copyBakAssetsToAssetBundle();
- AssetBundleUtil.CleanUnusedAB();
- var buildMap = getLuaBuildMap(true);
- var abm = BuildPipeline.BuildAssetBundles(TempAssetBundlePath, buildMap, BuildOptions,
- EditorUserBuildSettings.activeBuildTarget);
- if (abm == null)
- {
- throw new Pack.PackException("BuildAssetBundles 失败");
- }
- AssetBundleUtil.GetAllAssetsNameInAssetBundle(abm, resVersionCode);
- copyAssetBundleToBakAssets();
- copyBakAssetsToStreamingAssets();
- }
- private static void copyBakAssetsToAssetBundle()
- {
- if (Directory.Exists(TempAssetBundlePath))
- {
- Directory.Delete(TempAssetBundlePath, true);
- }
- Directory.CreateDirectory(TempAssetBundlePath);
- string path = AssetsObscureUtil.GetUniqueValue();
- string dirPath;
- BuildTarget buildTarget = EditorUserBuildSettings.activeBuildTarget;
- if (!s_AssetbundleSavePaths.ContainsKey(buildTarget))
- {
- return;
- }
- if (string.IsNullOrEmpty(path))
- {
- dirPath = TempAssetBundleBakPath + s_AssetbundleSavePaths[buildTarget] + DefaultBakDirPath;
- }
- else
- {
- dirPath = TempAssetBundleBakPath + s_AssetbundleSavePaths[buildTarget] + path;
- }
- if (!Directory.Exists(dirPath))
- {
- return;
- }
- foreach (string file in Directory.GetFiles(dirPath))
- {
- string fileName = Path.GetFileName(file);
- if (fileName == "assetbundle" || Path.GetExtension(file) == ".manifest")
- {
- string des = Path.Combine(TempAssetBundlePath, fileName);
- File.Copy(file, des, true);
- }
- }
- }
- private static void copyAssetBundleToBakAssets()
- {
- string path = AssetsObscureUtil.GetUniqueValue();
- string dirPath;
- BuildTarget buildTarget = EditorUserBuildSettings.activeBuildTarget;
- if (!s_AssetbundleSavePaths.ContainsKey(buildTarget))
- {
- return;
- }
- if (string.IsNullOrEmpty(path))
- {
- dirPath = TempAssetBundleBakPath + s_AssetbundleSavePaths[buildTarget] + DefaultBakDirPath;
- }
- else
- {
- dirPath = TempAssetBundleBakPath + s_AssetbundleSavePaths[buildTarget] + path;
- }
- if (!Directory.Exists(dirPath))
- {
- Directory.CreateDirectory(dirPath);
- }
- foreach (string file in Directory.GetFiles(TempAssetBundlePath))
- {
- string fileName = Path.GetFileName(file);
- string des = Path.Combine(dirPath, fileName);
- File.Copy(file, des, true);
- }
- }
- /// <summary>
- /// 拷贝打包好的资源到目标目录
- /// </summary>
- private static void copyBakAssetsToStreamingAssets()
- {
- string path = string.Empty;
- if (Directory.Exists(Application.streamingAssetsPath))
- {
- foreach (var item in s_AssetbundleSavePaths)
- {
- path = Application.streamingAssetsPath + item.Value;
- DeleteDirectoryAssets(path, true);
- }
- }
- BuildTarget buildTarget = EditorUserBuildSettings.activeBuildTarget;
- if (!s_AssetbundleSavePaths.ContainsKey(buildTarget))
- {
- return;
- }
- string assetBundlePath = Application.streamingAssetsPath + s_AssetbundleSavePaths[buildTarget];
- if (!Directory.Exists(assetBundlePath))
- {
- Directory.CreateDirectory(assetBundlePath);
- }
- path = AssetsObscureUtil.GetUniqueValue();
- string dirPath;
- if (string.IsNullOrEmpty(path))
- {
- dirPath = TempAssetBundleBakPath + s_AssetbundleSavePaths[buildTarget] + DefaultBakDirPath;
- }
- else
- {
- dirPath = TempAssetBundleBakPath + s_AssetbundleSavePaths[buildTarget] + path;
- }
- // 拷贝临时打包目录中的ab资源到对应的StreamingAssets目录下
- foreach (string file in Directory.GetFiles(dirPath))
- {
- if (Path.GetExtension(file) != ".manifest")
- {
- string fileName = Path.GetFileName(file);
- if (fileName == "assetbundle")
- {
- fileName = AssetsObscureUtil.GetABFileName(fileName);
- LogABName("assetbundle", fileName);
- }
- else if (fileName == $"{ GetAssetsMappingName()}.bytes")
- {
- EncryptAssetsmapping(file, Path.Combine(assetBundlePath, fileName));
- continue;
- }
- string des = Path.Combine(assetBundlePath, fileName);
- File.Copy(file, des, true);
- WriteMeaninglessDataToFile(des);
- }
- }
- AssetDatabase.Refresh();
- }
- private static object GetAssetsMappingName()
- {
- #if UNITY_IOS
- return "afi";
- #else
- return "assetsmapping";
- #endif
- }
- private static Dictionary<BuildTarget, string> s_AssetbundleSavePaths = new Dictionary<BuildTarget, string>()
- {
- {BuildTarget.iOS, "/unityRes/"},
- {BuildTarget.Android, "/AssetsAndroid/"},
- {BuildTarget.StandaloneWindows64, "/AssetsPC/"},
- {BuildTarget.WebGL, "/AssetsPC/"},
- };
- private static void EncryptAssetsmapping(string destPath, string filePath)
- {
- byte[] bytes = File.ReadAllBytes(destPath);
- byte value;
- int length = bytes.Length;
- for (int i = 0, iMax = Mathf.FloorToInt(length * 0.5f); i < iMax; i += 2)
- {
- value = bytes[i];
- bytes[i] = bytes[length - i - 1];
- bytes[length - i - 1] = value;
- }
- using (var fs = File.Open(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite))
- {
- fs.Write(bytes, 0, length);
- }
- }
- /// <summary>
- /// 这个函数不能适配所有文件大小,理论上来说需要文件小于 (int.MaxValue / 2) byte
- /// </summary>
- /// <param name="filePath"></param>
- private static void WriteMeaninglessDataToFile(string filePath)
- {
- if (!AssetsObscureUtil.IsObscure()) return;
- string fileName = Path.GetFileNameWithoutExtension(filePath);
- ulong offset = AssetsObscureUtil.GetABOffset(fileName);
- if (offset <= 0) return;
- uint offsetMain = (uint)AssetsObscureUtil.GetObscureOffsetMin();
- if (offset <= offsetMain) return;
- byte[] bytes = File.ReadAllBytes(filePath);
- int length = bytes.Length;
- if (length <= 0) return;
- byte[] offsetBytes = new byte[offset];
- Array.Copy(bytes, offsetBytes, offsetMain);
- byte[] md5Bytes;
- using(var md5 = new MD5CryptoServiceProvider())
- {
- UTF8Encoding encoding = new UTF8Encoding(false);
- md5Bytes = md5.ComputeHash(bytes);
- }
- uint md5Length = (uint)md5Bytes.Length;
- int idx = 0;
- for (ulong i = offsetMain + 1,
- iMax = (offset - offsetMain);
- i < iMax; i++)
- {
- idx = idx + md5Bytes[i % md5Length];
- if (idx >= length - 1)
- {
- idx = idx - length + 1;
- }
- offsetBytes[i] = bytes[idx];
- }
- using (FileStream fs = File.Open(filePath, FileMode.Open, FileAccess.ReadWrite))
- {
- fs.Write(offsetBytes, 0, (int)offset);
- fs.Write(bytes, 0, length);
- }
- }
- private struct AssetMap
- {
- /// <summary>
- /// key => ab name
- /// value => asset names
- /// </summary>
- public Dictionary<string, HashSet<string>> Maps;
- /// <summary>
- /// 记录重复的情况
- /// </summary>
- public HashSet<string> AllAssets;
- }
- public static CheckResWindowInfo GetCheckResWindowInfo()
- {
- string path = "Assets/Editor/AssetBundle/CheckHotResWindownInfo.asset";
- return AssetDatabase.LoadAssetAtPath<CheckResWindowInfo>(path);
- }
- public static Dictionary<string, LanguageAssetBundleName> GetAbNameCfg()
- {
- Dictionary<string, LanguageAssetBundleName> ret = new Dictionary<string, LanguageAssetBundleName>();
- string path = "Assets/Editor/AssetBundle/abname.csv";
- TextAsset ta = AssetDatabase.LoadAssetAtPath<TextAsset>(path);
- CsvReader csvReader = new CsvReader(LanguageAssetBundleName.FileName_S(), ta.bytes);
- LanguageAssetBundleName.OnCsvLoad(csvReader);
- LanguageAssetBundleName.Foreach(it =>
- {
- ret.Add(it.BaseName, it);
- //Debug.Log(it.BaseName);
- });
- LanguageAssetBundleName.Clear();
- return ret;
- }
- /// <summary>
- /// 构建全部资源
- /// </summary>
- /// <param name="showProcessBar"></param>
- /// <returns></returns>
- private static AssetBundleBuild[] getAllBuildMap(bool showProcessBar)
- {
- if (showProcessBar)
- EditorUtility.DisplayProgressBar("Generate Bundle Build Map", "Start...", 0.1f);
- AssetMap maps = new AssetMap();
- maps.Maps = new Dictionary<string, HashSet<string>>(1024);
- maps.AllAssets = new HashSet<string>();
- // 增加公共shader
- addBuildAssetsCommon(maps, ShaderAbName, Constants.ShaderDir, "*.shader");
- addBuildAssetsCommon(maps, ShaderAbName, Constants.ShaderDir, "*.shadervariants");
- // font
- if (showProcessBar)
- EditorUtility.DisplayProgressBar("Generate Bundle Build Map", "Font...", 0.2f);
- addBuildAssetsFonts(maps);
- addBuildAssetsCommon(maps, "commonmat.unity3d", Constants.CommonMaterialDir, "*.mat");
- // config
- if (showProcessBar)
- EditorUtility.DisplayProgressBar("Generate Bundle Build Map", "Config...", 0.3f);
- addBuildAssetsCommon(maps, "config.unity3d", Constants.CsvConfig, "*.csv");
- addBuildAssetsCommon(maps, "xml.unity3d", Constants.XmlConfig, "*.xml");
- // audio
- if (showProcessBar)
- EditorUtility.DisplayProgressBar("Generate Bundle Build Map", "Audio...", 0.4f);
- addBuildAssetsBGM(maps);
- addBuildAssetsCommon(maps, "UI_Audio.unity3d", Constants.UIAudioPath, "*.ogg");
- addBuildAssetsCommon(maps, "Fight_Audio.unity3d", Constants.FightAudioPath, "*.ogg");
- if (showProcessBar)
- EditorUtility.DisplayProgressBar("Generate Bundle Build Map", "Icons...", 0.5f);
- addBuildAssetsIcons(maps);
- if (showProcessBar)
- EditorUtility.DisplayProgressBar("Generate Bundle Build Map", "Camera...", 0.6f);
- addBuildAssetsCamera(maps);
- if (showProcessBar)
- EditorUtility.DisplayProgressBar("Generate Bundle Build Map", "Animator...", 0.65f);
- addBuildAssetsAnimator(maps);
- if (showProcessBar)
- EditorUtility.DisplayProgressBar("Generate Bundle Build Map", "Actor...", 0.7f);
- addBuildAssetsActor(maps);
- if (showProcessBar)
- EditorUtility.DisplayProgressBar("Generate Bundle Build Map", "Effect...", 0.75f);
- addBuildAssetsEffect(maps);
- if (showProcessBar)
- EditorUtility.DisplayProgressBar("Generate Bundle Build Map", "UI...", 0.8f);
- addBuildAssetsUI(maps);
- if (showProcessBar)
- EditorUtility.DisplayProgressBar("Generate Bundle Build Map", "Scene...", 0.85f);
- addBuildAssetsScene(maps);
- if (showProcessBar)
- EditorUtility.DisplayProgressBar("Generate Bundle Build Map", "Lua...", 0.9f);
- addBuildAssetsLua(maps);
- Dictionary<string, LanguageAssetBundleName> abnamecfg = GetAbNameCfg();
-
- List<AssetBundleBuild> bm = new List<AssetBundleBuild>(maps.Maps.Count);
- foreach (var items in maps.Maps)
- {
- AssetBundleBuild bundle = new AssetBundleBuild();
- string abname = AssetsObscureUtil.GetABFileName(items.Key);
- CheckResWindowInfo info = GetCheckResWindowInfo();
- if (abnamecfg.ContainsKey(abname))
- {
- if (info.Language == BuildLanguage.cn)
- {
- bundle.assetBundleName = abnamecfg[abname].BaseName;
- }
- else
- {
- bundle.assetBundleName = abnamecfg[abname].En;
- }
- }
- else
- {
- bundle.assetBundleName = abname;
- }
- bundle.assetNames = items.Value.ToArray();
- LogABName(items.Key,bundle.assetBundleName);
- if (bundle.assetNames.Length == 0)
- {
- Debug.LogWarning(items.Key + " empty assetNames");
- continue;
- }
- bm.Add(bundle);
- }
- if (showProcessBar)
- {
- EditorUtility.DisplayProgressBar("Generate Bundle Build Map", "Finish...", 1f);
- EditorUtility.ClearProgressBar();
- }
- return bm.ToArray();
- }
- static string[] logNames = new string[] { "font.unity3d", "shader.unity3d", "UITexture_UILanuch.unity3d" , "UITexture_UILoading.unity3d", "UILanuch_uiprefab.unity3d", "UILoading_uiprefab.unity3d", "assetbundle" };
- private static void LogABName(string name,string abname)
- {
- if (logNames.Contains(name))
- {
- Debug.Log($"=========== 包名:【{name}】 ab包名【{abname}】");
- }
- else
- {
- Debug.Log($"包名:【{name}】 ab包名【{abname}】");
- }
- }
- /// <summary>
- /// 只构建lua相关的资源
- /// </summary>
- /// <param name="showProcessBar"></param>
- /// <returns></returns>
- private static AssetBundleBuild[] getLuaBuildMap(bool showProcessBar)
- {
- if (showProcessBar)
- EditorUtility.DisplayProgressBar("Generate Bundle Build Map", "Start...", 0.1f);
- AssetMap maps = new AssetMap();
- maps.Maps = new Dictionary<string, HashSet<string>>();
- maps.AllAssets = new HashSet<string>();
- if (showProcessBar)
- EditorUtility.DisplayProgressBar("Generate Bundle Build Map", "Lua...", 0.9f);
- addBuildAssetsCommon(maps, "config.unity3d", Constants.CsvConfig, "*.csv");
- addBuildAssetsLua(maps);
- List<AssetBundleBuild> bm = new List<AssetBundleBuild>(maps.Maps.Count);
- foreach (var items in maps.Maps)
- {
- AssetBundleBuild bundle = new AssetBundleBuild();
- bundle.assetBundleName = items.Key;
- bundle.assetNames = items.Value.ToArray();
- LogABName(items.Key, bundle.assetBundleName);
- if (bundle.assetNames.Length == 0)
- {
- Debug.LogWarning(items.Key + " empty assetNames");
- continue;
- }
- bm.Add(bundle);
- }
- if (showProcessBar)
- {
- EditorUtility.DisplayProgressBar("Generate Bundle Build Map", "Finish...", 1f);
- EditorUtility.ClearProgressBar();
- }
- bm.Sort(assetBundleBuildSort);
- return bm.ToArray();
- }
- private static AssetBundleBuildSort s_AssetBundleBuildSort = null;
- private static AssetBundleBuildSort assetBundleBuildSort
- {
- get
- {
- if (s_AssetBundleBuildSort == null)
- {
- s_AssetBundleBuildSort = new AssetBundleBuildSort();
- }
- return s_AssetBundleBuildSort;
- }
- }
- private class AssetBundleBuildSort : IComparer<AssetBundleBuild>
- {
- public int Compare(AssetBundleBuild x, AssetBundleBuild y)
- {
- return string.Compare(x.assetBundleName, y.assetBundleName);
- }
- }
- #region 处理各个模块的assetbundle
- private static void addBuildAssetsCommon(AssetMap maps, string abName, string dir,
- string filter)
- {
- string[] fileList = FileUtils.TraverseAllFiles(dir, filter);
- for (int idx = 0; idx < fileList.Length; idx++)
- {
- string fullPath = fileList[idx];
- string relativePath = FileUtils.ExtractAssetRelativePath(fullPath);
- if (relativePath.EndsWith(".meta")) continue;
- addData(maps, abName, relativePath);
- }
- }
- private static void addData(AssetMap maps, string assetBundleName, string assetName)
- {
- // 重复资源不能加入;按照资源初始化顺序写入
- if (maps.AllAssets.Contains(assetName))
- {
- return;
- }
- maps.AllAssets.Add(assetName);
- if (!maps.Maps.ContainsKey(assetBundleName))
- {
- maps.Maps[assetBundleName] = new HashSet<string>();
- }
- maps.Maps[assetBundleName].Add(assetName);
- }
- private static void addBuildAssetsFonts(AssetMap maps)
- {
- string[] fileList = FileUtils.TraverseFiles(Constants.FontDir, "*.*");
- for (int idx = 0; idx < fileList.Length; idx++)
- {
- string fullPath = fileList[idx];
- string relativePath = FileUtils.ExtractAssetRelativePath(fullPath);
- if (relativePath.Contains(".meta")) continue;
- addData(maps, "font.unity3d", relativePath);
- }
- string[] dirList = Directory.GetDirectories(Constants.FontDir);
- for (int idx = 0; idx < dirList.Length; idx++)
- {
- string fullPath = dirList[idx];
- string relativePath = FileUtils.ExtractAssetRelativePath(fullPath);
- string abName = relativePath.Replace("Assets/", "").Replace("/", "_") + ".unity3d";
- string[] files = FileUtils.TraverseFiles(relativePath, "*.*");
- for (int jdx = 0; jdx < files.Length; jdx++)
- {
- string filePathName = files[jdx];
- if (filePathName.Contains(".meta")) continue;
- string fileRelativePath = FileUtils.ExtractAssetRelativePath(filePathName);
- addData(maps, abName, fileRelativePath);
- }
- }
- }
- private static void addBuildAssetsBGM(AssetMap maps)
- {
- string[] files = FileUtils.TraverseAllFiles(Constants.BGMAudioPath, "*.ogg");
- for (int idx = 0; idx < files.Length; idx++)
- {
- string fullPath = files[idx];
- string relativePath = FileUtils.ExtractAssetRelativePath(fullPath);
- string abName = FileUtils.RemoveExtension(FileUtils.ExtractPureName(relativePath)) + ".unity3d";
- addData(maps, abName, relativePath);
- }
- }
- private static void addBuildAssetsIcons(AssetMap maps)
- {
- string[] allAbNames = AssetDatabase.GetAllAssetBundleNames();
- string[] dirs = Directory.GetDirectories(Constants.IconDir, "*", SearchOption.AllDirectories);
- for (int idx = 0; idx < dirs.Length; idx++)
- {
- string dirName = dirs[idx];
- string abName = FileUtils.ExtractPureName(dirName) + "_icons.unity3d";
- if (allAbNames.Contains(abName))
- {
- AssetDatabase.RemoveAssetBundleName(abName, true);
- }
- string[] files = FileUtils.TraverseAllFiles(dirName, "*.png");
- for (int jdx = 0; jdx < files.Length; jdx++)
- {
- string fullPath = files[jdx];
- string relativePath = FileUtils.ExtractAssetRelativePath(fullPath);
- addData(maps, abName, relativePath);
- string[] dependencyAssets = AssetDatabase.GetDependencies(relativePath);
- for (int kdx = 0; kdx < dependencyAssets.Length; kdx++)
- {
- string dependencyAssetName = dependencyAssets[kdx];
- string fileType = dependencyAssetName.Substring(dependencyAssetName.LastIndexOf('.') + 1);
- fileType = fileType.ToLower();
- if (fileType == "shader")
- {
- addData(maps, ShaderAbName, dependencyAssetName);
- }
- else if (fileType == "mat")
- {
- addData(maps, MatAbName, dependencyAssetName);
- }
- else if (fileType != "cs")
- {
- addData(maps, abName, dependencyAssetName);
- }
- }
- }
- }
- }
- private static void addBuildAssetsAnimator(AssetMap maps)
- {
- string[] dirs = Directory.GetDirectories(Constants.AnimatorPath, "*", SearchOption.AllDirectories);
- for (int i = 0; i < dirs.Length; i++)
- {
- string dirName = dirs[i];
- string[] files = FileUtils.TraverseAllFiles(dirName, "*.controller");
- for (int idx = 0; idx < files.Length; idx++)
- {
- string fullPath = files[idx];
- string fileName = FileUtils.ExtractPureName(fullPath);
- string[] tempList = fileName.Split('_');
- string abName = tempList[0] + "_animator.unity3d";
- string relativePath = FileUtils.ExtractAssetRelativePath(fullPath);
- addData(maps, abName, relativePath);
- string[] dependencyAssets = AssetDatabase.GetDependencies(relativePath);
- for (int jdx = 0; jdx < dependencyAssets.Length; jdx++)
- {
- string dependencyAssetName = dependencyAssets[jdx];
- string fileType = dependencyAssetName.Substring(dependencyAssetName.LastIndexOf('.') + 1);
- fileType = fileType.ToLower();
- if (fileType == "shader")
- {
- addData(maps, ShaderAbName, dependencyAssetName);
- }
- else if (fileType == "mat")
- {
- addData(maps, MatAbName, dependencyAssetName);
- }
- else if (fileType != "cs")
- {
- addData(maps, abName, dependencyAssetName);
- }
- }
- }
- }
- }
- private static void addBuildAssetsActor(AssetMap maps)
- {
- string[] dirs = Directory.GetDirectories(Constants.ModelPath, "*", SearchOption.AllDirectories);
- for (int i = 0; i < dirs.Length; i++)
- {
- string dirName = dirs[i];
- string abName = FileUtils.ExtractPureName(dirName) + ".unity3d";
- string textureABName = FileUtils.ExtractPureName(dirName) + "_texture.unity3d";
- string fbxABName = FileUtils.ExtractPureName(dirName) + "_model.unity3d";
- string[] files = FileUtils.TraverseAllFiles(dirName, "*.prefab");
- for (int idx = 0; idx < files.Length; idx++)
- {
- string fullPath = files[idx];
- string relativePath = FileUtils.ExtractAssetRelativePath(fullPath);
- addData(maps, abName, relativePath);
- string[] dependencyAssets = AssetDatabase.GetDependencies(relativePath);
- for (int jdx = 0; jdx < dependencyAssets.Length; jdx++)
- {
- string dependencyAssetName = dependencyAssets[jdx];
- string fileType = dependencyAssetName.Substring(dependencyAssetName.LastIndexOf('.') + 1);
- fileType = fileType.ToLower();
- if (fileType == "shader")
- {
- addData(maps, ShaderAbName, dependencyAssetName);
- }
- else if (fileType == "mat")
- {
- addData(maps, MatAbName, dependencyAssetName);
- }
- else if (fileType == "jpg" || fileType == "png" || fileType == "tga" || fileType == "tif" ||
- fileType == "psd")
- {
- addData(maps, textureABName, dependencyAssetName);
- }
- else if (fileType == "fbx")
- {
- addData(maps, fbxABName, dependencyAssetName);
- }
- else if (fileType != "cs")
- {
- addData(maps, abName, dependencyAssetName);
- }
- }
- }
- }
- }
- private static void addBuildAssetsCamera(AssetMap maps)
- {
- string[] files = FileUtils.TraverseAllFiles("Assets/Content/Prefabs/Camera", "*.prefab");
- for (int idx = 0; idx < files.Length; idx++)
- {
- string fullPath = files[idx];
- string relativePath = FileUtils.ExtractAssetRelativePath(fullPath);
- string abName = "prefab_camera.unity3d";
- addData(maps, abName, relativePath);
- string[] dependencyAssets = AssetDatabase.GetDependencies(relativePath);
- for (int jdx = 0; jdx < dependencyAssets.Length; jdx++)
- {
- string dependencyAssetName = dependencyAssets[jdx];
- string fileType = dependencyAssetName.Substring(dependencyAssetName.LastIndexOf('.') + 1);
- fileType = fileType.ToLower();
- if (fileType == "shader")
- {
- addData(maps, ShaderAbName, dependencyAssetName);
- }
- else if (fileType == "mat")
- {
- addData(maps, MatAbName, dependencyAssetName);
- }
- else if (fileType != "cs")
- {
- addData(maps, abName, dependencyAssetName);
- }
- }
- }
- }
- private static void addBuildAssetsEffect(AssetMap maps)
- {
- string effectTextureABName = "effect_texture.unity3d";
- string effectAnimABName = "effect_dep.unity3d";
- string abName = "effect.unity3d";
- string[] dirs = Directory.GetDirectories(Constants.EffectPath, "*", SearchOption.AllDirectories);
- for (int idx = 0; idx < dirs.Length; idx++)
- {
- string dirName = dirs[idx];
- string[] fileList = FileUtils.TraverseAllFiles(dirName, "*.prefab");
- for (int jdx = 0; jdx < fileList.Length; jdx++)
- {
- string fullPath = fileList[jdx];
- string relativePath = FileUtils.ExtractAssetRelativePath(fullPath);
- addData(maps, abName, relativePath);
- string[] dependencyAssets = AssetDatabase.GetDependencies(relativePath);
- for (int kdx = 0; kdx < dependencyAssets.Length; kdx++)
- {
- string dependencyAssetName = dependencyAssets[kdx];
- string fileType = dependencyAssetName.Substring(dependencyAssetName.LastIndexOf('.') + 1);
- fileType = fileType.ToLower();
- if (fileType == "shader")
- {
- addData(maps, ShaderAbName, dependencyAssetName);
- }
- else if (fileType == "mat")
- {
- addData(maps, MatAbName, dependencyAssetName);
- }
- else if (fileType == "jpg" || fileType == "png" || fileType == "tga" || fileType == "tif" ||
- fileType == "psd")
- {
- addData(maps, effectTextureABName, dependencyAssetName);
- }
- else if (fileType != "cs")
- {
- addData(maps, effectAnimABName, dependencyAssetName);
- }
- }
- }
- }
- }
- private static void addBuildAssetsUI(AssetMap maps)
- {
- addBuildAssets3DUIPrefab(maps);
- addBuildAssetsUIPrefab(maps);
- }
- private static void addBuildAssets3DUIPrefab(AssetMap maps)
- {
- string[] allAbNames = AssetDatabase.GetAllAssetBundleNames();
- string[] dirs = Directory.GetDirectories(Constants.UI3DPath, "*", SearchOption.AllDirectories);
- for (int idx = 0; idx < dirs.Length; idx++)
- {
- string dirName = dirs[idx];
- string abName = FileUtils.ExtractPureName(dirName) + "_3duiprefab.unity3d";
- string textureABName = FileUtils.ExtractPureName(dirName) + "_3duiprefab_texture.unity3d";
- if (allAbNames.Contains(abName))
- {
- AssetDatabase.RemoveAssetBundleName(abName, true);
- }
- string[] files = FileUtils.TraverseAllFiles(dirName, "*.prefab");
- for (int jdx = 0; jdx < files.Length; jdx++)
- {
- string fullPath = files[jdx];
- string relativePath = FileUtils.ExtractAssetRelativePath(fullPath);
- addData(maps, abName, relativePath);
- string[] dependencyAssets = AssetDatabase.GetDependencies(relativePath);
- for (int kdx = 0; kdx < dependencyAssets.Length; kdx++)
- {
- string dependencyAssetName = dependencyAssets[kdx];
- string fileType = dependencyAssetName.Substring(dependencyAssetName.LastIndexOf('.') + 1);
- fileType = fileType.ToLower();
- if (fileType == "shader")
- {
- addData(maps, ShaderAbName, dependencyAssetName);
- }
- else if (fileType == "mat")
- {
- addData(maps, MatAbName, dependencyAssetName);
- }
- else if (fileType == "jpg" || fileType == "png" || fileType == "tga" || fileType == "tif" ||
- fileType == "psd")
- {
- addData(maps, textureABName, dependencyAssetName);
- }
- else if (fileType != "cs")
- {
- addData(maps, abName, dependencyAssetName);
- }
- }
- }
- }
- }
- private static void addBuildAssetsUIPrefab(AssetMap maps)
- {
- string[] allAbNames = AssetDatabase.GetAllAssetBundleNames();
- string[] dirs = Directory.GetDirectories(Constants.UIPath, "*", SearchOption.AllDirectories);
- for (int idx = 0; idx < dirs.Length; idx++)
- {
- string dirName = dirs[idx];
- string dirRelativeName = FileUtils.ExtractAssetRelativePath(dirName);
- string abName = "";
- if (dirRelativeName == Constants.UICommonPath)
- {
- abName = "prefabsuicommon.unity3d";
- }
- else
- {
- abName = FileUtils.ExtractPureName(dirName) + "_uiprefab.unity3d";
- }
- if (allAbNames.Contains(abName))
- {
- AssetDatabase.RemoveAssetBundleName(abName, true);
- }
- string[] files = FileUtils.TraverseAllFiles(dirName, "*.prefab");
- for (int jdx = 0; jdx < files.Length; jdx++)
- {
- string fullPath = files[jdx];
- string relativePath = FileUtils.ExtractAssetRelativePath(fullPath);
- addData(maps, abName, relativePath);
- string rootParent;
- FileUtils.ExtractParent(relativePath, out rootParent);
- string[] dependencyAssets = AssetDatabase.GetDependencies(relativePath);
- for (int kdx = 0; kdx < dependencyAssets.Length; kdx++)
- {
- string dependencyAssetName = dependencyAssets[kdx];
- string fileType = dependencyAssetName.Substring(dependencyAssetName.LastIndexOf('.') + 1);
- fileType = fileType.ToLower();
- if (fileType == "shader")
- {
- addData(maps, ShaderAbName, dependencyAssetName);
- }
- else if (fileType == "mat")
- {
- addData(maps, MatAbName, dependencyAssetName);
- }
- else if (fileType == "jpg" || fileType == "png" || fileType == "tga" || fileType == "tif" ||
- fileType == "psd")
- {
- string parent;
- FileUtils.ExtractParent(dependencyAssetName, out parent);
- parent = FileUtils.ExtractPureName(parent);
- string uiTextureABName = "UITexture_" + parent + ".unity3d";
- addData(maps, uiTextureABName, dependencyAssetName);
- }
- else if (fileType == "prefab")
- {
- string parent;
- FileUtils.ExtractParent(dependencyAssetName, out parent);
- if (parent.StartsWith(Constants.UIPath))
- {
- if (rootParent == parent)
- {
- addData(maps, abName, dependencyAssetName);
- }
- }
- else
- {
- addData(maps, abName, dependencyAssetName);
- }
- }
- else if (fileType != "cs")
- {
- addData(maps, abName, dependencyAssetName);
- }
- }
- }
- }
- }
- private static void addBuildAssetsScene(AssetMap maps)
- {
- string[] fileList = Directory.GetFiles(Constants.ScenePath, "*.unity", SearchOption.AllDirectories);
- for (int idx = 0; idx < fileList.Length; idx++)
- {
- string fullPath = fileList[idx];
- if (fullPath.Contains("UIScene") || fullPath.Contains("meta") || fullPath.Contains("SceneCG") ||
- fullPath.Replace('\\', '/').Contains("Scene/Other") || fullPath.Contains("WasteAsset") ||
- fullPath.Contains("Building"))
- continue;
- if (fullPath.Contains("game.unity") || fullPath.Contains("Loading.unity"))
- {
- continue;
- }
- string relativePath = FileUtils.ExtractAssetRelativePath(fullPath);
- string abName = FileUtils.RemoveExtension(FileUtils.ExtractPureName(relativePath)) + ".unity3d";
- addData(maps, abName, relativePath);
- string scenePrefabABName =
- FileUtils.RemoveExtension(FileUtils.ExtractPureName(relativePath)) + "_prefab.unity3d";
- string sceneTextureABName =
- FileUtils.RemoveExtension(FileUtils.ExtractPureName(relativePath)) + "_texture.unity3d";
- string sceneOtherABName =
- FileUtils.RemoveExtension(FileUtils.ExtractPureName(relativePath)) + "_other.unity3d";
- string lightmapABName = FileUtils.RemoveExtension(FileUtils.ExtractPureName(relativePath)) + "_lm.unity3d";
- string[] dependencyAssets = AssetDatabase.GetDependencies(relativePath);
- for (int kdx = 0; kdx < dependencyAssets.Length; kdx++)
- {
- string dependencyAssetName = dependencyAssets[kdx];
- if (dependencyAssetName.Contains("Lightmap-") ||
- dependencyAssetName.Contains("LightingData") ||
- dependencyAssetName.Contains("ReflectionProbe"))
- {
- addData(maps, lightmapABName, dependencyAssetName);
- }
- else if (dependencyAssetName.Contains("PostProcessing") && !dependencyAssetName.Contains(".cs"))
- {
- if (dependencyAssetName.Contains(".shader"))
- {
- addData(maps, ShaderAbName, dependencyAssetName);
- }
- else
- {
- addData(maps, "PostProcessing.unity3d", dependencyAssetName);
- }
- }
- else
- {
- string fileType = dependencyAssetName.Substring(dependencyAssetName.LastIndexOf('.') + 1);
- fileType = fileType.ToLower();
- if (fileType == "shader")
- {
- addData(maps, ShaderAbName, dependencyAssetName);
- }
- else if (fileType == "mat")
- {
- addData(maps, MatAbName, dependencyAssetName);
- }
- else if (fileType == "prefab")
- {
- if (dependencyAssetName.Contains("Scenes/Scene_common"))
- {
- string sCommonAb = "scene_common_prefab.unity3d";
- addData(maps, sCommonAb, dependencyAssetName);
- }
- else
- {
- addData(maps, scenePrefabABName, dependencyAssetName);
- }
- }
- else if (fileType == "png" || fileType == "tga" || fileType == "jpg" || fileType == "tif" ||
- fileType == "psd")
- {
- if (dependencyAssetName.Contains("Scenes/Scene_common"))
- {
- string sCommonAb = "scene_common_texture.unity3d";
- addData(maps, sCommonAb, dependencyAssetName);
- }
- else
- {
- addData(maps, sceneTextureABName, dependencyAssetName);
- }
- }
- else if (fileType != "cs")
- {
- if (dependencyAssetName.Contains("Scenes/Scene_common"))
- {
- string sCommonAb = "scene_common_other.unity3d";
- addData(maps, sCommonAb, dependencyAssetName);
- }
- else
- {
- addData(maps, sceneOtherABName, dependencyAssetName);
- }
- }
- }
- }
- }
- }
- #endregion
- #region 处理lua文件
- private static void addBuildAssetsLua(AssetMap maps)
- {
- CopyLuaAssets(Constants.LuaDir, Constants.ABLuaDir);
- CopyLuaAssets(Constants.LuaLogicDir, Constants.ABLuaLogicDir, true);
- CopyLuaAssets(Constants.LuaPbDir, Constants.ABLuaPbDir);
- CopyLuaAssets(Constants.PubSec, Constants.ABPubsec);
- AssetDatabase.Refresh();
- ProcessConfigAndLua(maps);
- }
- private static void CopyLuaAssets(string sourceDir, string destDir, bool optimize = false)
- {
- string path = Application.dataPath;
- path = path.Replace("Assets", "");
- string sourceAbsDir = Path.Combine(path, sourceDir);
- string destAbsDir = Path.Combine(path, destDir);
- HashSet<string> sourceFiles = getAllFilesPathEX(sourceAbsDir);
- HashSet<string> destFiles = getAllFilesPathEX(destAbsDir);
- // 删除不再需要的文件
- foreach (var destFile in destFiles)
- {
- string sourceFile = destFile.Replace(destAbsDir, sourceAbsDir);
- string fileName = Path.GetExtension(sourceFile);
- if (fileName.ToLower() == ".txt")
- {
- string ext = Path.GetExtension(fileName);
- if (s_ValidExtMap.Contains(ext))
- {
- sourceFile = sourceFile.Remove(sourceFile.Length - 4);
- }
- }
- if (!sourceFiles.Contains(sourceFile))
- {
- File.Delete(destFile);
- string metaFilePath = destFile + ".meta";
- if (File.Exists(metaFilePath))
- {
- File.Delete(metaFilePath);
- }
- string folder = Path.GetDirectoryName(destFile);
- DeleteDirectoryAssets(folder);
- }
- }
- // 替换新文件
- foreach (var filePath in sourceFiles)
- {
- string destFilePath = filePath.Replace(sourceAbsDir, destAbsDir);
- string destFolder = Path.GetDirectoryName(destFilePath);
- if (!Directory.Exists(destFolder))
- {
- Directory.CreateDirectory(destFolder);
- }
- destFilePath = destFilePath + ".txt";
- if (!File.Exists(destFilePath))
- {
- File.Copy(filePath, destFilePath, true);
- if (optimize && destFilePath.Contains("_Generate"))
- {
- string[] lines = FileSystem.ReadFileLines(destFilePath);
- List<string> contents = new List<string>(lines);
- for (var i = contents.Count - 1; i >= 0; --i)
- {
- if (contents[i].StartsWith("---@") || contents[i].Equals(""))
- {
- contents.RemoveAt(i);
- }
- }
- File.WriteAllLines(destFilePath, contents.ToArray());
- }
- }
- else
- {
- if (optimize && destFilePath.Contains("_Generate"))
- {
- string[] lines1 = FileSystem.ReadFileLines(filePath);
- List<string> contents = new List<string>(lines1);
- for (var i = contents.Count - 1; i >= 0; --i)
- {
- if (contents[i].StartsWith("---@") || contents[i].Equals(""))
- {
- contents.RemoveAt(i);
- }
- }
- lines1 = contents.ToArray();
- string[] lines2 = FileSystem.ReadFileLines(destFilePath);
- bool diff = (lines1.Length != lines2.Length);
- if (!diff)
- {
- for (var i = lines1.Length - 1; i >= 0; i--)
- {
- if (lines1[i] != lines2[i])
- {
- diff = true;
- continue;
- }
- }
- }
- if (diff)
- File.WriteAllLines(destFilePath, lines1);
- }
- else
- {
- string content1 = File.ReadAllText(filePath);
- string content2 = File.ReadAllText(destFilePath);
- if (content1 != content2)
- {
- File.Copy(filePath, destFilePath, true);
- }
- }
- }
- }
- }
- private static void DeleteDirectoryAssets(string dir, bool forceDelete = false)
- {
- if (!Directory.Exists(dir)) return;
- var ls = Directory.GetFileSystemEntries(dir);
- if (forceDelete || ls == null || ls.Length <= 0)
- {
- Directory.Delete(dir, true);
- if (dir.EndsWith("/") || dir.EndsWith("\\"))
- {
- dir = dir.Substring(0, dir.Length - 1);
- }
- string metaFilePath = dir + ".meta";
- if (File.Exists(metaFilePath))
- {
- File.Delete(metaFilePath);
- }
- string folder = Path.GetDirectoryName(dir);
- DeleteDirectoryAssets(folder);
- }
- }
- private static void CopyLuaAsset(string dir)
- {
- string path = Application.dataPath;
- path = Path.Combine(path.Replace("Assets", ""), dir);
- List<string> files = FileSystem.getAllFilesPathEX(path);
- foreach (string fileName in files)
- {
- string destName = fileName.Insert(fileName.IndexOf("Assets/") + 7, "Content/");
- string destFolder = Path.GetDirectoryName(destName);
- if (!Directory.Exists(destFolder))
- {
- Directory.CreateDirectory(destFolder);
- }
- File.Copy(fileName, destName + ".txt", true);
- if (dir.CompareTo(Constants.LuaLogicDir) == 0 && destName.Contains("_Generate"))
- {
- string[] lines = FileSystem.ReadFileLines(destName + ".txt");
- List<string> contents = new List<string>(lines);
- for (var i = contents.Count - 1; i >= 0; --i)
- {
- if (contents[i].StartsWith("---@") || contents[i].Equals(""))
- {
- contents.RemoveAt(i);
- }
- }
- File.WriteAllLines(destName + ".txt", contents.ToArray());
- }
- }
- }
- static void ProcessConfigAndLua(AssetMap maps)
- {
- string luaABName = Constants.LuaDir.Replace("Assets", "").Replace("Content", "").Replace("/", "").ToLower();
- string luaLogicABName = Constants.LuaLogicDir.Replace("Assets", "").Replace("Content", "").Replace("/", "")
- .ToLower();
- string luaPbABName = Constants.LuaPbDir.Replace("Assets", "").Replace("Content", "").Replace("/", "").ToLower();
- string pubSecABName = Constants.PubSec.Replace("Assets", "").Replace("Content", "").Replace("/", "").ToLower();
- string[] allAbNames = AssetDatabase.GetAllAssetBundleNames();
- if (allAbNames.Contains(luaABName))
- AssetDatabase.RemoveAssetBundleName(luaABName, true);
- if (allAbNames.Contains(luaLogicABName))
- AssetDatabase.RemoveAssetBundleName(luaLogicABName, true);
- if (allAbNames.Contains(luaPbABName))
- AssetDatabase.RemoveAssetBundleName(luaPbABName, true);
- if (allAbNames.Contains(pubSecABName))
- AssetDatabase.RemoveAssetBundleName(pubSecABName, true);
- ProcessConfigAndLua(maps, Constants.ABLuaPbDir, luaPbABName);
- ProcessConfigAndLua(maps, Constants.ABLuaDir, luaABName);
- ProcessConfigAndLua(maps, Constants.ABLuaLogicDir, luaLogicABName);
- ProcessConfigAndLua(maps, Constants.ABPubsec, pubSecABName);
- }
- static void ProcessConfigAndLua(AssetMap maps, string dir, string abName)
- {
- List<string> fileList = FileSystem.getAllFilesPathEX(dir);
- for (int idx = 0; idx < fileList.Count; idx++)
- {
- string fullPath = fileList[idx];
- string relativePath = FileUtils.ExtractAssetRelativePath(fullPath);
- if (relativePath.Contains("/Pb/") && dir != Constants.ABLuaPbDir) continue;
- addData(maps, abName + ".unity3d", relativePath);
- }
- }
- #endregion
- private static HashSet<string> s_ValidExtMap = new HashSet<string>()
- {
- ".prefab", ".txt", ".xml", ".txt", ".lua", ".csv", ".ogg", ".wav", ".ttf", ".bytes", ".pb",
- };
- private static HashSet<string> getAllFilesPathEX(string path, string searchPattern = "*.*", SearchOption searchOption = SearchOption.AllDirectories)
- {
- HashSet<string> all = new HashSet<string>();
- if (!Directory.Exists(path))
- {
- return all;
- }
- string[] allFiles = Directory.GetFiles(path, searchPattern, searchOption);
- for (int j = 0; j < allFiles.Length; ++j)
- {
- string fileName = allFiles[j];
- string ext = Path.GetExtension(fileName);
- if (string.IsNullOrEmpty(ext)) continue;
- ext = ext.ToLower();
- if (s_ValidExtMap.Contains(ext))
- {
- all.Add(fileName.Replace('\\', '/'));
- }
- }
- return all;
- }
- }
|