From 633456ada011a1b410b432d827b2fdf664a5ca92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=BE=B9=E4=B8=8A=E6=B5=B7?= Date: Mon, 30 Jan 2023 19:48:59 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=86=20inputsystem=20=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=E6=B7=BB=E5=8A=A0=E5=88=B0=20xchart=20?= =?UTF-8?q?=E8=8F=9C=E5=8D=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 顺便简化了 tmp 支持相关逻辑 --- Editor/Windows/XChartsEditor.cs | 150 ++++++++++++++++++++++++++-- Runtime/Internal/XChartsMgr.cs | 170 ++------------------------------ 2 files changed, 154 insertions(+), 166 deletions(-) diff --git a/Editor/Windows/XChartsEditor.cs b/Editor/Windows/XChartsEditor.cs index d72e84ff..99ed4adc 100644 --- a/Editor/Windows/XChartsEditor.cs +++ b/Editor/Windows/XChartsEditor.cs @@ -1,8 +1,14 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; using UnityEditor; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI; using XCharts.Runtime; +using ADB = UnityEditor.AssetDatabase; + namespace XCharts.Editor { @@ -22,7 +28,7 @@ namespace XCharts.Editor canvas = canvasObject.AddComponent(); canvas.renderMode = RenderMode.ScreenSpaceCamera; var mainCamera = GameObject.FindGameObjectWithTag("MainCamera"); - canvas.worldCamera = mainCamera == null? null : mainCamera.GetComponent(); + canvas.worldCamera = mainCamera == null ? null : mainCamera.GetComponent(); canvasObject.AddComponent(); canvasObject.AddComponent(); if (GameObject.Find("EventSystem") == null) @@ -188,23 +194,155 @@ namespace XCharts.Editor XCThemeMgr.ReloadThemeList(); } + #region Text mesh pro support +#if UNITY_2017_1_OR_NEWER + const string SYMBOL_TMP = "dUI_TextMeshPro"; + const string ASMDEF_TMP = "Unity.TextMeshPro"; + [MenuItem("XCharts/TextMeshPro Enable")] public static void EnableTextMeshPro() { - if (!XChartsMgr.IsExistTMPAssembly()) + if (!IsSpecifyAssemblyExist(ASMDEF_TMP)) { Debug.LogError("TextMeshPro is not in the project, please import TextMeshPro package first."); return; } - XChartsMgr.EnableTextMeshPro(); - XChartsMgr.ModifyTMPRefence(); + DefineSymbolsUtil.AddGlobalDefine(SYMBOL_TMP); + XChartsMgr.RemoveAllChartObject(); + InsertSpecifyReferenceIntoAssembly(Platform.Editor, ASMDEF_TMP); + InsertSpecifyReferenceIntoAssembly(Platform.Runtime, ASMDEF_TMP); } [MenuItem("XCharts/TextMeshPro Disable")] public static void DisableTextMeshPro() { - XChartsMgr.ModifyTMPRefence(true); - XChartsMgr.DisableTextMeshPro(); + RemoveSpecifyReferenceFromAssembly(Platform.Editor, ASMDEF_TMP); + RemoveSpecifyReferenceFromAssembly(Platform.Runtime, ASMDEF_TMP); + DefineSymbolsUtil.RemoveGlobalDefine(SYMBOL_TMP); + XChartsMgr.RemoveAllChartObject(); } +#endif + #endregion + + #region InputSystem Support +#if UNITY_2019_1_OR_NEWER + //As InputSystem is released in 2019.1+ ,when unity version is 2019.1+ , enable InputSystem Support + const string SYMBOL_I_S = "INPUT_SYSTEM_ENABLED"; + const string ASMDEF_I_S = "Unity.InputSystem"; + [MenuItem("XCharts/InputSystem Enable")] + public static void EnableInputSystem() + { + if (!IsSpecifyAssemblyExist(ASMDEF_I_S)) + { + Debug.LogError("InputSystem is not in the project, please import InputSystem package first."); + return; + } + // insert input system package into editor and runtime assembly + InsertSpecifyReferenceIntoAssembly(Platform.Editor, ASMDEF_I_S); + InsertSpecifyReferenceIntoAssembly(Platform.Runtime, ASMDEF_I_S); + // add scripting define symbols + DefineSymbolsUtil.AddGlobalDefine(SYMBOL_I_S); + } + [MenuItem("XCharts/InputSystem Disable")] + public static void DisableInputSystem() + { + // remove input system package into editor and runtime assembly + RemoveSpecifyReferenceFromAssembly(Platform.Editor, ASMDEF_I_S); + RemoveSpecifyReferenceFromAssembly(Platform.Runtime, ASMDEF_I_S); + // remove scripting define symbols + DefineSymbolsUtil.RemoveGlobalDefine(SYMBOL_I_S); + } +#endif + #endregion + + #region Assistant members +#if UNITY_2017_1_OR_NEWER + // as text mesh pro is released in 2017.1, so we may use these function and types in 2017.1 or later + private static void InsertSpecifyReferenceIntoAssembly(Platform platform, string reference) + { + var file = GetPackageAssemblyDefinitionPath(platform); + var content = File.ReadAllText(file); + var data = new AssemblyDefinitionData(); + EditorJsonUtility.FromJsonOverwrite(content, data); + if (!data.references.Contains(reference)) + { + data.references.Add(reference); + var json = EditorJsonUtility.ToJson(data, true); + File.WriteAllText(file, json); + AssetDatabase.SaveAssets(); + AssetDatabase.Refresh(); + } + } + + private static void RemoveSpecifyReferenceFromAssembly(Platform platform, string reference) + { + var file = GetPackageAssemblyDefinitionPath(platform); + var content = File.ReadAllText(file); + var data = new AssemblyDefinitionData(); + EditorJsonUtility.FromJsonOverwrite(content, data); + if (data.references.Contains(reference)) + { + data.references.Remove(reference); + var json = EditorJsonUtility.ToJson(data, true); + File.WriteAllText(file, json); + } + } + + public enum Platform { Editor, Runtime } + public static string GetPackageAssemblyDefinitionPath(Platform platform) + { + var p = platform == Platform.Editor ? "Editor" : "Runtime"; + var f = "XCharts." + p + ".asmdef"; + var sub = Path.Combine(p, f); + string packagePath = Path.GetFullPath("Packages/com.monitor1394.xcharts"); + if (!Directory.Exists(packagePath)) + { + packagePath = ADB.FindAssets("t:Script") + .Where(v => Path.GetFileNameWithoutExtension(ADB.GUIDToAssetPath(v)) == "XChartsMgr") + .Select(id => ADB.GUIDToAssetPath(id)) + .FirstOrDefault(); + packagePath = Path.GetDirectoryName(packagePath); + packagePath = packagePath.Substring(0, packagePath.LastIndexOf("Runtime")); + } + return Path.Combine(packagePath, sub); + } + + public static bool IsSpecifyAssemblyExist(string name) + { +#if UNITY_2018_1_OR_NEWER + foreach (var assembly in UnityEditor.Compilation.CompilationPipeline.GetAssemblies(UnityEditor.Compilation.AssembliesType.Player)) + { + if (assembly.name.Equals(name)) return true; + } +#elif UNITY_2017_3_OR_NEWER + foreach (var assembly in UnityEditor.Compilation.CompilationPipeline.GetAssemblies()) + { + if (assembly.name.Equals(name)) return true; + } +#endif + return false; + } + + [Serializable] + class AssemblyDefinitionData + { +#pragma warning disable 649 + public string name; + public List references; + public List includePlatforms; + public List excludePlatforms; + public bool allowUnsafeCode; + public bool overrideReferences; + public List precompiledReferences; + public bool autoReferenced; + public List defineConstraints; + public List versionDefines; + public bool noEngineReferences; +#pragma warning restore 649 + } +#endif + #endregion + + } } \ No newline at end of file diff --git a/Runtime/Internal/XChartsMgr.cs b/Runtime/Internal/XChartsMgr.cs index 64265b24..5de9998d 100644 --- a/Runtime/Internal/XChartsMgr.cs +++ b/Runtime/Internal/XChartsMgr.cs @@ -2,8 +2,9 @@ using System.Collections.Generic; using System.IO; using UnityEngine; using UnityEngine.SceneManagement; +using System.Linq; #if UNITY_EDITOR -using UnityEditor; +using ADB = UnityEditor.AssetDatabase; #endif namespace XCharts.Runtime @@ -127,6 +128,7 @@ namespace XCharts.Runtime } #if UNITY_EDITOR + public static string GetPackageFullPath() { string packagePath = Path.GetFullPath("Packages/com.monitor1394.xcharts"); @@ -134,38 +136,13 @@ namespace XCharts.Runtime { return packagePath; } - packagePath = Path.GetFullPath("Assets/.."); - if (Directory.Exists(packagePath)) - { - if (File.Exists(packagePath + "/Assets/Packages/XCharts/package.json")) - { - return packagePath + "/Assets/Packages/XCharts"; - } - - if (File.Exists(packagePath + "/Assets/XCharts/package.json")) - { - return packagePath + "/Assets/XCharts"; - } - - string[] matchingPaths = Directory.GetDirectories(packagePath, "XCharts", SearchOption.AllDirectories); - string path = ValidateLocation(matchingPaths, packagePath); - if (path != null) return Path.Combine(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; + packagePath = ADB.FindAssets("t:Script") + .Where(v => Path.GetFileNameWithoutExtension(ADB.GUIDToAssetPath(v)) == "XChartsMgr") + .Select(id => ADB.GUIDToAssetPath(id)) + .FirstOrDefault(); + packagePath = Path.GetDirectoryName(packagePath); + packagePath = packagePath.Substring(0, packagePath.LastIndexOf("Runtime")); + return packagePath; } [UnityEditor.Callbacks.DidReloadScripts] @@ -185,133 +162,6 @@ namespace XCharts.Runtime } } } - - public static void EnableTextMeshPro() - { - DefineSymbolsUtil.AddGlobalDefine("dUI_TextMeshPro"); - RemoveAllChartObject(); - } - - public static void DisableTextMeshPro() - { - DefineSymbolsUtil.RemoveGlobalDefine("dUI_TextMeshPro"); - RemoveAllChartObject(); - } - - public static bool IsExistTMPAssembly() - { - -#if UNITY_2018_1_OR_NEWER - foreach (var assembly in UnityEditor.Compilation.CompilationPipeline.GetAssemblies(UnityEditor.Compilation.AssembliesType.Player)) - { - if (assembly.name.Equals("Unity.TextMeshPro")) return true; - } -#elif UNITY_2017_3_OR_NEWER - foreach (var assembly in UnityEditor.Compilation.CompilationPipeline.GetAssemblies()) - { - if (assembly.name.Equals("Unity.TextMeshPro")) return true; - } -#endif - return false; - } - - public static bool ModifyTMPRefence(bool removeTMP = false) - { - var packagePath = 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; - } - 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\""; - var refCount = 0; - foreach (var line in lines) - { - if (string.IsNullOrEmpty(line)) continue; - if (line.Contains("\"references\": [")) - { - dest.Add(line); - referencesStart = true; - } - else if (referencesStart) - { - if (line.Contains("],")) - { - referencesStart = false; - if (refCount > 0) - { - var old = dest[dest.Count - 1]; - if (old.EndsWith(",")) - dest[dest.Count - 1] = old.Substring(0, old.Length - 1); - } - 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)) - { - dest.Add(line); - refCount++; - } - else - { - removedTMP = true; - } - } - else - { - dest.Add(line); - refs.Add(line.Trim()); - } - } - } - else - { - dest.Add(line); - } - } - if (addedTMP || removedTMP) - { - File.WriteAllText(asmdefPath, string.Join("\n", dest.ToArray())); - AssetDatabase.SaveAssets(); - AssetDatabase.Refresh(); - } - return true; - } - catch (System.Exception e) - { - Debug.LogError("AddTMPRefence ERROR:" + e.Message); - return false; - } - } #endif } } \ No newline at end of file