diff --git a/Assets/YooAsset/Editor/MacrosProcessor.cs b/Assets/YooAsset/Editor/MacrosProcessor.cs index 465ee5ff..3ad49de6 100644 --- a/Assets/YooAsset/Editor/MacrosProcessor.cs +++ b/Assets/YooAsset/Editor/MacrosProcessor.cs @@ -3,87 +3,75 @@ using System.IO; using System.Linq; using System.Xml; using UnityEditor; -using UnityEngine; namespace YooAsset.Editor { [InitializeOnLoad] - class YooMacrosProcessor : AssetPostprocessor + public class MacrosProcessor : AssetPostprocessor { - static readonly List YooAssertMacros = new List() + static MacrosProcessor() { - "YOO_VERSION_2", - "YOO_VERSION_2_3_4", - "YOO_VERSION_2_3_4_OR_NEWER", + } + + /// + /// YooAsset版本宏定义 + /// + private static readonly List YooAssetMacros = new List() + { + "YOO_ASSET_2", + "YOO_ASSET_2_3", + "YOO_ASSET_2_3_OR_NEWER", }; + static string OnGeneratedCSProject(string path, string content) { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(content); - if (!IsCSProjectReferenced(xmlDoc.DocumentElement)) - { + if (IsCSProjectReferenced(xmlDoc.DocumentElement) == false) return content; - } - if (!ProcessDefineConstants(xmlDoc.DocumentElement, YooAssertMacros)) - { + if (ProcessDefineConstants(xmlDoc.DocumentElement) == false) return content; - } - StringWriter sw = new StringWriter(); - XmlWriter writer = XmlWriter.Create(sw, new XmlWriterSettings() - { - Indent = true, - }); - xmlDoc.WriteTo(writer); - writer.Flush(); - - return sw.ToString(); + // 将修改后的XML结构重新输出为文本 + var stringWriter = new StringWriter(); + var writerSettings = new XmlWriterSettings(); + writerSettings.Indent = true; + var xmlWriter = XmlWriter.Create(stringWriter, writerSettings); + xmlDoc.WriteTo(xmlWriter); + xmlWriter.Flush(); + return stringWriter.ToString(); } - static bool ProcessDefineConstants(XmlElement element, List macros) + /// + /// 处理宏定义 + /// + private static bool ProcessDefineConstants(XmlElement element) { 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) + HashSet hashSets = new HashSet(defines); + foreach (string yooMacro in YooAssetMacros) { - tmpMacro = macro.Trim(); - if (string.IsNullOrEmpty(tmpMacro)) - continue; - //加入YooAsset定义的宏 - hs.Add(tmpMacro); + string tmpMacro = yooMacro.Trim(); + if (hashSets.Contains(tmpMacro) == false) + hashSets.Add(tmpMacro); } - //更新节点InnerText - childNode.InnerText = string.Join(";", hs.ToArray()); - + childNode.InnerText = string.Join(";", hashSets.ToArray()); processed = true; } } @@ -91,36 +79,31 @@ namespace YooAsset.Editor return processed; } - static bool IsCSProjectReferenced(XmlElement element) + /// + /// 检测工程是否引用了YooAsset + /// + private 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/Runtime/YooAsset.asmdef b/Assets/YooAsset/Runtime/YooAsset.asmdef index f363ce42..8f1c3e77 100644 --- a/Assets/YooAsset/Runtime/YooAsset.asmdef +++ b/Assets/YooAsset/Runtime/YooAsset.asmdef @@ -12,12 +12,18 @@ "versionDefines": [ { "name": "com.tuyoogame.yooasset", - "define": "YOO_VERSION_2_3_4" + "expression": "", + "define": "YOO_ASSET_2" }, { "name": "com.tuyoogame.yooasset", - "expression": "2.3.4", - "define": "YOO_VERSION_2_3_4_OR_NEWER" + "expression": "2.3", + "define": "YOO_ASSET_2_3" + }, + { + "name": "com.tuyoogame.yooasset", + "expression": "2.3", + "define": "YOO_ASSET_2_3_OR_NEWER" } ], "noEngineReferences": false