124 lines
3.6 KiB
C#
124 lines
3.6 KiB
C#
using System;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using System.IO;
|
||
using System.Linq;
|
||
using Ichni;
|
||
using UnityEditor;
|
||
using UnityEditor.Build;
|
||
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);
|
||
}
|
||
}
|
||
}
|
||
|
||
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
|
||
};
|
||
}
|
||
|
||
[MenuItem("AssetBundleTools/BuildToWindows64")]
|
||
public static void BuildToWindows64()
|
||
{
|
||
BuildForTarget(BuildTarget.StandaloneWindows64);
|
||
}
|
||
|
||
[MenuItem("AssetBundleTools/BuildToOSX")]
|
||
public static void BuildToOSX()
|
||
{
|
||
BuildForTarget(BuildTarget.StandaloneOSX);
|
||
}
|
||
|
||
[MenuItem("AssetBundleTools/BuildToAndroid")]
|
||
public static void BuildToAndroid()
|
||
{
|
||
BuildForTarget(BuildTarget.Android);
|
||
}
|
||
|
||
[MenuItem("AssetBundleTools/BuildToIOS")]
|
||
public static void BuildToIOS()
|
||
{
|
||
BuildForTarget(BuildTarget.iOS);
|
||
}
|
||
}
|
||
|
||
[InitializeOnLoad]
|
||
public static class AssetBundleBuildPlayerHandler
|
||
{
|
||
static AssetBundleBuildPlayerHandler()
|
||
{
|
||
BuildPlayerWindow.RegisterBuildPlayerHandler(BuildPlayerWithAssetBundles);
|
||
}
|
||
|
||
private static void BuildPlayerWithAssetBundles(BuildPlayerOptions options)
|
||
{
|
||
Debug.Log($"Rebuild AssetBundles before player build: {options.target}");
|
||
BuildAssetBundle.BuildForTarget(options.target);
|
||
BuildPlayerWindow.DefaultBuildMethods.BuildPlayer(options);
|
||
}
|
||
}
|