优化主题Theme的导入和字体自动刷新 (#148)

This commit is contained in:
monitor1394
2021-06-12 14:32:23 +08:00
parent c9f6e9ef66
commit 998a066c59
15 changed files with 272 additions and 69 deletions

View File

@@ -3,21 +3,23 @@
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using System.Reflection;
#if UNITY_EDITOR
using UnityEditor;
#endif
#if dUI_TextMeshPro
using TMPro;
#endif
namespace XCharts
{
public static class XThemeMgr
{
/// <summary>
/// 重新加载主题列表
/// </summary>
public static void ReloadThemeList()
{
//Debug.Log("LoadThemesFromResources");
XChartsMgr.Instance.m_ThemeDict.Clear();
XChartsMgr.Instance.m_ThemeNames.Clear();
AddTheme(ChartTheme.Default);
@@ -28,10 +30,69 @@ namespace XCharts
if (json != null && !string.IsNullOrEmpty(json.text))
{
var theme = JsonUtility.FromJson<ChartTheme>(json.text);
theme.font = GetCustomThemeFont(theme);
#if dUI_TextMeshPro
theme.tmpFont = GetCustomThemeTMPFont(theme);
#endif
AddTheme(theme);
}
}
//Debug.Log("LoadThemesFromResources DONE: theme count=" + m_ThemeDict.Keys.Count);
}
private static Font GetCustomThemeFont(ChartTheme theme)
{
Font font = null;
#if UNITY_EDITOR
if (string.IsNullOrEmpty(theme.fontName)) return null;
if (theme.fontName.Equals("Arial")) return Resources.GetBuiltinResource<Font>("Arial.ttf");
var guids = AssetDatabase.FindAssets("t:Font");
foreach (var guid in guids)
{
var assetPath = AssetDatabase.GUIDToAssetPath(guid);
var tempFont = AssetDatabase.LoadAssetAtPath<Font>(assetPath);
if (tempFont.name.Equals(theme.fontName))
{
font = tempFont;
break;
}
}
#else
font = FindObjectByInstanceId(theme.fontInstanceId) as Font;
#endif
return font;
}
#if dUI_TextMeshPro
private static TMP_FontAsset GetCustomThemeTMPFont(ChartTheme theme)
{
TMP_FontAsset font = null;
#if UNITY_EDITOR
if (!string.IsNullOrEmpty(theme.tmpFontName)){
//TODO: how to find TMP_FontAsset asset
var guids = AssetDatabase.FindAssets("t:Texture");
foreach (var guid in guids)
{
var assetPath = AssetDatabase.GUIDToAssetPath(guid);
if(!assetPath.EndsWith(".asset"))continue;
var tempFont = AssetDatabase.LoadAssetAtPath<TMP_FontAsset>(assetPath);
if (tempFont && tempFont.name.Equals(theme.tmpFontName))
{
font = tempFont;
break;
}
}
}
#else
font = FindObjectByInstanceId(theme.fontInstanceId) as TMP_FontAsset;
#endif
return font;
}
#endif
public static Object FindObjectByInstanceId(int instanceId)
{
return (Object)typeof(Object).GetMethod("FindObjectFromInstanceID",
BindingFlags.NonPublic | BindingFlags.Static).Invoke(null, new object[] { instanceId });
}
public static void AddTheme(ChartTheme theme)
@@ -42,10 +103,6 @@ namespace XCharts
XChartsMgr.Instance.m_ThemeDict.Add(theme.themeName, theme);
XChartsMgr.Instance.m_ThemeNames.Add(theme.themeName);
}
else
{
Debug.LogError("Theme name is exist:" + theme.themeName);
}
}
public static ChartTheme GetTheme(string themeName)
@@ -62,6 +119,16 @@ namespace XCharts
return XChartsMgr.Instance.m_ThemeNames;
}
public static List<ChartTheme> GetThemeList()
{
var list = new List<ChartTheme>();
foreach (var theme in XChartsMgr.Instance.m_ThemeDict.Values)
{
list.Add(theme);
}
return list;
}
public static bool ContainsTheme(string themeName)
{
return XChartsMgr.Instance.m_ThemeNames.Contains(themeName);
@@ -69,7 +136,6 @@ namespace XCharts
public static void SwitchTheme(BaseChart chart, string themeName)
{
Debug.Log("SwitchTheme:" + themeName);
#if UNITY_EDITOR
if (XChartsMgr.Instance.m_ThemeDict.Count == 0)
{
@@ -82,8 +148,7 @@ namespace XCharts
return;
}
var target = XChartsMgr.Instance.m_ThemeDict[themeName];
chart.theme.CopyTheme(target);
chart.RefreshAllComponent();
chart.UpdateTheme(target);
}
public static bool ExportTheme(ChartTheme theme, string themeNewName)
@@ -94,22 +159,9 @@ namespace XCharts
newtheme.theme = Theme.Custom;
newtheme.themeName = themeNewName;
if (!Directory.Exists(Application.dataPath + "/XCharts"))
{
Directory.CreateDirectory(Application.dataPath + "/XCharts");
}
if (!Directory.Exists(Application.dataPath + "/XCharts/Resources"))
{
Directory.CreateDirectory(Application.dataPath + "/XCharts/Resources");
}
var themeFileName = "XTheme-" + newtheme.themeName;
var assetPath = string.Format("Assets/XCharts/Resources/{0}", themeFileName);
var filePath = string.Format("{0}/../{1}.json", Application.dataPath, assetPath);
var json = JsonUtility.ToJson(newtheme, true);
File.WriteAllText(filePath, json);
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
var obj = Resources.Load<TextAsset>(themeFileName);
ExportTheme(newtheme);
var themeAssetName = XChartsSettings.THEME_ASSET_NAME_PREFIX + theme.themeName;
var obj = Resources.Load<TextAsset>(themeAssetName);
XChartsSettings.AddJsonTheme(obj);
ReloadThemeList();
return true;
@@ -118,9 +170,48 @@ namespace XCharts
#endif
}
public static bool ExportTheme(ChartTheme theme)
{
#if UNITY_EDITOR
theme.SyncFontName();
var themeAssetName = XChartsSettings.THEME_ASSET_NAME_PREFIX + theme.themeName;
var themeAssetPath = Application.dataPath + "/../" + XChartsSettings.THEME_ASSET_FOLDER;
if (!Directory.Exists(themeAssetPath))
{
Directory.CreateDirectory(themeAssetPath);
}
var assetPath = string.Format("{0}/{1}", XChartsSettings.THEME_ASSET_FOLDER, themeAssetName);
var themeAssetFilePath = string.Format("{0}/{1}.json", themeAssetPath, themeAssetName);
var json = JsonUtility.ToJson(theme, true);
File.WriteAllText(themeAssetFilePath, json);
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
return true;
#else
return false;
#endif
}
public static void ExportAllCustomTheme()
{
var list = new List<ChartTheme>();
foreach (var theme in XChartsMgr.Instance.m_ThemeDict.Values)
{
if (theme.theme == Theme.Custom)
{
list.Add(theme);
}
}
foreach (var theme in list)
{
ExportTheme(theme);
}
}
public static string GetThemeAssetPath(string themeName)
{
return string.Format("Assets/XCharts/Resources/XTheme-{0}.json", themeName);
return string.Format("{0}/{1}{2}.json", XChartsSettings.THEME_ASSET_FOLDER,
XChartsSettings.THEME_ASSET_NAME_PREFIX, themeName);
}
}
}