if (GUILayout.Button("Build AssetBundles"))
{
StartBuildAssetBundles();
}
public static void StartBuildAssetBundles()
{
string sourcePath = Application.dataPath + "/" + assetBundleSourcePath;
var buildAssetBundlePath = Path.GetFullPath(Path.Combine(Application.dataPath, "../ab1/"));
//ClearAssetBundlesName();
packedFile.Clear();
string outputPath = Path.Combine(buildAssetBundlePath, Platform.GetPlatformFolder(EditorUserBuildSettings.activeBuildTarget));
if (Directory.Exists(outputPath))
{
Directory.Delete(outputPath, true);
}
Directory.CreateDirectory(outputPath);
Pack(sourcePath);
if (!Directory.Exists(outputPath))
{
Directory.CreateDirectory(outputPath);
}
//根据BuildSetting里面所激活的平台进行打包 设置过AssetBundleName的都会进行打包
BuildPipeline.BuildAssetBundles(outputPath, BuildAssetBundleOptions.ForceRebuildAssetBundle, EditorUserBuildSettings.activeBuildTarget);
var versions = EnumFileMd5(outputPath, outputPath);
File.WriteAllText(outputPath + "/versions.txt", versions);
ClearAssetBundlesName();
AssetDatabase.Refresh();
System.Diagnostics.Process.Start(buildAssetBundlePath);
}
static void Pack(string source)
{
//Debug.Log("Pack source " + source);
DirectoryInfo folder = new DirectoryInfo(source);
FileSystemInfo[] files = folder.GetFileSystemInfos();
int length = files.Length;
for (int i = 0; i < length; i++)
{
if (files[i] is DirectoryInfo)
{
Pack(files[i].FullName);
}
else
{
if (!files[i].Name.EndsWith(".meta")
&& !files[i].Name.EndsWith(".xlsx"))
{
fileWithDepends(files[i].FullName);
}
}
}
foreach (var d in dDependences)
{
PackItem(d.Key, AssetImporter.GetAtPath(d.Key));
}
}
private static void PackItem(string dp, AssetImporter assetImporter)
{
string pathTmp = dp.Substring("Assets".Length + 1);
string assetName = pathTmp.Substring(pathTmp.IndexOf("/") + 1).Replace(" ", "_");
assetName = assetName.Replace(Path.GetExtension(assetName), UAssetBundleDownloader.AssetBundleSuffix);
assetImporter.SetAssetBundleNameAndVariant(assetName, string.Empty);
//fileWithDepends(assetImporter.assetPath);
}
//设置要打包的文件
static void fileWithDepends(string source)
{
if (packedFile.Contains(source))
{
return;
}
packedFile.Add(source);
string _source = Replace(source);
string _assetPath = _source;
if (_source.StartsWith(Application.dataPath))
{
_assetPath = "Assets" + _source.Substring(Application.dataPath.Length);
}
//if (dDependences.ContainsKey(_assetPath))
//{
// dDependences[_assetPath]++;
//}
//else
//{
// dDependences.Add(_assetPath, 1);
//}
//PackItem(_assetPath, AssetImporter.GetAtPath(_assetPath));
//自动获取依赖项并给其资源设置AssetBundleName
string[] dps = AssetDatabase.GetDependencies(_assetPath);
foreach (var dp in dps)
{
if (dp.EndsWith(".cs"))
continue;
AssetImporter assetImporter = AssetImporter.GetAtPath(dp);
if (assetImporter is TextureImporter)
{
TextureImporter tai = assetImporter as TextureImporter;
if (!string.IsNullOrEmpty(tai.spritePackingTag))
{
tai.SetAssetBundleNameAndVariant(tai.spritePackingTag + UAssetBundleDownloader.AssetBundleSuffix, null);
}
else
{
if (dDependences.ContainsKey(dp))
{
dDependences[dp]++;
}
else
{
dDependences.Add(dp, 1);
}
//PackItem(dp, assetImporter);
}
}
else
{
if (dDependences.ContainsKey(dp))
{
dDependences[dp]++;
}
else
{
dDependences.Add(dp, 1);
}
//PackItem(dp, assetImporter);
}
}
}
static string Replace(string s)
{
return s.Replace("\\", "/");
}
static void ClearAssetBundlesName()
{
int length = AssetDatabase.GetAllAssetBundleNames().Length;
string[] oldAssetBundleNames = new string[length];
for (int i = 0; i < length; i++)
{
oldAssetBundleNames[i] = AssetDatabase.GetAllAssetBundleNames()[i];
}
for (int j = 0; j < oldAssetBundleNames.Length; j++)
{
AssetDatabase.RemoveAssetBundleName(oldAssetBundleNames[j], true);
}
}
private static string EnumFileMd5(string sdir, string outputDir)
{
string sresult = "";
foreach (var s in Directory.GetFiles(sdir))
{
FileInfo fi = new FileInfo(s);
if (fi.Name.EndsWith(".aspx") || fi.Name.EndsWith(".config") || fi.Name.Contains("/mono/") || fi.Name.Contains("\\mono\\") || fi.Name.EndsWith("version.txt"))
{
continue;
}
sresult += fi.FullName.Replace("\\", "/").Replace(outputDir.Replace("\\", "/"), "");
sresult += "|" + MD5String.GetMD5HashFromFile(s) + "|" + fi.Length;
sresult += "\r\n";
}
foreach (var s in Directory.GetDirectories(sdir))
{
sresult += EnumFileMd5(s, outputDir);
}
return sresult;
}