Update AssetBundleGrouper

pull/4/head
hevinci 2022-04-02 18:34:46 +08:00
parent 2b95a1eb9d
commit 528eccbd9e
12 changed files with 264 additions and 94 deletions

View File

@ -35,6 +35,21 @@ namespace YooAsset.Editor
public string AssetTags = string.Empty; public string AssetTags = string.Empty;
/// <summary>
/// 检测配置错误
/// </summary>
public void CheckConfigError()
{
if(AssetDatabase.LoadAssetAtPath<UnityEngine.Object>(CollectPath) == null)
throw new Exception($"Invalid collect path : {CollectPath}");
if (AssetBundleGrouperSettingData.HasPackRuleName(PackRuleName) == false)
throw new Exception($"Invalid {nameof(IPackRule)} class type : {PackRuleName}");
if (AssetBundleGrouperSettingData.HasFilterRuleName(FilterRuleName) == false)
throw new Exception($"Invalid {nameof(IFilterRule)} class type : {FilterRuleName}");
}
/// <summary> /// <summary>
/// 获取打包收集的资源文件 /// 获取打包收集的资源文件
/// </summary> /// </summary>

View File

@ -31,6 +31,17 @@ namespace YooAsset.Editor
public List<AssetBundleCollector> Collectors = new List<AssetBundleCollector>(); public List<AssetBundleCollector> Collectors = new List<AssetBundleCollector>();
/// <summary>
/// 检测配置错误
/// </summary>
public void CheckConfigError()
{
foreach(var collector in Collectors)
{
collector.CheckConfigError();
}
}
/// <summary> /// <summary>
/// 获取打包收集的资源文件 /// 获取打包收集的资源文件
/// </summary> /// </summary>

View File

@ -0,0 +1,162 @@
using System;
using System.IO;
using System.Xml;
using System.Text;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
namespace YooAsset.Editor
{
public class AssetBundleGrouperConfig
{
public const string XmlShader = "Shader";
public const string XmlAutoCollectShader = "AutoCollectShader";
public const string XmlShaderBundleName = "ShaderBundleName";
public const string XmlGrouper = "Grouper";
public const string XmlGrouperName = "GrouperName";
public const string XmlGrouperDesc = "GrouperDesc";
public const string XmlCollector = "Collector";
public const string XmlDirectory = "CollectPath";
public const string XmlPackRule = "PackRule";
public const string XmlFilterRule = "FilterRule";
public const string XmlNotWriteToAssetList = "NotWriteToAssetList";
public const string XmlAssetTags = "AssetTags";
/// <summary>
/// 导入XML配置表
/// </summary>
public static void ImportXmlConfig(string filePath)
{
if (File.Exists(filePath) == false)
throw new FileNotFoundException(filePath);
if (Path.GetExtension(filePath) != ".xml")
throw new Exception($"Only support xml : {filePath}");
// 加载配置文件
XmlDocument xml = new XmlDocument();
xml.Load(filePath);
XmlElement root = xml.DocumentElement;
// 读取着色器配置
bool autoCollectShaders = false;
string shaderBundleName = string.Empty;
var shaderNodeList = root.GetElementsByTagName(XmlShader);
if (shaderNodeList.Count > 0)
{
XmlElement shaderElement = shaderNodeList[0] as XmlElement;
if (shaderElement.HasAttribute(XmlAutoCollectShader) == false)
throw new Exception($"Not found attribute {XmlAutoCollectShader} in {XmlShader}");
if (shaderElement.HasAttribute(XmlShaderBundleName) == false)
throw new Exception($"Not found attribute {XmlShaderBundleName} in {XmlShader}");
autoCollectShaders = shaderElement.GetAttribute(XmlAutoCollectShader) == "True" ? true : false;
shaderBundleName = shaderElement.GetAttribute(XmlShaderBundleName);
}
// 读取分组配置
List<AssetBundleGrouper> grouperTemper = new List<AssetBundleGrouper>();
var grouperNodeList = root.GetElementsByTagName(XmlGrouper);
foreach (var grouperNode in grouperNodeList)
{
XmlElement grouperElement = grouperNode as XmlElement;
if (grouperElement.HasAttribute(XmlGrouperName) == false)
throw new Exception($"Not found attribute {XmlGrouperName} in {XmlGrouper}");
if (grouperElement.HasAttribute(XmlGrouperDesc) == false)
throw new Exception($"Not found attribute {XmlGrouperDesc} in {XmlGrouper}");
if (grouperElement.HasAttribute(XmlAssetTags) == false)
throw new Exception($"Not found attribute {XmlAssetTags} in {XmlGrouper}");
AssetBundleGrouper grouper = new AssetBundleGrouper();
grouper.GrouperName = grouperElement.GetAttribute(XmlGrouperName);
grouper.GrouperDesc = grouperElement.GetAttribute(XmlGrouperDesc);
grouper.AssetTags = grouperElement.GetAttribute(XmlAssetTags);
grouperTemper.Add(grouper);
// 读取收集器配置
var collectorNodeList = grouperElement.GetElementsByTagName(XmlCollector);
foreach (var collectorNode in collectorNodeList)
{
XmlElement collectorElement = collectorNode as XmlElement;
if (collectorElement.HasAttribute(XmlDirectory) == false)
throw new Exception($"Not found attribute {XmlDirectory} in {XmlCollector}");
if (collectorElement.HasAttribute(XmlPackRule) == false)
throw new Exception($"Not found attribute {XmlPackRule} in {XmlCollector}");
if (collectorElement.HasAttribute(XmlFilterRule) == false)
throw new Exception($"Not found attribute {XmlFilterRule} in {XmlCollector}");
if (collectorElement.HasAttribute(XmlNotWriteToAssetList) == false)
throw new Exception($"Not found attribute {XmlNotWriteToAssetList} in {XmlCollector}");
if (collectorElement.HasAttribute(XmlAssetTags) == false)
throw new Exception($"Not found attribute {XmlAssetTags} in {XmlCollector}");
AssetBundleCollector collector = new AssetBundleCollector();
collector.CollectPath = collectorElement.GetAttribute(XmlDirectory);
collector.PackRuleName = collectorElement.GetAttribute(XmlPackRule);
collector.FilterRuleName = collectorElement.GetAttribute(XmlFilterRule);
collector.NotWriteToAssetList = collectorElement.GetAttribute(XmlNotWriteToAssetList) == "True" ? true : false;
collector.AssetTags = collectorElement.GetAttribute(XmlAssetTags); ;
grouper.Collectors.Add(collector);
}
}
// 保存配置数据
AssetBundleGrouperSettingData.ClearAll();
AssetBundleGrouperSettingData.Setting.AutoCollectShaders = autoCollectShaders;
AssetBundleGrouperSettingData.Setting.ShadersBundleName = shaderBundleName;
AssetBundleGrouperSettingData.Setting.Groupers.AddRange(grouperTemper);
AssetBundleGrouperSettingData.SaveFile();
Debug.Log($"导入配置完毕!");
}
/// <summary>
/// 导出XML配置表
/// </summary>
public static void ExportXmlConfig(string savePath)
{
if (File.Exists(savePath))
File.Delete(savePath);
StringBuilder sb = new StringBuilder();
sb.AppendLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
sb.AppendLine("<root>");
sb.AppendLine("</root>");
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(sb.ToString());
XmlElement root = xmlDoc.DocumentElement;
// 设置着色器配置
var shaderElement = xmlDoc.CreateElement(XmlShader);
shaderElement.SetAttribute(XmlAutoCollectShader, AssetBundleGrouperSettingData.Setting.AutoCollectShaders.ToString());
shaderElement.SetAttribute(XmlShaderBundleName, AssetBundleGrouperSettingData.Setting.ShadersBundleName);
// 设置分组配置
foreach (var grouper in AssetBundleGrouperSettingData.Setting.Groupers)
{
var grouperElement = xmlDoc.CreateElement(XmlGrouper);
grouperElement.SetAttribute(XmlGrouperName, grouper.GrouperName);
grouperElement.SetAttribute(XmlGrouperDesc, grouper.GrouperDesc);
grouperElement.SetAttribute(XmlAssetTags, grouper.AssetTags);
root.AppendChild(grouperElement);
// 设置收集器配置
foreach (var collector in grouper.Collectors)
{
var collectorElement = xmlDoc.CreateElement(XmlCollector);
collectorElement.SetAttribute(XmlDirectory, collector.CollectPath);
collectorElement.SetAttribute(XmlPackRule, collector.PackRuleName);
collectorElement.SetAttribute(XmlFilterRule, collector.FilterRuleName);
collectorElement.SetAttribute(XmlNotWriteToAssetList, collector.NotWriteToAssetList.ToString());
collectorElement.SetAttribute(XmlAssetTags, collector.AssetTags);
grouperElement.AppendChild(collectorElement);
}
}
// 生成配置文件
xmlDoc.Save(savePath);
Debug.Log($"导出配置完毕!");
}
}
}

View File

@ -1,17 +0,0 @@
using System;
namespace YooAsset.Editor
{
public class AssetBundleGrouperConfigDefine
{
public const string XmlGrouper = "Grouper";
public const string XmlCollector = "Collector";
public const string XmlDirectory = "CollectPath";
public const string XmlPackRule = "PackRule";
public const string XmlFilterRule = "FilterRule";
public const string XmlNotWriteToAssetList = "NotWriteToAssetList";
public const string XmlAssetTags = "AssetTags";
}
}

View File

@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: f2190cc6d8995aa458ded22ffd7fc1e0
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,14 +0,0 @@
using System;
using System.IO;
using System.Xml;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
namespace YooAsset.Editor
{
public class AssetBundleGrouperConfigExporter
{
}
}

View File

@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: 57959ce1a0581284c8bb37655a81dc1e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,14 +0,0 @@
using System;
using System.IO;
using System.Xml;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
namespace YooAsset.Editor
{
public class AssetBundleGrouperConfigImporter
{
}
}

View File

@ -24,6 +24,17 @@ namespace YooAsset.Editor
public List<AssetBundleGrouper> Groupers = new List<AssetBundleGrouper>(); public List<AssetBundleGrouper> Groupers = new List<AssetBundleGrouper>();
/// <summary>
/// 检测配置错误
/// </summary>
public void CheckConfigError()
{
foreach (var grouper in Groupers)
{
grouper.CheckConfigError();
}
}
/// <summary> /// <summary>
/// 获取打包收集的资源文件 /// 获取打包收集的资源文件
/// </summary> /// </summary>

View File

@ -28,7 +28,7 @@ namespace YooAsset.Editor
private TextField _grouperNameTxt; private TextField _grouperNameTxt;
private TextField _grouperDescTxt; private TextField _grouperDescTxt;
private TextField _grouperAssetTagsTxt; private TextField _grouperAssetTagsTxt;
private VisualElement _rightContainer; private VisualElement _grouperContainer;
public void CreateGUI() public void CreateGUI()
{ {
@ -50,16 +50,20 @@ namespace YooAsset.Editor
try try
{ {
// 导入导出按钮
var exportBtn = root.Q<Button>("ExportButton");
exportBtn.clicked += ExportBtn_clicked;
var importBtn = root.Q<Button>("ImportButton");
importBtn.clicked += ImportBtn_clicked;
// 着色器相关 // 着色器相关
_autoCollectShaderToogle = root.Q<Toggle>("AutoCollectShader"); _autoCollectShaderToogle = root.Q<Toggle>("AutoCollectShader");
_autoCollectShaderToogle.SetValueWithoutNotify(AssetBundleGrouperSettingData.Setting.AutoCollectShaders);
_autoCollectShaderToogle.RegisterValueChangedCallback(evt => _autoCollectShaderToogle.RegisterValueChangedCallback(evt =>
{ {
AssetBundleGrouperSettingData.ModifyShader(evt.newValue, _shaderBundleNameTxt.value); AssetBundleGrouperSettingData.ModifyShader(evt.newValue, _shaderBundleNameTxt.value);
_shaderBundleNameTxt.SetEnabled(evt.newValue);
}); });
_shaderBundleNameTxt = root.Q<TextField>("ShaderBundleName"); _shaderBundleNameTxt = root.Q<TextField>("ShaderBundleName");
_shaderBundleNameTxt.SetValueWithoutNotify(AssetBundleGrouperSettingData.Setting.ShadersBundleName);
_shaderBundleNameTxt.RegisterValueChangedCallback(evt => _shaderBundleNameTxt.RegisterValueChangedCallback(evt =>
{ {
AssetBundleGrouperSettingData.ModifyShader(_autoCollectShaderToogle.value, evt.newValue); AssetBundleGrouperSettingData.ModifyShader(_autoCollectShaderToogle.value, evt.newValue);
@ -84,9 +88,8 @@ namespace YooAsset.Editor
removeBtn.clicked += RemoveGrouperBtn_clicked; removeBtn.clicked += RemoveGrouperBtn_clicked;
} }
// 右侧容器 // 分组容器
_rightContainer = root.Q("RightContainer"); _grouperContainer = root.Q("GrouperContainer");
_rightContainer.visible = false;
// 分组信息相关 // 分组信息相关
_grouperNameTxt = root.Q<TextField>("GrouperName"); _grouperNameTxt = root.Q<TextField>("GrouperName");
@ -141,14 +144,44 @@ namespace YooAsset.Editor
removeBtn.clicked += RemoveCollectorBtn_clicked; removeBtn.clicked += RemoveCollectorBtn_clicked;
} }
// 初始化界面 // 刷新窗体
FillGrouperViewData(); RefreshWindow();
} }
catch (System.Exception e) catch (System.Exception e)
{ {
Debug.LogError(e.ToString()); Debug.LogError(e.ToString());
} }
} }
// 刷新窗体
private void RefreshWindow()
{
_autoCollectShaderToogle.SetValueWithoutNotify(AssetBundleGrouperSettingData.Setting.AutoCollectShaders);
_shaderBundleNameTxt.SetEnabled(AssetBundleGrouperSettingData.Setting.AutoCollectShaders);
_shaderBundleNameTxt.SetValueWithoutNotify(AssetBundleGrouperSettingData.Setting.ShadersBundleName);
_grouperContainer.visible = false;
FillGrouperViewData();
}
// 导入导出按钮
private void ExportBtn_clicked()
{
string resultPath = EditorTools.OpenFolderPanel("Export XML", "Assets/");
if (resultPath != null)
{
AssetBundleGrouperConfig.ExportXmlConfig($"{resultPath}/{nameof(AssetBundleGrouperConfig)}.xml");
}
}
private void ImportBtn_clicked()
{
string resultPath = EditorTools.OpenFilePath("Import XML", "Assets/", "xml");
if (resultPath != null)
{
AssetBundleGrouperConfig.ImportXmlConfig(resultPath);
RefreshWindow();
}
}
// 分组列表相关 // 分组列表相关
private void FillGrouperViewData() private void FillGrouperViewData()
@ -178,7 +211,10 @@ namespace YooAsset.Editor
// Grouper Name // Grouper Name
var textField1 = element.Q<Label>("Label1"); var textField1 = element.Q<Label>("Label1");
textField1.text = grouper.GrouperName; if (string.IsNullOrEmpty(grouper.GrouperDesc))
textField1.text = grouper.GrouperName;
else
textField1.text = $"{grouper.GrouperName} ({grouper.GrouperDesc})";
} }
private void GrouperListView_onSelectionChange(IEnumerable<object> objs) private void GrouperListView_onSelectionChange(IEnumerable<object> objs)
{ {
@ -205,11 +241,11 @@ namespace YooAsset.Editor
var selectGrouper = _grouperListView.selectedItem as AssetBundleGrouper; var selectGrouper = _grouperListView.selectedItem as AssetBundleGrouper;
if (selectGrouper == null) if (selectGrouper == null)
{ {
_rightContainer.visible = false; _grouperContainer.visible = false;
return; return;
} }
_rightContainer.visible = true; _grouperContainer.visible = true;
_collectorListView.Clear(); _collectorListView.Clear();
_collectorListView.ClearSelection(); _collectorListView.ClearSelection();
_collectorListView.itemsSource = selectGrouper.Collectors; _collectorListView.itemsSource = selectGrouper.Collectors;

View File

@ -4,27 +4,29 @@
<ui:Button text="导出" display-tooltip-when-elided="true" name="ExportButton" style="width: 50px; background-color: rgb(56, 147, 58);" /> <ui:Button text="导出" display-tooltip-when-elided="true" name="ExportButton" style="width: 50px; background-color: rgb(56, 147, 58);" />
<ui:Button text="导入" display-tooltip-when-elided="true" name="ImportButton" style="width: 50px; background-color: rgb(56, 147, 58);" /> <ui:Button text="导入" display-tooltip-when-elided="true" name="ImportButton" style="width: 50px; background-color: rgb(56, 147, 58);" />
</uie:Toolbar> </uie:Toolbar>
<ui:VisualElement name="ShaderContainer" style="height: 24px; background-color: rgb(77, 77, 77); border-left-width: 2px; border-right-width: 2px; border-top-width: 2px; border-bottom-width: 2px; flex-direction: row; border-top-left-radius: 2px; border-bottom-left-radius: 2px; border-top-right-radius: 2px; border-bottom-right-radius: 2px;">
<ui:Toggle label="Auto Collect Shaders" name="AutoCollectShader" style="width: 196px; -unity-text-align: middle-left;" />
<ui:TextField picking-mode="Ignore" label="Shader Bundle Name" name="ShaderBundleName" style="flex-grow: 1; -unity-text-align: middle-left;" />
</ui:VisualElement>
<ui:VisualElement name="ContentContainer" style="flex-grow: 1; flex-direction: row;"> <ui:VisualElement name="ContentContainer" style="flex-grow: 1; flex-direction: row;">
<ui:VisualElement name="LeftContainer" style="width: 200px; flex-grow: 0; background-color: rgb(67, 67, 67); border-left-width: 5px; border-right-width: 5px; border-top-width: 5px; border-bottom-width: 5px;"> <ui:VisualElement name="LeftContainer" style="width: 200px; flex-grow: 0; background-color: rgb(67, 67, 67); border-left-width: 5px; border-right-width: 5px; border-top-width: 5px; border-bottom-width: 5px;">
<ui:VisualElement name="GrouperAddContainer" style="height: 20px; flex-direction: row;">
<ui:Button text=" - " display-tooltip-when-elided="true" name="RemoveBtn" />
<ui:Button text=" + " display-tooltip-when-elided="true" name="AddBtn" />
</ui:VisualElement>
<ui:ListView focusable="true" name="GrouperListView" item-height="20" style="flex-grow: 1;" /> <ui:ListView focusable="true" name="GrouperListView" item-height="20" style="flex-grow: 1;" />
</ui:VisualElement> <ui:VisualElement name="GrouperAddContainer" style="height: 20px; flex-direction: row; justify-content: center;">
<ui:VisualElement name="RightContainer" style="flex-grow: 1; border-left-width: 5px; border-right-width: 5px; border-top-width: 5px; border-bottom-width: 5px;">
<ui:TextField picking-mode="Ignore" label="Grouper Name" name="GrouperName" />
<ui:TextField picking-mode="Ignore" label="Grouper Desc" name="GrouperDesc" />
<ui:TextField picking-mode="Ignore" label="Grouper Asset Tags" name="GrouperAssetTags" />
<ui:VisualElement name="CollectorAddContainer" style="height: 20px; flex-direction: row-reverse;">
<ui:Button text=" - " display-tooltip-when-elided="true" name="RemoveBtn" /> <ui:Button text=" - " display-tooltip-when-elided="true" name="RemoveBtn" />
<ui:Button text=" + " display-tooltip-when-elided="true" name="AddBtn" /> <ui:Button text=" + " display-tooltip-when-elided="true" name="AddBtn" />
</ui:VisualElement> </ui:VisualElement>
<ui:ListView focusable="true" name="CollectorListView" item-height="50" style="flex-grow: 1;" /> </ui:VisualElement>
<ui:VisualElement name="RightContainer" style="flex-direction: column; flex-grow: 1;">
<ui:VisualElement name="ShaderContainer" style="height: 30px; background-color: rgb(67, 67, 67); flex-direction: row; border-left-width: 5px; border-right-width: 5px; border-top-width: 5px; border-bottom-width: 5px;">
<ui:Toggle label="Auto Collect Shaders" name="AutoCollectShader" style="width: 196px; -unity-text-align: middle-left;" />
<ui:TextField picking-mode="Ignore" label="Shader Bundle Name" name="ShaderBundleName" style="flex-grow: 1; -unity-text-align: middle-left;" />
</ui:VisualElement>
<ui:VisualElement name="GrouperContainer" style="flex-grow: 1; border-left-width: 5px; border-right-width: 5px; border-top-width: 5px; border-bottom-width: 5px;">
<ui:TextField picking-mode="Ignore" label="Grouper Name" name="GrouperName" />
<ui:TextField picking-mode="Ignore" label="Grouper Desc" name="GrouperDesc" />
<ui:TextField picking-mode="Ignore" label="Grouper Asset Tags" name="GrouperAssetTags" />
<ui:VisualElement name="CollectorAddContainer" style="height: 20px; flex-direction: row-reverse;">
<ui:Button text=" - " display-tooltip-when-elided="true" name="RemoveBtn" />
<ui:Button text=" + " display-tooltip-when-elided="true" name="AddBtn" />
</ui:VisualElement>
<ui:ListView focusable="true" name="CollectorListView" item-height="50" style="flex-grow: 1;" />
</ui:VisualElement>
</ui:VisualElement> </ui:VisualElement>
</ui:VisualElement> </ui:VisualElement>
</ui:UXML> </ui:UXML>