Update AssetBundleBuilder

pull/4/head
hevinci 2022-04-03 22:23:22 +08:00
parent 5e62fd2bff
commit ab3b958bd0
5 changed files with 162 additions and 134 deletions

View File

@ -1,122 +1,141 @@
using System; #if UNITY_2019_4_OR_NEWER
using System.IO; using System;
using System.Linq; using System.Linq;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine;
using UnityEditor; using UnityEditor;
using UnityEngine;
using UnityEditor.UIElements;
using UnityEngine.UIElements;
namespace YooAsset.Editor namespace YooAsset.Editor
{ {
public class AssetBundleBuilderWindow : EditorWindow public class AssetBundleBuilderWindow : EditorWindow
{ {
static AssetBundleBuilderWindow _thisInstance;
[MenuItem("YooAsset/AssetBundle Builder", false, 102)] [MenuItem("YooAsset/AssetBundle Builder", false, 102)]
static void ShowWindow() public static void ShowExample()
{ {
if (_thisInstance == null) AssetBundleBuilderWindow window = GetWindow<AssetBundleBuilderWindow>();
{ window.titleContent = new GUIContent("资源包构建工具");
_thisInstance = EditorWindow.GetWindow(typeof(AssetBundleBuilderWindow), false, "资源包构建工具", true) as AssetBundleBuilderWindow; window.minSize = new Vector2(800, 600);
_thisInstance.minSize = new Vector2(800, 600);
}
_thisInstance.Show();
} }
// 构建器
private readonly AssetBundleBuilder _assetBuilder = new AssetBundleBuilder();
// 构建参数
private int _buildVersion;
private BuildTarget _buildTarget; private BuildTarget _buildTarget;
private ECompressOption _compressOption;
private bool _appendExtension = false;
private bool _forceRebuild = false;
private string _buildinTags = string.Empty;
// 加密类相关
private List<Type> _encryptionServicesClassTypes; private List<Type> _encryptionServicesClassTypes;
private string[] _encryptionServicesClassNames; private List<string> _encryptionServicesClassNames;
private int _encryptionServicesSelectIndex = -1;
// GUI相关 private TextField _buildOutputTxt;
private bool _isInit = false; private IntegerField _buildVersionField;
private GUIStyle _centerStyle; private EnumField _compressionField;
private GUIStyle _leftStyle; private PopupField<string> _encryptionField;
private Toggle _appendExtensionToggle;
private Toggle _forceRebuildToggle;
private TextField _buildTagsTxt;
private void OnGUI() public void CreateGUI()
{ {
InitInternal(); VisualElement root = this.rootVisualElement;
// 标题 // 加载布局文件
EditorGUILayout.LabelField("Build setup", _centerStyle); string rootPath = EditorTools.GetYooAssetPath();
EditorGUILayout.Space(); string uxml = $"{rootPath}/Editor/AssetBundleBuilder/{nameof(AssetBundleBuilderWindow)}.uxml";
var visualAsset = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>(uxml);
// 输出路径 if (visualAsset == null)
string defaultOutputRoot = AssetBundleBuilderHelper.GetDefaultOutputRoot();
string pipelineOutputDirectory = AssetBundleBuilderHelper.MakePipelineOutputDirectory(defaultOutputRoot, _buildTarget);
EditorGUILayout.LabelField("Build Output", pipelineOutputDirectory);
// 构建参数
_buildVersion = EditorGUILayout.IntField("Build Version", _buildVersion, GUILayout.MaxWidth(300));
_compressOption = (ECompressOption)EditorGUILayout.EnumPopup("Compression", _compressOption, GUILayout.MaxWidth(300));
if (_encryptionServicesClassNames.Length > 0)
_encryptionServicesSelectIndex = EditorGUILayout.Popup("Encryption Services", _encryptionServicesSelectIndex, _encryptionServicesClassNames, GUILayout.MaxWidth(300));
_appendExtension = GUILayout.Toggle(_appendExtension, "Append Extension", GUILayout.MaxWidth(120));
_forceRebuild = GUILayout.Toggle(_forceRebuild, "Force Rebuild", GUILayout.MaxWidth(120));
if (_forceRebuild)
_buildinTags = EditorGUILayout.TextField("Buildin Tags", _buildinTags);
// 构建按钮
EditorGUILayout.Space();
if (GUILayout.Button("Build", GUILayout.MaxHeight(40)))
{ {
string title; Debug.LogError($"Not found {nameof(AssetBundleBuilderWindow)}.uxml : {uxml}");
string content; return;
if (_forceRebuild) }
visualAsset.CloneTree(root);
try
{
_buildTarget = EditorUserBuildSettings.activeBuildTarget;
_encryptionServicesClassTypes = GetEncryptionServicesClassTypes();
_encryptionServicesClassNames = _encryptionServicesClassTypes.Select(t => t.FullName).ToList();
// 输出目录
string defaultOutputRoot = AssetBundleBuilderHelper.GetDefaultOutputRoot();
string pipelineOutputDirectory = AssetBundleBuilderHelper.MakePipelineOutputDirectory(defaultOutputRoot, _buildTarget);
_buildOutputTxt = root.Q<TextField>("BuildOutput");
_buildOutputTxt.SetValueWithoutNotify(pipelineOutputDirectory);
_buildOutputTxt.SetEnabled(false);
// 构建版本
var appVersion = new Version(Application.version);
_buildVersionField = root.Q<IntegerField>("BuildVersion");
_buildVersionField.SetValueWithoutNotify(appVersion.Revision);
// 压缩方式
_compressionField = root.Q<EnumField>("Compression");
_compressionField.Init(ECompressOption.LZ4);
_compressionField.SetValueWithoutNotify(ECompressOption.LZ4);
_compressionField.style.width = 300;
// 加密方法
var encryptionContainer = root.Q("EncryptionContainer");
if (_encryptionServicesClassNames.Count > 0)
{ {
title = "警告"; _encryptionField = new PopupField<string>(_encryptionServicesClassNames, 0);
content = "确定开始强制构建吗,这样会删除所有已有构建的文件"; _encryptionField.label = "Encryption";
_encryptionField.style.width = 300;
encryptionContainer.Add(_encryptionField);
} }
else else
{ {
title = "提示"; _encryptionField = new PopupField<string>();
content = "确定开始增量构建吗"; _encryptionField.label = "Encryption";
_encryptionField.style.width = 300;
encryptionContainer.Add(_encryptionField);
} }
if (EditorUtility.DisplayDialog(title, content, "Yes", "No"))
// 附加后缀格式
_appendExtensionToggle = root.Q<Toggle>("AppendExtension");
// 强制构建
_forceRebuildToggle = root.Q<Toggle>("ForceRebuild");
_forceRebuildToggle.SetValueWithoutNotify(true);
_forceRebuildToggle.RegisterValueChangedCallback(evt =>
{ {
SaveSettingsToPlayerPrefs(); _buildTagsTxt.SetEnabled(_forceRebuildToggle.value);
EditorTools.ClearUnityConsole(); });
EditorApplication.delayCall += ExecuteBuild;
} // 内置标签
else _buildTagsTxt = root.Q<TextField>("BuildinTags");
{ _buildTagsTxt.SetEnabled(_forceRebuildToggle.value);
Debug.LogWarning("[Build] 打包已经取消");
} // 构建按钮
var buildButton = root.Q<Button>("Build");
buildButton.clicked += BuildButton_clicked; ;
}
catch (Exception e)
{
Debug.LogError(e.ToString());
} }
} }
private void InitInternal()
private void BuildButton_clicked()
{ {
if (_isInit) string title;
return; string content;
_isInit = true; if (_forceRebuildToggle.value)
{
// GUI相关 title = "警告";
_centerStyle = new GUIStyle(GUI.skin.GetStyle("Label")); content = "确定开始强制构建吗,这样会删除所有已有构建的文件";
_centerStyle.alignment = TextAnchor.UpperCenter; }
_leftStyle = new GUIStyle(GUI.skin.GetStyle("Label")); else
_leftStyle.alignment = TextAnchor.MiddleLeft; {
title = "提示";
// 构建参数 content = "确定开始增量构建吗";
var appVersion = new Version(Application.version); }
_buildVersion = appVersion.Revision; if (EditorUtility.DisplayDialog(title, content, "Yes", "No"))
_buildTarget = EditorUserBuildSettings.activeBuildTarget; {
EditorTools.ClearUnityConsole();
_encryptionServicesClassTypes = GetEncryptionServicesClassTypes(); EditorApplication.delayCall += ExecuteBuild;
_encryptionServicesClassNames = _encryptionServicesClassTypes.Select(t => t.FullName).ToArray(); }
else
// 读取配置 {
LoadSettingsFromPlayerPrefs(); Debug.LogWarning("[Build] 打包已经取消");
}
} }
/// <summary> /// <summary>
@ -129,55 +148,36 @@ namespace YooAsset.Editor
buildParameters.VerifyBuildingResult = true; buildParameters.VerifyBuildingResult = true;
buildParameters.OutputRoot = defaultOutputRoot; buildParameters.OutputRoot = defaultOutputRoot;
buildParameters.BuildTarget = _buildTarget; buildParameters.BuildTarget = _buildTarget;
buildParameters.BuildVersion = _buildVersion; buildParameters.BuildVersion = _buildVersionField.value;
buildParameters.CompressOption = _compressOption; buildParameters.CompressOption = (ECompressOption)_compressionField.value;
buildParameters.AppendFileExtension = _appendExtension; buildParameters.AppendFileExtension = _appendExtensionToggle.value;
buildParameters.EncryptionServices = CreateEncryptionServicesInstance(); buildParameters.EncryptionServices = CreateEncryptionServicesInstance();
buildParameters.ForceRebuild = _forceRebuild; buildParameters.ForceRebuild = _forceRebuildToggle.value;
buildParameters.BuildinTags = _buildinTags; buildParameters.BuildinTags = _buildTagsTxt.value;
_assetBuilder.Run(buildParameters);
AssetBundleBuilder builder = new AssetBundleBuilder();
builder.Run(buildParameters);
} }
/// <summary>
/// 获取加密类的类型列表
/// </summary>
private List<Type> GetEncryptionServicesClassTypes() private List<Type> GetEncryptionServicesClassTypes()
{ {
List<Type> classTypes = AssemblyUtility.GetAssignableTypes(AssemblyUtility.UnityDefaultAssemblyEditorName, typeof(IEncryptionServices)); List<Type> classTypes = AssemblyUtility.GetAssignableTypes(AssemblyUtility.UnityDefaultAssemblyEditorName, typeof(IEncryptionServices));
return classTypes; return classTypes;
} }
/// <summary>
/// 创建加密类的实例
/// </summary>
private IEncryptionServices CreateEncryptionServicesInstance() private IEncryptionServices CreateEncryptionServicesInstance()
{ {
if (_encryptionServicesSelectIndex < 0) if (_encryptionField.index < 0)
return null; return null;
var classType = _encryptionServicesClassTypes[_encryptionServicesSelectIndex]; var classType = _encryptionServicesClassTypes[_encryptionField.index];
return (IEncryptionServices)Activator.CreateInstance(classType); return (IEncryptionServices)Activator.CreateInstance(classType);
} }
#region 配置相关
private const string StrEditorCompressOption = "StrEditorCompressOption";
private const string StrEditorAppendExtension = "StrEditorAppendExtension";
private const string StrEditorForceRebuild = "StrEditorForceRebuild";
private const string StrEditorBuildinTags = "StrEditorBuildinTags";
/// <summary>
/// 存储配置
/// </summary>
private void SaveSettingsToPlayerPrefs()
{
EditorTools.PlayerSetEnum<ECompressOption>(StrEditorCompressOption, _compressOption);
EditorPrefs.SetBool(StrEditorAppendExtension, _appendExtension);
EditorPrefs.SetBool(StrEditorForceRebuild, _forceRebuild);
EditorPrefs.SetString(StrEditorBuildinTags, _buildinTags);
}
/// <summary>
/// 读取配置
/// </summary>
private void LoadSettingsFromPlayerPrefs()
{
_compressOption = EditorTools.PlayerGetEnum<ECompressOption>(StrEditorCompressOption, ECompressOption.Uncompressed);
_appendExtension = EditorPrefs.GetBool(StrEditorAppendExtension, false);
_forceRebuild = EditorPrefs.GetBool(StrEditorForceRebuild, false);
_buildinTags = EditorPrefs.GetString(StrEditorBuildinTags, string.Empty);
}
#endregion
} }
} }
#endif

View File

@ -0,0 +1,13 @@
<ui:UXML xmlns:ui="UnityEngine.UIElements" xmlns:uie="UnityEditor.UIElements" xsi="http://www.w3.org/2001/XMLSchema-instance" engine="UnityEngine.UIElements" editor="UnityEditor.UIElements" noNamespaceSchemaLocation="../../UIElementsSchema/UIElements.xsd" editor-extension-mode="True">
<uie:Toolbar name="Toolbar" style="display: flex;" />
<ui:VisualElement name="BuildContainer">
<ui:TextField picking-mode="Ignore" label="Build Output" name="BuildOutput" />
<uie:IntegerField label="Build Version" value="0" name="BuildVersion" />
<uie:EnumField label="Compression" value="Center" name="Compression" />
<ui:VisualElement name="EncryptionContainer" style="height: 24px;" />
<ui:Toggle label="Append Extension" name="AppendExtension" style="height: 15px;" />
<ui:Toggle label="Force Rebuild" name="ForceRebuild" />
<ui:TextField picking-mode="Ignore" label="Buildin Tags" name="BuildinTags" />
<ui:Button text="构建" display-tooltip-when-elided="true" name="Build" style="height: 50px; background-color: rgb(40, 106, 42); margin-top: 10px;" />
</ui:VisualElement>
</ui:UXML>

View File

@ -0,0 +1,10 @@
fileFormatVersion: 2
guid: 28ba29adb4949284e8c48893218b0d9a
ScriptedImporter:
internalIDToNameTable: []
externalObjects: {}
serializedVersion: 2
userData:
assetBundleName:
assetBundleVariant:
script: {fileID: 13804, guid: 0000000000000000e000000000000000, type: 0}

View File

@ -49,13 +49,18 @@ namespace YooAsset.Editor
int progressValue = 0; int progressValue = 0;
foreach (var bundleInfo in buildMapContext.BundleInfos) foreach (var bundleInfo in buildMapContext.BundleInfos)
{ {
var bundleName = bundleInfo.BundleName; if (encryptionServices.Check(bundleInfo.BundleName))
string filePath = $"{buildParameters.PipelineOutputDirectory}/{bundleName}";
if (encryptionServices.Check(filePath))
{ {
encryptList.Add(bundleName); if (bundleInfo.IsRawFile)
{
UnityEngine.Debug.LogWarning($"Encryption not support raw file : {bundleInfo.BundleName}");
continue;
}
encryptList.Add(bundleInfo.BundleName);
// 注意:通过判断文件合法性,规避重复加密一个文件 // 注意:通过判断文件合法性,规避重复加密一个文件
string filePath = $"{buildParameters.PipelineOutputDirectory}/{bundleInfo.BundleName}";
byte[] fileData = File.ReadAllBytes(filePath); byte[] fileData = File.ReadAllBytes(filePath);
if (EditorTools.CheckBundleFileValid(fileData)) if (EditorTools.CheckBundleFileValid(fileData))
{ {

View File

@ -6,8 +6,8 @@ namespace YooAsset.Editor
/// <summary> /// <summary>
/// 检测是否需要加密 /// 检测是否需要加密
/// </summary> /// </summary>
bool Check(string filePath); bool Check(string bundleName);
/// <summary> /// <summary>
/// 加密方法 /// 加密方法
/// </summary> /// </summary>