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

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

View File

@@ -35,10 +35,11 @@
## master
* (2021.06.12) Optimized import of `Theme` and automatic font refresh (#148)
* (2021.06.10) Fixed compatibility issues with `Unity` version (#154)
* (2021.06.05) Improved Candlestickchart support for inverse (#152)
* (2021.06.04) Optimized auto-refresh of custom `Theme` (#148)
* (2021.06.04) Fixed `Gauge` having an abnormal pointer position when the minimum value is negative
* (2021.06.04) Fixed `Gauge` having an abnormal pointer position when the minimum value is negative (#153)
## v2.2.0

View File

@@ -35,10 +35,11 @@
## master
* (2021.06.12) 优化主题`Theme`的导入和字体自动刷新 (#148)
* (2021.06.10) 修复`Unity`版本兼容问题 (#154)
* (2021.06.05) 完善`CandlestickChart``inverse`的支持 (#152)
* (2021.06.04) 优化自定义主题`Theme`的自动刷新 (#148)
* (2021.06.04) 修复`Gauge`在最小值为负数时指针指示位置异常的问题
* (2021.06.04) 修复`Gauge`在最小值为负数时指针指示位置异常的问题 (#153)
## v2.2.0

View File

@@ -31,10 +31,6 @@ namespace XCharts
btnRect.y = m_DrawRect.y - EditorGUIUtility.singleLineHeight - 3;
btnRect.width = btnWidth;
var chart = prop.serializedObject.targetObject as BaseChart;
var lastFont = chart.theme.font;
#if dUI_TextMeshPro
var lastTMPFont = chart.theme.tmpFont;
#endif
if (GUI.Button(btnRect, new GUIContent("Reset", "Reset to theme default color")))
{
chart.theme.ResetTheme();
@@ -60,8 +56,18 @@ namespace XCharts
}
#if dUI_TextMeshPro
PropertyField(prop, "m_TMPFont");
if(chart.theme.tmpFont == null && !string.IsNullOrEmpty(chart.theme.tmpFontName))
{
var msg = string.Format("Can't find theme font asset:{0} in project.", chart.theme.tmpFontName);
EditorGUILayout.HelpBox(msg, MessageType.Error);
}
#else
PropertyField(prop, "m_Font");
if (chart.theme.font == null && !string.IsNullOrEmpty(chart.theme.fontName))
{
var msg = string.Format("Can't find theme font asset:{0} in project.", chart.theme.fontName);
EditorGUILayout.HelpBox(msg, MessageType.Error);
}
#endif
PropertyField(prop, "m_ContrastColor");
PropertyField(prop, "m_BackgroundColor");

View File

@@ -1,4 +1,11 @@
using UnityEngine;
/************************************************/
/* */
/* Copyright (c) 2018 - 2021 monitor1394 */
/* https://github.com/monitor1394 */
/* */
/************************************************/
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;

View File

@@ -1,3 +1,10 @@
/************************************************/
/* */
/* Copyright (c) 2018 - 2021 monitor1394 */
/* https://github.com/monitor1394 */
/* */
/************************************************/
using System.IO;
using UnityEditor;
using UnityEngine;
@@ -63,7 +70,8 @@ namespace XCharts
private static bool IsThemeAsset(string assetPath)
{
if (!assetPath.EndsWith(".json")) return false;
if (!assetPath.StartsWith("XTheme")) return false;
var assetName = Path.GetFileNameWithoutExtension(assetPath);
if (!assetName.StartsWith(XChartsSettings.THEME_ASSET_NAME_PREFIX)) return false;
return true;
}
}

View File

@@ -0,0 +1,28 @@
/************************************************/
/* */
/* Copyright (c) 2018 - 2021 monitor1394 */
/* https://github.com/monitor1394 */
/* */
/************************************************/
using System.IO;
using UnityEditor;
using UnityEditor.Build;
using UnityEngine;
namespace XCharts
{
public class XChartsBuild : IPreprocessBuild, IPostprocessBuild
{
public int callbackOrder => 1;
public void OnPostprocessBuild(BuildTarget target, string path)
{
}
public void OnPreprocessBuild(BuildTarget target, string path)
{
XThemeMgr.ExportAllCustomTheme();
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: df212e63c528e40fa9e8fffcd868ab7d
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -637,6 +637,7 @@ namespace XCharts
public void UpdateTheme(ChartTheme theme)
{
m_Theme.CopyTheme(theme);
SetAllComponentDirty();
}
/// <summary>

View File

@@ -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;

View File

@@ -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

View File

@@ -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();

View File

@@ -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)
{

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);
}
}
}

View File

@@ -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()
{

View File

@@ -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;