diff --git a/Editor/XChartEditor.cs b/Editor/XChartEditor.cs index a0e437bf..b0c8aad6 100644 --- a/Editor/XChartEditor.cs +++ b/Editor/XChartEditor.cs @@ -9,6 +9,10 @@ using UnityEditor; using UnityEngine; using UnityEngine.UI; using UnityEngine.EventSystems; +using UnityEditor.Compilation; +using System.IO; +using System; +using System.Collections.Generic; namespace XCharts { @@ -151,6 +155,12 @@ namespace XCharts [MenuItem("XCharts/TextMeshPro Enable")] public static void EnableTextMeshPro() { + if (!IsExistTMPAssembly()) + { + Debug.LogError("TextMeshPro is not in the project, please import TextMeshPro package first."); + return; + } + AddTMPRefence(); XChartsMgr.EnableTextMeshPro(); } @@ -159,5 +169,84 @@ 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 f6bf7381..6b7cb3e3 100644 --- a/Runtime/XChartsMgr.cs +++ b/Runtime/XChartsMgr.cs @@ -14,6 +14,7 @@ using System.Text.RegularExpressions; using UnityEngine; using UnityEngine.Networking; using UnityEngine.SceneManagement; +using System.IO; #if UNITY_EDITOR using UnityEditor; #endif @@ -412,10 +413,55 @@ namespace XCharts } } + public static string GetPackageFullPath() + { + string packagePath = Path.GetFullPath("Packages/com.monitor1394.xcharts"); + if (Directory.Exists(packagePath)) + { + return packagePath; + } + packagePath = Path.GetFullPath("Assets/.."); + if (Directory.Exists(packagePath)) + { + // Search default location for development package + if (File.Exists(packagePath + "/Assets/Packages/com.monitor1394.xcharts/package.json")) + { + return packagePath + "/Assets/Packages/com.monitor1394.xcharts"; + } + + // Search for default location of normal XCharts AssetStore package + if (File.Exists(packagePath + "/Assets/XCharts/package.json")) + { + return packagePath + "/Assets/XCharts"; + } + + // Search for potential alternative locations in the user project + string[] matchingPaths = Directory.GetDirectories(packagePath, "XCharts", SearchOption.AllDirectories); + string path = ValidateLocation(matchingPaths, packagePath); + if (path != null) return packagePath + path; + } + + return null; + } + + private static string ValidateLocation(string[] paths, string projectPath) + { + for (int i = 0; i < paths.Length; i++) + { + if (File.Exists(paths[i] + "/package.json")) + { + string folderPath = paths[i].Replace(projectPath, ""); + folderPath = folderPath.TrimStart('\\', '/'); + return folderPath; + } + } + + return null; + } + #if UNITY_EDITOR public static void EnableTextMeshPro() { - DefineSymbolsUtil.AddGlobalDefine("dUI_TextMeshPro"); RemoveAllChartObject(); }