mirror of
https://github.com/XCharts-Team/XCharts.git
synced 2026-05-23 09:20:08 +00:00
优化主题Theme的导入和字体自动刷新 (#148)
This commit is contained in:
@@ -637,6 +637,7 @@ namespace XCharts
|
||||
public void UpdateTheme(ChartTheme theme)
|
||||
{
|
||||
m_Theme.CopyTheme(theme);
|
||||
SetAllComponentDirty();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -47,6 +47,10 @@ namespace XCharts
|
||||
{
|
||||
[SerializeField] private Theme m_Theme = Theme.Default;
|
||||
[SerializeField] private string m_ThemeName = Theme.Default.ToString();
|
||||
[SerializeField] private string m_FontName;
|
||||
[SerializeField] private string m_TMPFontName;
|
||||
[SerializeField] private int m_FontInstacneId;
|
||||
[SerializeField] private int m_TMPFontInstanceId;
|
||||
[SerializeField] private Font m_Font;
|
||||
#if dUI_TextMeshPro
|
||||
[SerializeField] private TMP_FontAsset m_TMPFont;
|
||||
@@ -75,6 +79,12 @@ namespace XCharts
|
||||
[SerializeField] private VisualMapTheme m_VisualMap;
|
||||
[SerializeField] private SerieTheme m_Serie;
|
||||
|
||||
private ChartTheme()
|
||||
{
|
||||
m_FontName = "Arial";
|
||||
m_TMPFontName = "LiberationSans SDF";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// the theme of chart.
|
||||
/// 主题类型。
|
||||
@@ -110,6 +120,27 @@ namespace XCharts
|
||||
set { if (PropertyUtil.SetColor(ref m_BackgroundColor, value)) SetVerticesDirty(); }
|
||||
}
|
||||
|
||||
public string fontName
|
||||
{
|
||||
get { return m_FontName; }
|
||||
set { if (PropertyUtil.SetClass(ref m_FontName, value)) SetComponentDirty(); }
|
||||
}
|
||||
public int fontInstanceId
|
||||
{
|
||||
get { return m_FontInstacneId; }
|
||||
set { if (PropertyUtil.SetStruct(ref m_FontInstacneId, value)) SetComponentDirty(); }
|
||||
}
|
||||
public string tmpFontName
|
||||
{
|
||||
get { return m_TMPFontName; }
|
||||
set { if (PropertyUtil.SetClass(ref m_TMPFontName, value)) SetComponentDirty(); }
|
||||
}
|
||||
public int tmpFontInstanceId
|
||||
{
|
||||
get { return m_TMPFontInstanceId; }
|
||||
set { if (PropertyUtil.SetStruct(ref m_TMPFontInstanceId, value)) SetComponentDirty(); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The color list of palette. If no color is set in series, the colors would be adopted sequentially and circularly from this list as the colors of series.
|
||||
/// 调色盘颜色列表。如果系列没有设置颜色,则会依次循环从该列表中取颜色作为系列颜色。
|
||||
@@ -139,11 +170,14 @@ namespace XCharts
|
||||
get { return m_TMPFont; }
|
||||
set
|
||||
{
|
||||
if (PropertyUtil.SetClass(ref m_TMPFont, value))
|
||||
m_TMPFont = value;
|
||||
if(value)
|
||||
{
|
||||
SetComponentDirty();
|
||||
SyncTMPFontToSubComponent();
|
||||
m_TMPFontName = value.name;
|
||||
m_TMPFontInstanceId = value.GetInstanceID();
|
||||
}
|
||||
SetComponentDirty();
|
||||
SyncTMPFontToSubComponent();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -156,11 +190,14 @@ namespace XCharts
|
||||
get { return m_Font; }
|
||||
set
|
||||
{
|
||||
if (PropertyUtil.SetClass(ref m_Font, value))
|
||||
m_Font = value;
|
||||
if (value)
|
||||
{
|
||||
SetComponentDirty();
|
||||
SyncFontToSubComponent();
|
||||
m_FontName = value.name;
|
||||
m_FontInstacneId = value.GetInstanceID();
|
||||
}
|
||||
SetComponentDirty();
|
||||
SyncFontToSubComponent();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -262,9 +299,13 @@ namespace XCharts
|
||||
m_Theme = theme.theme;
|
||||
m_ThemeName = theme.themeName;
|
||||
#if dUI_TextMeshPro
|
||||
m_TMPFont = theme.tmpFont;
|
||||
tmpFont = theme.tmpFont;
|
||||
#endif
|
||||
m_Font = theme.m_Font;
|
||||
font = theme.font;
|
||||
m_FontName = theme.fontName;
|
||||
m_FontInstacneId = theme.fontInstanceId;
|
||||
m_TMPFontName = theme.tmpFontName;
|
||||
m_TMPFontInstanceId = theme.tmpFontInstanceId;
|
||||
m_ContrastColor = theme.contrastColor;
|
||||
m_BackgroundColor = theme.m_BackgroundColor;
|
||||
m_Common.Copy(theme.common);
|
||||
@@ -438,6 +479,22 @@ namespace XCharts
|
||||
}
|
||||
}
|
||||
|
||||
public void SyncFontName()
|
||||
{
|
||||
if (font)
|
||||
{
|
||||
m_FontName = font.name;
|
||||
m_FontInstacneId = font.GetInstanceID();
|
||||
}
|
||||
#if dUI_TextMeshPro
|
||||
if (tmpFont)
|
||||
{
|
||||
m_TMPFontName = tmpFont.name;
|
||||
m_TMPFontInstanceId = tmpFont.GetInstanceID();
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
public void SyncFontToSubComponent()
|
||||
{
|
||||
common.font = font;
|
||||
|
||||
@@ -25,13 +25,13 @@ namespace XCharts
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
/// the font of text。
|
||||
/// the font of text.
|
||||
/// 字体。
|
||||
/// </summary>
|
||||
public Font font
|
||||
{
|
||||
get { return m_Font; }
|
||||
set { if (PropertyUtil.SetClass(ref m_Font, value)) SetComponentDirty(); }
|
||||
set { m_Font = value; SetComponentDirty(); }
|
||||
}
|
||||
/// <summary>
|
||||
/// the color of text.
|
||||
@@ -69,7 +69,7 @@ namespace XCharts
|
||||
public TMP_FontAsset tmpFont
|
||||
{
|
||||
get { return m_TMPFont; }
|
||||
set { if (PropertyUtil.SetClass(ref m_TMPFont, value)) SetComponentDirty(); }
|
||||
set { m_TMPFont = value; SetComponentDirty(); }
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
@@ -216,10 +216,6 @@ namespace XCharts
|
||||
}
|
||||
else
|
||||
{
|
||||
if (m_Theme.font == null)
|
||||
{
|
||||
m_Theme.font = XChartsSettings.font;
|
||||
}
|
||||
if (m_Theme.colorPalette.Count == 0)
|
||||
{
|
||||
m_Theme.ResetTheme();
|
||||
|
||||
@@ -241,10 +241,6 @@ namespace XCharts
|
||||
}
|
||||
|
||||
#if dUI_TextMeshPro
|
||||
public void SetAlignment(TextAlignmentOptions alignment)
|
||||
{
|
||||
if (m_TMPText != null) m_TMPText.alignment = alignment;
|
||||
}
|
||||
|
||||
public void SetFont(TMP_FontAsset font)
|
||||
{
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -30,9 +30,6 @@ namespace XCharts
|
||||
public string homepage = "";
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
[InitializeOnLoad]
|
||||
#endif
|
||||
[ExecuteInEditMode]
|
||||
public class XChartsMgr : MonoBehaviour
|
||||
{
|
||||
@@ -67,19 +64,13 @@ namespace XCharts
|
||||
}
|
||||
}
|
||||
|
||||
private XChartsMgr()
|
||||
{
|
||||
}
|
||||
|
||||
static XChartsMgr()
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
EditorApplication.delayCall += () =>
|
||||
{
|
||||
var mgr = XChartsMgr.Instance;
|
||||
};
|
||||
#endif
|
||||
[InitializeOnLoadMethod]
|
||||
private static void OnInitializeOnLoadMethod()
|
||||
{
|
||||
XThemeMgr.ReloadThemeList();
|
||||
}
|
||||
#endif
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
|
||||
@@ -11,6 +11,9 @@ using System.Collections.Generic;
|
||||
#if dUI_TextMeshPro
|
||||
using TMPro;
|
||||
#endif
|
||||
#if UNITY_EDITOR
|
||||
using UnityEditor;
|
||||
#endif
|
||||
|
||||
namespace XCharts
|
||||
{
|
||||
@@ -21,6 +24,8 @@ namespace XCharts
|
||||
#endif
|
||||
public class XChartsSettings : ScriptableObject
|
||||
{
|
||||
public readonly static string THEME_ASSET_NAME_PREFIX = "XTheme-";
|
||||
public readonly static string THEME_ASSET_FOLDER = "Assets/XCharts/Resources";
|
||||
|
||||
[SerializeField] private Font m_Font = null;
|
||||
#if dUI_TextMeshPro
|
||||
@@ -145,7 +150,6 @@ namespace XCharts
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
return s_Instance;
|
||||
}
|
||||
}
|
||||
@@ -156,6 +160,11 @@ namespace XCharts
|
||||
if (!Instance.m_CustomThemes.Contains(theme))
|
||||
{
|
||||
Instance.m_CustomThemes.Add(theme);
|
||||
#if UNITY_EDITOR
|
||||
EditorUtility.SetDirty(Instance);
|
||||
AssetDatabase.SaveAssets();
|
||||
AssetDatabase.Refresh();
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
||||
Reference in New Issue
Block a user