From 9bcbc108625834ab6a2f1643885b88ad3d76bc03 Mon Sep 17 00:00:00 2001 From: Y-way Date: Fri, 7 Mar 2025 15:00:20 +0800 Subject: [PATCH] Support DefineConstants for YooAsset Package MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 自定义YOO_VERSION_2 以及版本管理宏,用于YooAsset升级时的宏定义处理 --- Assets/YooAsset/Editor/MacrosProcessor.cs | 126 ++++++++++++++++++ .../YooAsset/Editor/MacrosProcessor.cs.meta | 11 ++ Assets/YooAsset/Runtime/YooAsset.asmdef | 12 +- 3 files changed, 148 insertions(+), 1 deletion(-) create mode 100644 Assets/YooAsset/Editor/MacrosProcessor.cs create mode 100644 Assets/YooAsset/Editor/MacrosProcessor.cs.meta diff --git a/Assets/YooAsset/Editor/MacrosProcessor.cs b/Assets/YooAsset/Editor/MacrosProcessor.cs new file mode 100644 index 00000000..465ee5ff --- /dev/null +++ b/Assets/YooAsset/Editor/MacrosProcessor.cs @@ -0,0 +1,126 @@ +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Xml; +using UnityEditor; +using UnityEngine; + +namespace YooAsset.Editor +{ + [InitializeOnLoad] + class YooMacrosProcessor : AssetPostprocessor + { + static readonly List YooAssertMacros = new List() + { + "YOO_VERSION_2", + "YOO_VERSION_2_3_4", + "YOO_VERSION_2_3_4_OR_NEWER", + }; + static string OnGeneratedCSProject(string path, string content) + { + XmlDocument xmlDoc = new XmlDocument(); + xmlDoc.LoadXml(content); + + if (!IsCSProjectReferenced(xmlDoc.DocumentElement)) + { + return content; + } + + if (!ProcessDefineConstants(xmlDoc.DocumentElement, YooAssertMacros)) + { + return content; + } + + StringWriter sw = new StringWriter(); + XmlWriter writer = XmlWriter.Create(sw, new XmlWriterSettings() + { + Indent = true, + }); + xmlDoc.WriteTo(writer); + writer.Flush(); + + return sw.ToString(); + } + + static bool ProcessDefineConstants(XmlElement element, List macros) + { + if (element == null) + { + return false; + } + + bool processed = false; + if (macros == null || macros.Count == 0) + { + return processed; + } + + foreach (XmlNode node in element.ChildNodes) + { + if (node.Name != "PropertyGroup") + { + continue; + } + + foreach (XmlNode childNode in node.ChildNodes) + { + if (childNode.Name != "DefineConstants") + { + continue; + } + + string[] defines = childNode.InnerText.Split(';'); + HashSet hs = new HashSet(defines); + + string tmpMacro = string.Empty; + foreach (string macro in macros) + { + tmpMacro = macro.Trim(); + if (string.IsNullOrEmpty(tmpMacro)) + continue; + //加入YooAsset定义的宏 + hs.Add(tmpMacro); + } + //更新节点InnerText + childNode.InnerText = string.Join(";", hs.ToArray()); + + processed = true; + } + } + + return processed; + } + + static bool IsCSProjectReferenced(XmlElement element) + { + if (element == null) + { + return false; + } + + foreach (XmlNode node in element.ChildNodes) + { + if (node.Name != "ItemGroup") + { + continue; + } + foreach (XmlNode childNode in node.ChildNodes) + { + if (childNode.Name != "Reference" && childNode.Name != "ProjectReference") + { + continue; + } + //工程引用了YooAsset + string include = childNode.Attributes["Include"].Value; + if (include.Contains("YooAsset")) + { + return true; + } + } + } + + return false; + } + } + +} diff --git a/Assets/YooAsset/Editor/MacrosProcessor.cs.meta b/Assets/YooAsset/Editor/MacrosProcessor.cs.meta new file mode 100644 index 00000000..89d654cd --- /dev/null +++ b/Assets/YooAsset/Editor/MacrosProcessor.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 88d5a41d078a82e40b82265ed4c3631a +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Runtime/YooAsset.asmdef b/Assets/YooAsset/Runtime/YooAsset.asmdef index e61c9d1b..f363ce42 100644 --- a/Assets/YooAsset/Runtime/YooAsset.asmdef +++ b/Assets/YooAsset/Runtime/YooAsset.asmdef @@ -9,6 +9,16 @@ "precompiledReferences": [], "autoReferenced": true, "defineConstraints": [], - "versionDefines": [], + "versionDefines": [ + { + "name": "com.tuyoogame.yooasset", + "define": "YOO_VERSION_2_3_4" + }, + { + "name": "com.tuyoogame.yooasset", + "expression": "2.3.4", + "define": "YOO_VERSION_2_3_4_OR_NEWER" + } + ], "noEngineReferences": false } \ No newline at end of file