mirror of
https://github.com/XCharts-Team/XCharts.git
synced 2026-05-29 12:48:47 +00:00
优化TMP的Enable和Disable
This commit is contained in:
@@ -155,12 +155,12 @@ namespace XCharts
|
|||||||
[MenuItem("XCharts/TextMeshPro Enable")]
|
[MenuItem("XCharts/TextMeshPro Enable")]
|
||||||
public static void EnableTextMeshPro()
|
public static void EnableTextMeshPro()
|
||||||
{
|
{
|
||||||
if (!IsExistTMPAssembly())
|
if (!XChartsMgr.IsExistTMPAssembly())
|
||||||
{
|
{
|
||||||
Debug.LogError("TextMeshPro is not in the project, please import TextMeshPro package first.");
|
Debug.LogError("TextMeshPro is not in the project, please import TextMeshPro package first.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
AddTMPRefence();
|
XChartsMgr.ModifyTMPRefence();
|
||||||
XChartsMgr.EnableTextMeshPro();
|
XChartsMgr.EnableTextMeshPro();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -169,84 +169,5 @@ namespace XCharts
|
|||||||
{
|
{
|
||||||
XChartsMgr.DisableTextMeshPro();
|
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<string>();
|
|
||||||
var refs = new List<string>();
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -17,6 +17,7 @@ using UnityEngine.SceneManagement;
|
|||||||
using System.IO;
|
using System.IO;
|
||||||
#if UNITY_EDITOR
|
#if UNITY_EDITOR
|
||||||
using UnityEditor;
|
using UnityEditor;
|
||||||
|
using UnityEditor.Compilation;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
namespace XCharts
|
namespace XCharts
|
||||||
@@ -471,6 +472,106 @@ namespace XCharts
|
|||||||
DefineSymbolsUtil.RemoveGlobalDefine("dUI_TextMeshPro");
|
DefineSymbolsUtil.RemoveGlobalDefine("dUI_TextMeshPro");
|
||||||
RemoveAllChartObject();
|
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<string>();
|
||||||
|
var refs = new List<string>();
|
||||||
|
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
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user