2025-06-03 02:42:28 -04:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using Ichni;
|
|
|
|
|
|
using UnityEditor;
|
2026-07-23 14:52:15 +08:00
|
|
|
|
using UnityEditor.Build;
|
2025-06-03 02:42:28 -04:00
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// AssetBundle 打包工具
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public class BuildAssetBundle
|
|
|
|
|
|
{
|
|
|
|
|
|
private static string BuildPrepare()
|
|
|
|
|
|
{
|
|
|
|
|
|
// 打包AB输出路径
|
|
|
|
|
|
string strABOutPAthDir = string.Empty;
|
|
|
|
|
|
|
|
|
|
|
|
// 获取“StreamingAssets”文件夹路径(不一定这个文件夹,可自定义)
|
|
|
|
|
|
strABOutPAthDir = Application.streamingAssetsPath + "/ThemeBundles";
|
|
|
|
|
|
|
|
|
|
|
|
// 判断文件夹是否存在,不存在则新建
|
|
|
|
|
|
if (Directory.Exists(strABOutPAthDir) == false)
|
|
|
|
|
|
{
|
|
|
|
|
|
Directory.CreateDirectory(strABOutPAthDir);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return strABOutPAthDir;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static void GenerateAbstracts(AssetBundleManifest manifest)
|
|
|
|
|
|
{
|
|
|
|
|
|
List<string> themeBundles = manifest.GetAllAssetBundles().ToList();
|
|
|
|
|
|
foreach (string bundle in themeBundles)
|
|
|
|
|
|
{
|
|
|
|
|
|
string uriAbs = Application.streamingAssetsPath + "/ThemeBundles/" + bundle + ".abs";
|
|
|
|
|
|
Debug.Log(uriAbs);
|
|
|
|
|
|
if (!ES3.FileExists(uriAbs))
|
|
|
|
|
|
{
|
|
|
|
|
|
ThemeBundleAbstract abs = new ThemeBundleAbstract(bundle);
|
|
|
|
|
|
ES3.Save("ThemeBundleAbstract", abs, uriAbs);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-07-23 14:52:15 +08:00
|
|
|
|
public static void BuildForTarget(BuildTarget target)
|
|
|
|
|
|
{
|
|
|
|
|
|
string platformDirectory = GetPlatformDirectory(target);
|
|
|
|
|
|
if (string.IsNullOrEmpty(platformDirectory))
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.LogWarning($"Skip AssetBundle build for unsupported build target: {target}");
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
string uri = Path.Combine(BuildPrepare(), platformDirectory);
|
|
|
|
|
|
if (Directory.Exists(uri) == false)
|
|
|
|
|
|
{
|
|
|
|
|
|
Directory.CreateDirectory(uri);
|
|
|
|
|
|
}
|
|
|
|
|
|
AssetBundleManifest manifest = BuildPipeline.BuildAssetBundles(uri, BuildAssetBundleOptions.None, target);
|
|
|
|
|
|
if (manifest == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new BuildFailedException($"AssetBundle build failed for target: {target}");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
GenerateAbstracts(manifest);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static string GetPlatformDirectory(BuildTarget target)
|
|
|
|
|
|
{
|
|
|
|
|
|
return target switch
|
|
|
|
|
|
{
|
|
|
|
|
|
BuildTarget.StandaloneWindows => "Windows64",
|
|
|
|
|
|
BuildTarget.StandaloneWindows64 => "Windows64",
|
|
|
|
|
|
BuildTarget.StandaloneOSX => "OSX",
|
|
|
|
|
|
BuildTarget.Android => "Android",
|
|
|
|
|
|
BuildTarget.iOS => "IOS",
|
|
|
|
|
|
_ => string.Empty
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-03 02:42:28 -04:00
|
|
|
|
[MenuItem("AssetBundleTools/BuildToWindows64")]
|
|
|
|
|
|
public static void BuildToWindows64()
|
|
|
|
|
|
{
|
2026-07-23 14:52:15 +08:00
|
|
|
|
BuildForTarget(BuildTarget.StandaloneWindows64);
|
2025-06-03 02:42:28 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[MenuItem("AssetBundleTools/BuildToOSX")]
|
|
|
|
|
|
public static void BuildToOSX()
|
|
|
|
|
|
{
|
2026-07-23 14:52:15 +08:00
|
|
|
|
BuildForTarget(BuildTarget.StandaloneOSX);
|
2025-06-03 02:42:28 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[MenuItem("AssetBundleTools/BuildToAndroid")]
|
|
|
|
|
|
public static void BuildToAndroid()
|
|
|
|
|
|
{
|
2026-07-23 14:52:15 +08:00
|
|
|
|
BuildForTarget(BuildTarget.Android);
|
2025-06-03 02:42:28 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[MenuItem("AssetBundleTools/BuildToIOS")]
|
|
|
|
|
|
public static void BuildToIOS()
|
|
|
|
|
|
{
|
2026-07-23 14:52:15 +08:00
|
|
|
|
BuildForTarget(BuildTarget.iOS);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-07-23 15:19:06 +08:00
|
|
|
|
[InitializeOnLoad]
|
|
|
|
|
|
public static class AssetBundleBuildPlayerHandler
|
2026-07-23 14:52:15 +08:00
|
|
|
|
{
|
2026-07-23 15:19:06 +08:00
|
|
|
|
static AssetBundleBuildPlayerHandler()
|
|
|
|
|
|
{
|
|
|
|
|
|
BuildPlayerWindow.RegisterBuildPlayerHandler(BuildPlayerWithAssetBundles);
|
|
|
|
|
|
}
|
2026-07-23 14:52:15 +08:00
|
|
|
|
|
2026-07-23 15:19:06 +08:00
|
|
|
|
private static void BuildPlayerWithAssetBundles(BuildPlayerOptions options)
|
2026-07-23 14:52:15 +08:00
|
|
|
|
{
|
2026-07-23 15:19:06 +08:00
|
|
|
|
Debug.Log($"Rebuild AssetBundles before player build: {options.target}");
|
|
|
|
|
|
BuildAssetBundle.BuildForTarget(options.target);
|
|
|
|
|
|
BuildPlayerWindow.DefaultBuildMethods.BuildPlayer(options);
|
2025-06-03 02:42:28 -04:00
|
|
|
|
}
|
2026-07-23 14:52:15 +08:00
|
|
|
|
}
|