123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEditor;
- using System.IO;
- using System.Security.Cryptography;
- using System;
- using System.Text.RegularExpressions;
- public class AssetBundleSetName : AssetBundlesExport
- {
- /// <summary>
- /// md5集合(名称,md5列表)
- /// </summary>
- private static Dictionary<string, List<string>> md5Dics = new Dictionary<string, List<string>>();
- /// <summary>
- /// 资源包后缀
- /// </summary>
- private static string AssetBundleSuffix = "unity3d";
- /// <summary>
- /// 是否忽略中文名字的文件
- /// </summary>
- private static bool ignoreZH = true;
- /// <summary>
- /// 忽略的图片尺寸
- /// </summary>
- private static int ignoreSiz = 512;
- [MenuItem("Build/Android平台 设置Assetbundle名字并打包(使用自身名字,重复资源自动设置单独Assetbundle名)", false, 100)]
- public static void SetAssetBundleLabls_Android()
- {
- SetAssetBundleLabls(BuildTarget.Android);
- }
- [MenuItem("Build/Windows平台 设置Assetbundle名字并打包(使用自身名字,重复资源自动设置单独Assetbundle名)", false, 200)]
- public static void SetAssetBundleLabls_Windows()
- {
- SetAssetBundleLabls(BuildTarget.StandaloneWindows);
- }
- //[MenuItem("Build/设置Assetbundle名字并打包 iOS平台 (使用自身名字,重复资源自动设置单独Assetbundle名)")]
- //public static void SetAssetBundleLabls_iOS()
- //{
- // SetAssetBundleLabls(BuildTarget.iOS);
- //}
- public static void SetAssetBundleLabls(BuildTarget buildTarget)
- {
- // 移除没有用的assetbundlename
- AssetDatabase.RemoveUnusedAssetBundleNames();
- md5Dics.Clear();
- int index = 0;
- //或取出Assets/Bundle所有prefab的guid
- string[] prefabPaths = AssetDatabase.FindAssets("t:Prefab t:TextAsset t:ExternalBehaviorTree t:SceneAsset", new string[] { "Assets/Bundle" });
- foreach (var prefabGuid in prefabPaths)
- {
- string prefabAssetPath = AssetDatabase.GUIDToAssetPath(prefabGuid);
- string prefabName = Path.GetFileNameWithoutExtension(prefabAssetPath);
- index++;
- //编辑器进度条
- EditorUtility.DisplayProgressBar("处理中>>>", prefabAssetPath, index / (float)prefabPaths.Length);
- // 是否忽略带中文的文件
- if (ignoreZH)
- {
- bool hasZH = false;
- // 正则表达式检测是否有中文
- for (int i = 0; i < prefabName.Length; i++)
- {
- if (Regex.IsMatch(prefabName[i].ToString(), @"[\u4E00-\u9FA5]+$"))
- {
- hasZH = true;
- break;
- }
- }
- if (hasZH)
- {
- continue;
- }
- }
- // 先设置Prefab打包名称
- AssetImporter prefabImporter = AssetImporter.GetAtPath(prefabAssetPath);
- prefabImporter.SetAssetBundleNameAndVariant(prefabName, AssetBundleSuffix);
- // 在设置相同引用的资源打包名称
- string[] depend = AssetDatabase.GetDependencies(prefabAssetPath, false);
- for (int i = 0; i < depend.Length; i++)
- {
- string assetPath = depend[i];
- AssetImporter importer = AssetImporter.GetAtPath(assetPath);
- bool ignoreFile = true;
- bool aloneFile = false;
- // 筛选图片加载器
- if (importer is TextureImporter)
- {
- aloneFile = true;
- TextureImporter textureImporter = importer as TextureImporter;
- int w, h;
- textureImporter.GetSourceTextureWidthAndHeight(out w, out h);
- // 图片宽或者高大于等于2048像素才不忽略
- if (w >= 2048 || h >= 2048)
- {
- ignoreFile = false;
- }
- }
- // 筛选模型加载器
- else if (importer is ModelImporter)
- {
- aloneFile = true;
- ignoreFile = false;
- }
- else
- {
- aloneFile = false;
- }
- // 独立检测文件
- if (aloneFile)
- {
- string m5 = GetMD5Hash(Path.Combine(Directory.GetCurrentDirectory(), assetPath));
- if (md5Dics.ContainsKey(assetPath))
- {
- if (md5Dics[assetPath].Contains(m5))
- {
- for (int j = 0; j < md5Dics[assetPath].Count; j++)
- {
- if (md5Dics[assetPath][j] == m5)
- {
- if (ignoreFile == false)
- {
- string fileName = Path.GetFileNameWithoutExtension(assetPath) + j.ToString();
- importer.SetAssetBundleNameAndVariant(fileName, AssetBundleSuffix);
- Debug.Log("含有相同文件" + assetPath);
- }
- else
- {
- //Debug.Log("含有相同文件,但文件太小,直接忽略" + assetPath);
- }
- }
- }
- }
- else
- {
- md5Dics[assetPath].Add(m5);
- }
- }
- else
- {
- List<string> md5s = new List<string>();
- md5s.Add(m5);
- md5Dics.Add(assetPath, md5s);
- }
- }
- }
- }
- // 移除没有用的assetbundlename
- AssetDatabase.RemoveUnusedAssetBundleNames();
- // 编辑器刷新
- AssetDatabase.Refresh();
- // 清理进度条
- EditorUtility.ClearProgressBar();
- // 如果存放目录不存在,就创建一个目录
- string targetPath = Application.dataPath + "/StreamingAssets/NewRes";
- if (!Directory.Exists(targetPath))
- {
- Directory.CreateDirectory(targetPath);
- }
-
- BuildPipeline.BuildAssetBundles(targetPath, BuildAssetBundleOptions.ChunkBasedCompression, buildTarget);
- // 编辑器刷新
- AssetDatabase.Refresh();
- // 完成弹窗
- EditorUtility.DisplayDialog("成功", "打包处理完成!", "好的");
- }
- [MenuItem("Build/清空Assetbundle名字")]
- public static void ClearAssetBundleLabls()
- {
- // 移除没有用的assetbundlename
- AssetDatabase.RemoveUnusedAssetBundleNames();
- md5Dics.Clear();
- int index = 0;
- //或取出Assets/Bundle所有prefab的guid
- string[] prefabPaths = AssetDatabase.FindAssets("t:Prefab t:TextAsset t:ExternalBehaviorTree", new string[] { "Assets/Bundle" });
- foreach (var prefabGuid in prefabPaths)
- {
- string prefabAssetPath = AssetDatabase.GUIDToAssetPath(prefabGuid);
- string prefabName = Path.GetFileNameWithoutExtension(prefabAssetPath);
- index++;
- //编辑器进度条
- EditorUtility.DisplayProgressBar("处理中>>>", prefabAssetPath, index / (float)prefabPaths.Length);
- // 是否忽略带中文的文件
- if (ignoreZH)
- {
- bool hasZH = false;
- // 正则表达式检测是否有中文
- for (int i = 0; i < prefabName.Length; i++)
- {
- if (Regex.IsMatch(prefabName[i].ToString(), @"[\u4E00-\u9FA5]+$"))
- {
- hasZH = true;
- break;
- }
- }
- if (hasZH)
- {
- continue;
- }
- }
- // 先设置Prefab打包名称
- AssetImporter prefabImporter = AssetImporter.GetAtPath(prefabAssetPath);
- prefabImporter.SetAssetBundleNameAndVariant("", "");
- // 在设置相同引用的资源打包名称
- string[] depend = AssetDatabase.GetDependencies(prefabAssetPath, false);
- for (int i = 0; i < depend.Length; i++)
- {
- string assetPath = depend[i];
- AssetImporter importer = AssetImporter.GetAtPath(assetPath);
- importer.SetAssetBundleNameAndVariant("", "");
- }
- }
- // 移除没有用的assetbundlename
- AssetDatabase.RemoveUnusedAssetBundleNames();
- // 编辑器刷新
- AssetDatabase.Refresh();
- // 清理进度条
- EditorUtility.ClearProgressBar();
- // 完成弹窗
- EditorUtility.DisplayDialog("成功", "清理处理完成!", "好的");
- }
- /// <summary>
- ///获取⽂件Md5
- /// </summary>
- /// <returns>Md5 hash.</returns>
- /// <param name="filePath">File path.</param>
- static string GetMD5Hash(string filePath)
- {
- MD5 md5 = new MD5CryptoServiceProvider();
- return BitConverter.ToString(md5.ComputeHash(File.ReadAllBytes(filePath))).Replace("-", "").ToLower();
- }
- }
|