将 inputsystem 支持功能添加到 xchart 菜单

顺便简化了 tmp 支持相关逻辑
This commit is contained in:
边上海
2023-01-30 19:48:59 +08:00
parent 7fbaf7af70
commit 633456ada0
2 changed files with 154 additions and 166 deletions

View File

@@ -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>();
canvas.renderMode = RenderMode.ScreenSpaceCamera;
var mainCamera = GameObject.FindGameObjectWithTag("MainCamera");
canvas.worldCamera = mainCamera == null? null : mainCamera.GetComponent<Camera>();
canvas.worldCamera = mainCamera == null ? null : mainCamera.GetComponent<Camera>();
canvasObject.AddComponent<CanvasScaler>();
canvasObject.AddComponent<GraphicRaycaster>();
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<string> references;
public List<string> includePlatforms;
public List<string> excludePlatforms;
public bool allowUnsafeCode;
public bool overrideReferences;
public List<string> precompiledReferences;
public bool autoReferenced;
public List<string> defineConstraints;
public List<string> versionDefines;
public bool noEngineReferences;
#pragma warning restore 649
}
#endif
#endregion
}
}

View File

@@ -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<string>();
var refs = new List<string>();
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
}
}