From 3a76f369e832144150112dbb3e9b2d5defd734d7 Mon Sep 17 00:00:00 2001 From: monitor1394 Date: Thu, 4 Mar 2021 07:30:29 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96TMP=E7=9A=84Enable=E5=92=8CDi?= =?UTF-8?q?sable?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Editor/XChartEditor.cs | 83 +-------------------------------- Runtime/XChartsMgr.cs | 101 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+), 81 deletions(-) diff --git a/Editor/XChartEditor.cs b/Editor/XChartEditor.cs index b0c8aad6..9568423a 100644 --- a/Editor/XChartEditor.cs +++ b/Editor/XChartEditor.cs @@ -155,12 +155,12 @@ namespace XCharts [MenuItem("XCharts/TextMeshPro Enable")] public static void EnableTextMeshPro() { - if (!IsExistTMPAssembly()) + if (!XChartsMgr.IsExistTMPAssembly()) { Debug.LogError("TextMeshPro is not in the project, please import TextMeshPro package first."); return; } - AddTMPRefence(); + XChartsMgr.ModifyTMPRefence(); XChartsMgr.EnableTextMeshPro(); } @@ -169,84 +169,5 @@ namespace XCharts { XChartsMgr.DisableTextMeshPro(); } - - private static bool IsExistTMPAssembly() - { - foreach (var assembly in CompilationPipeline.GetAssemblies(AssembliesType.Player)) - { - if (assembly.name.Equals("Unity.TextMeshPro")) return true; - } - return false; - } - - private static bool AddTMPRefence() - { - var packagePath = XChartsMgr.GetPackageFullPath(); - if (!AddTMPRefence(packagePath + "/Runtime/XCharts.Runtime.asmdef")) return false; - if (!AddTMPRefence(packagePath + "/Editor/XCharts.Editor.asmdef")) return false; - return true; - } - - private static bool AddTMPRefence(string asmdefPath) - { - if (!File.Exists(asmdefPath)) - { - Debug.LogError("AddTMPRefence ERROR: can't find: " + asmdefPath); - return false; - } - var oldText = File.ReadAllText(asmdefPath); - try - { - var dest = new List(); - var refs = new List(); - var lines = File.ReadAllLines(asmdefPath); - var referencesStart = false; - var addTMP = false; - foreach (var line in lines) - { - if (string.IsNullOrWhiteSpace(line)) continue; - if (line.Contains("\"references\": [")) - { - dest.Add(line); - referencesStart = true; - } - else if (referencesStart) - { - if (line.Contains("],")) - { - referencesStart = false; - if (!refs.Contains("\"Unity.TextMeshPro\":")) - { - if (refs.Count > 0) - dest[dest.Count - 1] = dest[dest.Count - 1] + ","; - dest.Add(" \"Unity.TextMeshPro\""); - dest.Add(line); - addTMP = true; - } - else - { - dest.Add(line); - } - } - else - { - dest.Add(line); - refs.Add(line.Trim()); - } - } - else - { - dest.Add(line); - } - } - if (addTMP) File.WriteAllText(asmdefPath, string.Join("\n", dest)); - return true; - } - catch (Exception e) - { - Debug.LogError("AddTMPRefence ERROR:" + e.Message); - return false; - } - } } } \ No newline at end of file diff --git a/Runtime/XChartsMgr.cs b/Runtime/XChartsMgr.cs index 6b7cb3e3..4ebcbadf 100644 --- a/Runtime/XChartsMgr.cs +++ b/Runtime/XChartsMgr.cs @@ -17,6 +17,7 @@ using UnityEngine.SceneManagement; using System.IO; #if UNITY_EDITOR using UnityEditor; +using UnityEditor.Compilation; #endif namespace XCharts @@ -471,6 +472,106 @@ namespace XCharts DefineSymbolsUtil.RemoveGlobalDefine("dUI_TextMeshPro"); RemoveAllChartObject(); } + + public static bool IsExistTMPAssembly() + { + +#if UNITY_2018_1_OR_NEWER + foreach (var assembly in CompilationPipeline.GetAssemblies(AssembliesType.Player)) + { + if (assembly.name.Equals("Unity.TextMeshPro")) return true; + } +#else + foreach (var assembly in CompilationPipeline.GetAssemblies()) + { + if (assembly.name.Equals("Unity.TextMeshPro")) return true; + } +#endif + return false; + } + + public static bool ModifyTMPRefence(bool removeTMP = false) + { + var packagePath = XChartsMgr.GetPackageFullPath(); + if (!ModifyTMPRefence(packagePath + "/Runtime/XCharts.Runtime.asmdef", removeTMP)) return false; + if (!ModifyTMPRefence(packagePath + "/Editor/XCharts.Editor.asmdef", removeTMP)) return false; + return true; + } + + private static bool ModifyTMPRefence(string asmdefPath, bool removeTMP = false) + { + if (!File.Exists(asmdefPath)) + { + Debug.LogError("AddTMPRefence ERROR: can't find: " + asmdefPath); + return false; + } + var oldText = File.ReadAllText(asmdefPath); + try + { + var dest = new List(); + var refs = new List(); + var lines = File.ReadAllLines(asmdefPath); + var referencesStart = false; + var addedTMP = false; + var removedTMP = false; + var tmpName = "\"Unity.TextMeshPro\""; + foreach (var line in lines) + { + if (string.IsNullOrWhiteSpace(line)) continue; + if (line.Contains("\"references\": [")) + { + dest.Add(line); + referencesStart = true; + } + else if (referencesStart) + { + if (line.Contains("],")) + { + referencesStart = false; + if (!removeTMP && !refs.Contains(tmpName)) + { + if (refs.Count > 0) + dest[dest.Count - 1] = dest[dest.Count - 1] + ","; + dest.Add(" " + tmpName); + dest.Add(line); + addedTMP = true; + } + else + { + dest.Add(line); + } + } + else + { + if (removeTMP) + { + if (!line.Contains(tmpName)) + { + removedTMP = true; + dest.Add(line); + } + } + else + { + dest.Add(line); + refs.Add(line.Trim()); + } + } + } + else + { + dest.Add(line); + } + } + if (addedTMP || removedTMP) File.WriteAllText(asmdefPath, string.Join("\n", dest)); + return true; + } + catch (System.Exception e) + { + Debug.LogError("AddTMPRefence ERROR:" + e.Message); + return false; + } + } #endif } } \ No newline at end of file