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
{
///
/// md5集合(名称,md5列表)
///
private static Dictionary> md5Dics = new Dictionary>();
///
/// 资源包后缀
///
private static string AssetBundleSuffix = "unity3d";
///
/// 是否忽略中文名字的文件
///
private static bool ignoreZH = true;
///
/// 忽略的图片尺寸
///
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 md5s = new List();
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("成功", "清理处理完成!", "好的");
}
///
///获取⽂件Md5
///
/// Md5 hash.
/// File path.
static string GetMD5Hash(string filePath)
{
MD5 md5 = new MD5CryptoServiceProvider();
return BitConverter.ToString(md5.ComputeHash(File.ReadAllBytes(filePath))).Replace("-", "").ToLower();
}
}