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,79 +1,123 @@
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)
{
Debug.LogError($"Not found {nameof(AssetBundleBuilderWindow)}.uxml : {uxml}");
return;
}
visualAsset.CloneTree(root);
// 输出路径 try
{
_buildTarget = EditorUserBuildSettings.activeBuildTarget;
_encryptionServicesClassTypes = GetEncryptionServicesClassTypes();
_encryptionServicesClassNames = _encryptionServicesClassTypes.Select(t => t.FullName).ToList();
// 输出目录
string defaultOutputRoot = AssetBundleBuilderHelper.GetDefaultOutputRoot(); string defaultOutputRoot = AssetBundleBuilderHelper.GetDefaultOutputRoot();
string pipelineOutputDirectory = AssetBundleBuilderHelper.MakePipelineOutputDirectory(defaultOutputRoot, _buildTarget); string pipelineOutputDirectory = AssetBundleBuilderHelper.MakePipelineOutputDirectory(defaultOutputRoot, _buildTarget);
EditorGUILayout.LabelField("Build Output", pipelineOutputDirectory); _buildOutputTxt = root.Q<TextField>("BuildOutput");
_buildOutputTxt.SetValueWithoutNotify(pipelineOutputDirectory);
_buildOutputTxt.SetEnabled(false);
// 构建参数 // 构建版本
_buildVersion = EditorGUILayout.IntField("Build Version", _buildVersion, GUILayout.MaxWidth(300)); var appVersion = new Version(Application.version);
_compressOption = (ECompressOption)EditorGUILayout.EnumPopup("Compression", _compressOption, GUILayout.MaxWidth(300)); _buildVersionField = root.Q<IntegerField>("BuildVersion");
if (_encryptionServicesClassNames.Length > 0) _buildVersionField.SetValueWithoutNotify(appVersion.Revision);
_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)); _compressionField = root.Q<EnumField>("Compression");
if (_forceRebuild) _compressionField.Init(ECompressOption.LZ4);
_buildinTags = EditorGUILayout.TextField("Buildin Tags", _buildinTags); _compressionField.SetValueWithoutNotify(ECompressOption.LZ4);
_compressionField.style.width = 300;
// 加密方法
var encryptionContainer = root.Q("EncryptionContainer");
if (_encryptionServicesClassNames.Count > 0)
{
_encryptionField = new PopupField<string>(_encryptionServicesClassNames, 0);
_encryptionField.label = "Encryption";
_encryptionField.style.width = 300;
encryptionContainer.Add(_encryptionField);
}
else
{
_encryptionField = new PopupField<string>();
_encryptionField.label = "Encryption";
_encryptionField.style.width = 300;
encryptionContainer.Add(_encryptionField);
}
// 附加后缀格式
_appendExtensionToggle = root.Q<Toggle>("AppendExtension");
// 强制构建
_forceRebuildToggle = root.Q<Toggle>("ForceRebuild");
_forceRebuildToggle.SetValueWithoutNotify(true);
_forceRebuildToggle.RegisterValueChangedCallback(evt =>
{
_buildTagsTxt.SetEnabled(_forceRebuildToggle.value);
});
// 内置标签
_buildTagsTxt = root.Q<TextField>("BuildinTags");
_buildTagsTxt.SetEnabled(_forceRebuildToggle.value);
// 构建按钮 // 构建按钮
EditorGUILayout.Space(); var buildButton = root.Q<Button>("Build");
if (GUILayout.Button("Build", GUILayout.MaxHeight(40))) buildButton.clicked += BuildButton_clicked; ;
}
catch (Exception e)
{
Debug.LogError(e.ToString());
}
}
private void BuildButton_clicked()
{ {
string title; string title;
string content; string content;
if (_forceRebuild) if (_forceRebuildToggle.value)
{ {
title = "警告"; title = "警告";
content = "确定开始强制构建吗,这样会删除所有已有构建的文件"; content = "确定开始强制构建吗,这样会删除所有已有构建的文件";
@ -85,7 +129,6 @@ namespace YooAsset.Editor
} }
if (EditorUtility.DisplayDialog(title, content, "Yes", "No")) if (EditorUtility.DisplayDialog(title, content, "Yes", "No"))
{ {
SaveSettingsToPlayerPrefs();
EditorTools.ClearUnityConsole(); EditorTools.ClearUnityConsole();
EditorApplication.delayCall += ExecuteBuild; EditorApplication.delayCall += ExecuteBuild;
} }
@ -94,30 +137,6 @@ namespace YooAsset.Editor
Debug.LogWarning("[Build] 打包已经取消"); Debug.LogWarning("[Build] 打包已经取消");
} }
} }
}
private void InitInternal()
{
if (_isInit)
return;
_isInit = true;
// GUI相关
_centerStyle = new GUIStyle(GUI.skin.GetStyle("Label"));
_centerStyle.alignment = TextAnchor.UpperCenter;
_leftStyle = new GUIStyle(GUI.skin.GetStyle("Label"));
_leftStyle.alignment = TextAnchor.MiddleLeft;
// 构建参数
var appVersion = new Version(Application.version);
_buildVersion = appVersion.Revision;
_buildTarget = EditorUserBuildSettings.activeBuildTarget;
_encryptionServicesClassTypes = GetEncryptionServicesClassTypes();
_encryptionServicesClassNames = _encryptionServicesClassTypes.Select(t => t.FullName).ToArray();
// 读取配置
LoadSettingsFromPlayerPrefs();
}
/// <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,7 +6,7 @@ namespace YooAsset.Editor
/// <summary> /// <summary>
/// 检测是否需要加密 /// 检测是否需要加密
/// </summary> /// </summary>
bool Check(string filePath); bool Check(string bundleName);
/// <summary> /// <summary>
/// 加密方法 /// 加密方法