| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292 |
- using System;
- using UnityEngine;
- using UnityEditor;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Security.Cryptography;
- /// <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;
- }
- /// <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);
- List<AssetBundleBuild> bm = new List<AssetBundleBuild>(maps.Maps.Count);
- foreach (var items in maps.Maps)
- {
- AssetBundleBuild bundle = new AssetBundleBuild();
- bundle.assetBundleName = AssetsObscureUtil.GetABFileName(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();
- }
- 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;
- }
- }
|