From 528eccbd9ea7336c59dbb9e3e59a27411006f2f4 Mon Sep 17 00:00:00 2001 From: hevinci Date: Sat, 2 Apr 2022 18:34:46 +0800 Subject: [PATCH] Update AssetBundleGrouper --- .../AssetBundleCollector.cs | 15 ++ .../AssetBundleGrouper/AssetBundleGrouper.cs | 11 ++ .../AssetBundleGrouperConfig.cs | 162 ++++++++++++++++++ ....meta => AssetBundleGrouperConfig.cs.meta} | 0 .../AssetBundleGrouperConfigDefine.cs | 17 -- .../AssetBundleGrouperConfigDefine.cs.meta | 11 -- .../AssetBundleGrouperConfigExporter.cs | 14 -- .../AssetBundleGrouperConfigExporter.cs.meta | 11 -- .../AssetBundleGrouperConfigImporter.cs | 14 -- .../AssetBundleGrouperSetting.cs | 11 ++ .../AssetBundleGrouperWindow.cs | 60 +++++-- .../AssetBundleGrouperWindow.uxml | 32 ++-- 12 files changed, 264 insertions(+), 94 deletions(-) create mode 100644 Assets/YooAsset/Editor/AssetBundleGrouper/AssetBundleGrouperConfig.cs rename Assets/YooAsset/Editor/AssetBundleGrouper/{AssetBundleGrouperConfigImporter.cs.meta => AssetBundleGrouperConfig.cs.meta} (100%) delete mode 100644 Assets/YooAsset/Editor/AssetBundleGrouper/AssetBundleGrouperConfigDefine.cs delete mode 100644 Assets/YooAsset/Editor/AssetBundleGrouper/AssetBundleGrouperConfigDefine.cs.meta delete mode 100644 Assets/YooAsset/Editor/AssetBundleGrouper/AssetBundleGrouperConfigExporter.cs delete mode 100644 Assets/YooAsset/Editor/AssetBundleGrouper/AssetBundleGrouperConfigExporter.cs.meta delete mode 100644 Assets/YooAsset/Editor/AssetBundleGrouper/AssetBundleGrouperConfigImporter.cs diff --git a/Assets/YooAsset/Editor/AssetBundleGrouper/AssetBundleCollector.cs b/Assets/YooAsset/Editor/AssetBundleGrouper/AssetBundleCollector.cs index 90708ab..b2eec3f 100644 --- a/Assets/YooAsset/Editor/AssetBundleGrouper/AssetBundleCollector.cs +++ b/Assets/YooAsset/Editor/AssetBundleGrouper/AssetBundleCollector.cs @@ -35,6 +35,21 @@ namespace YooAsset.Editor public string AssetTags = string.Empty; + /// + /// 检测配置错误 + /// + public void CheckConfigError() + { + if(AssetDatabase.LoadAssetAtPath(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}"); + } + /// /// 获取打包收集的资源文件 /// diff --git a/Assets/YooAsset/Editor/AssetBundleGrouper/AssetBundleGrouper.cs b/Assets/YooAsset/Editor/AssetBundleGrouper/AssetBundleGrouper.cs index b462ef2..7046ac2 100644 --- a/Assets/YooAsset/Editor/AssetBundleGrouper/AssetBundleGrouper.cs +++ b/Assets/YooAsset/Editor/AssetBundleGrouper/AssetBundleGrouper.cs @@ -31,6 +31,17 @@ namespace YooAsset.Editor public List Collectors = new List(); + /// + /// 检测配置错误 + /// + public void CheckConfigError() + { + foreach(var collector in Collectors) + { + collector.CheckConfigError(); + } + } + /// /// 获取打包收集的资源文件 /// diff --git a/Assets/YooAsset/Editor/AssetBundleGrouper/AssetBundleGrouperConfig.cs b/Assets/YooAsset/Editor/AssetBundleGrouper/AssetBundleGrouperConfig.cs new file mode 100644 index 0000000..c0cb7ef --- /dev/null +++ b/Assets/YooAsset/Editor/AssetBundleGrouper/AssetBundleGrouperConfig.cs @@ -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"; + + + /// + /// 导入XML配置表 + /// + 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 grouperTemper = new List(); + 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($"导入配置完毕!"); + } + + /// + /// 导出XML配置表 + /// + public static void ExportXmlConfig(string savePath) + { + if (File.Exists(savePath)) + File.Delete(savePath); + + StringBuilder sb = new StringBuilder(); + sb.AppendLine(""); + sb.AppendLine(""); + sb.AppendLine(""); + + 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($"导出配置完毕!"); + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Editor/AssetBundleGrouper/AssetBundleGrouperConfigImporter.cs.meta b/Assets/YooAsset/Editor/AssetBundleGrouper/AssetBundleGrouperConfig.cs.meta similarity index 100% rename from Assets/YooAsset/Editor/AssetBundleGrouper/AssetBundleGrouperConfigImporter.cs.meta rename to Assets/YooAsset/Editor/AssetBundleGrouper/AssetBundleGrouperConfig.cs.meta diff --git a/Assets/YooAsset/Editor/AssetBundleGrouper/AssetBundleGrouperConfigDefine.cs b/Assets/YooAsset/Editor/AssetBundleGrouper/AssetBundleGrouperConfigDefine.cs deleted file mode 100644 index c1f48b6..0000000 --- a/Assets/YooAsset/Editor/AssetBundleGrouper/AssetBundleGrouperConfigDefine.cs +++ /dev/null @@ -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"; - - - } -} \ No newline at end of file diff --git a/Assets/YooAsset/Editor/AssetBundleGrouper/AssetBundleGrouperConfigDefine.cs.meta b/Assets/YooAsset/Editor/AssetBundleGrouper/AssetBundleGrouperConfigDefine.cs.meta deleted file mode 100644 index 0325996..0000000 --- a/Assets/YooAsset/Editor/AssetBundleGrouper/AssetBundleGrouperConfigDefine.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: f2190cc6d8995aa458ded22ffd7fc1e0 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/YooAsset/Editor/AssetBundleGrouper/AssetBundleGrouperConfigExporter.cs b/Assets/YooAsset/Editor/AssetBundleGrouper/AssetBundleGrouperConfigExporter.cs deleted file mode 100644 index 47169fd..0000000 --- a/Assets/YooAsset/Editor/AssetBundleGrouper/AssetBundleGrouperConfigExporter.cs +++ /dev/null @@ -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 - { - - } -} \ No newline at end of file diff --git a/Assets/YooAsset/Editor/AssetBundleGrouper/AssetBundleGrouperConfigExporter.cs.meta b/Assets/YooAsset/Editor/AssetBundleGrouper/AssetBundleGrouperConfigExporter.cs.meta deleted file mode 100644 index 7168819..0000000 --- a/Assets/YooAsset/Editor/AssetBundleGrouper/AssetBundleGrouperConfigExporter.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 57959ce1a0581284c8bb37655a81dc1e -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/YooAsset/Editor/AssetBundleGrouper/AssetBundleGrouperConfigImporter.cs b/Assets/YooAsset/Editor/AssetBundleGrouper/AssetBundleGrouperConfigImporter.cs deleted file mode 100644 index dc9e596..0000000 --- a/Assets/YooAsset/Editor/AssetBundleGrouper/AssetBundleGrouperConfigImporter.cs +++ /dev/null @@ -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 - { - - } -} \ No newline at end of file diff --git a/Assets/YooAsset/Editor/AssetBundleGrouper/AssetBundleGrouperSetting.cs b/Assets/YooAsset/Editor/AssetBundleGrouper/AssetBundleGrouperSetting.cs index 0ff2b8f..fe3c4ac 100644 --- a/Assets/YooAsset/Editor/AssetBundleGrouper/AssetBundleGrouperSetting.cs +++ b/Assets/YooAsset/Editor/AssetBundleGrouper/AssetBundleGrouperSetting.cs @@ -24,6 +24,17 @@ namespace YooAsset.Editor public List Groupers = new List(); + /// + /// 检测配置错误 + /// + public void CheckConfigError() + { + foreach (var grouper in Groupers) + { + grouper.CheckConfigError(); + } + } + /// /// 获取打包收集的资源文件 /// diff --git a/Assets/YooAsset/Editor/AssetBundleGrouper/AssetBundleGrouperWindow.cs b/Assets/YooAsset/Editor/AssetBundleGrouper/AssetBundleGrouperWindow.cs index 51fea61..6b129a0 100644 --- a/Assets/YooAsset/Editor/AssetBundleGrouper/AssetBundleGrouperWindow.cs +++ b/Assets/YooAsset/Editor/AssetBundleGrouper/AssetBundleGrouperWindow.cs @@ -28,7 +28,7 @@ namespace YooAsset.Editor private TextField _grouperNameTxt; private TextField _grouperDescTxt; private TextField _grouperAssetTagsTxt; - private VisualElement _rightContainer; + private VisualElement _grouperContainer; public void CreateGUI() { @@ -50,16 +50,20 @@ namespace YooAsset.Editor try { + // 导入导出按钮 + var exportBtn = root.Q