XCSettings.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using UnityEngine;
  5. #if dUI_TextMeshPro
  6. using TMPro;
  7. #endif
  8. #if UNITY_EDITOR
  9. using UnityEditor;
  10. #endif
  11. namespace XCharts.Runtime
  12. {
  13. [Serializable]
  14. #if UNITY_2018_3
  15. [ExcludeFromPresetAttribute]
  16. #endif
  17. public class XCSettings : ScriptableObject
  18. {
  19. public readonly static string THEME_ASSET_NAME_PREFIX = "XCTheme-";
  20. public readonly static string THEME_ASSET_FOLDER = "Assets/XCharts/Resources";
  21. [SerializeField] private Lang m_Lang = null;
  22. [SerializeField] private Font m_Font = null;
  23. #if dUI_TextMeshPro
  24. [SerializeField] private TMP_FontAsset m_TMPFont = null;
  25. #endif
  26. [SerializeField][Range(1, 200)] private int m_FontSizeLv1 = 28;
  27. [SerializeField][Range(1, 200)] private int m_FontSizeLv2 = 24;
  28. [SerializeField][Range(1, 200)] private int m_FontSizeLv3 = 20;
  29. [SerializeField][Range(1, 200)] private int m_FontSizeLv4 = 18;
  30. [SerializeField] private LineStyle.Type m_AxisLineType = LineStyle.Type.Solid;
  31. [SerializeField][Range(0, 20)] private float m_AxisLineWidth = 0.8f;
  32. [SerializeField] private LineStyle.Type m_AxisSplitLineType = LineStyle.Type.Solid;
  33. [SerializeField][Range(0, 20)] private float m_AxisSplitLineWidth = 0.8f;
  34. [SerializeField][Range(0, 20)] private float m_AxisTickWidth = 0.8f;
  35. [SerializeField][Range(0, 20)] private float m_AxisTickLength = 5f;
  36. [SerializeField][Range(0, 200)] private float m_GaugeAxisLineWidth = 15f;
  37. [SerializeField][Range(0, 20)] private float m_GaugeAxisSplitLineWidth = 0.8f;
  38. [SerializeField][Range(0, 20)] private float m_GaugeAxisSplitLineLength = 15f;
  39. [SerializeField][Range(0, 20)] private float m_GaugeAxisTickWidth = 0.8f;
  40. [SerializeField][Range(0, 20)] private float m_GaugeAxisTickLength = 5f;
  41. [SerializeField][Range(0, 20)] private float m_TootipLineWidth = 0.8f;
  42. [SerializeField][Range(0, 20)] private float m_DataZoomBorderWidth = 0.5f;
  43. [SerializeField][Range(0, 20)] private float m_DataZoomDataLineWidth = 0.5f;
  44. [SerializeField][Range(0, 20)] private float m_VisualMapBorderWidth = 0f;
  45. [SerializeField][Range(0, 20)] private float m_SerieLineWidth = 1.8f;
  46. [SerializeField][Range(0, 200)] private float m_SerieLineSymbolSize = 5f;
  47. [SerializeField][Range(0, 200)] private float m_SerieScatterSymbolSize = 20f;
  48. [SerializeField][Range(0, 200)] private float m_SerieSelectedRate = 1.3f;
  49. [SerializeField][Range(0, 10)] private float m_SerieCandlestickBorderWidth = 1f;
  50. [SerializeField] private bool m_EditorShowAllListData = false;
  51. [SerializeField][Range(1, 20)] protected int m_MaxPainter = 10;
  52. [SerializeField][Range(1, 10)] protected float m_LineSmoothStyle = 3f;
  53. [SerializeField][Range(1f, 20)] protected float m_LineSmoothness = 2f;
  54. [SerializeField][Range(1f, 20)] protected float m_LineSegmentDistance = 3f;
  55. [SerializeField][Range(1, 10)] protected float m_CicleSmoothness = 2f;
  56. [SerializeField][Range(10, 50)] protected float m_VisualMapTriangeLen = 20f;
  57. [SerializeField] protected List<Theme> m_CustomThemes = new List<Theme>();
  58. public static Lang lang { get { return Instance.m_Lang; } }
  59. public static Font font { get { return Instance.m_Font; } }
  60. #if dUI_TextMeshPro
  61. public static TMP_FontAsset tmpFont { get { return Instance.m_TMPFont; } }
  62. #endif
  63. /// <summary>
  64. /// 一级字体大小。
  65. /// </summary>
  66. public static int fontSizeLv1 { get { return Instance.m_FontSizeLv1; } }
  67. public static int fontSizeLv2 { get { return Instance.m_FontSizeLv2; } }
  68. public static int fontSizeLv3 { get { return Instance.m_FontSizeLv3; } }
  69. public static int fontSizeLv4 { get { return Instance.m_FontSizeLv4; } }
  70. public static LineStyle.Type axisLineType { get { return Instance.m_AxisLineType; } }
  71. public static float axisLineWidth { get { return Instance.m_AxisLineWidth; } }
  72. public static LineStyle.Type axisSplitLineType { get { return Instance.m_AxisSplitLineType; } }
  73. public static float axisSplitLineWidth { get { return Instance.m_AxisSplitLineWidth; } }
  74. public static float axisTickWidth { get { return Instance.m_AxisTickWidth; } }
  75. public static float axisTickLength { get { return Instance.m_AxisTickLength; } }
  76. public static float gaugeAxisLineWidth { get { return Instance.m_GaugeAxisLineWidth; } }
  77. public static float gaugeAxisSplitLineWidth { get { return Instance.m_GaugeAxisSplitLineWidth; } }
  78. public static float gaugeAxisSplitLineLength { get { return Instance.m_GaugeAxisSplitLineLength; } }
  79. public static float gaugeAxisTickWidth { get { return Instance.m_GaugeAxisTickWidth; } }
  80. public static float gaugeAxisTickLength { get { return Instance.m_GaugeAxisTickLength; } }
  81. public static float tootipLineWidth { get { return Instance.m_TootipLineWidth; } }
  82. public static float dataZoomBorderWidth { get { return Instance.m_DataZoomBorderWidth; } }
  83. public static float dataZoomDataLineWidth { get { return Instance.m_DataZoomDataLineWidth; } }
  84. public static float visualMapBorderWidth { get { return Instance.m_VisualMapBorderWidth; } }
  85. #region serie
  86. public static float serieLineWidth { get { return Instance.m_SerieLineWidth; } }
  87. public static float serieLineSymbolSize { get { return Instance.m_SerieLineSymbolSize; } }
  88. public static float serieScatterSymbolSize { get { return Instance.m_SerieScatterSymbolSize; } }
  89. public static float serieSelectedRate { get { return Instance.m_SerieSelectedRate; } }
  90. public static float serieCandlestickBorderWidth { get { return Instance.m_SerieCandlestickBorderWidth; } }
  91. #endregion
  92. #region editor
  93. public static bool editorShowAllListData { get { return Instance.m_EditorShowAllListData; } }
  94. #endregion
  95. #region graphic
  96. public static int maxPainter { get { return Instance.m_MaxPainter; } }
  97. public static float lineSmoothStyle { get { return Instance.m_LineSmoothStyle; } }
  98. public static float lineSmoothness { get { return Instance.m_LineSmoothness; } }
  99. public static float lineSegmentDistance { get { return Instance.m_LineSegmentDistance; } }
  100. public static float cicleSmoothness { get { return Instance.m_CicleSmoothness; } }
  101. public static float visualMapTriangeLen { get { return Instance.m_VisualMapTriangeLen; } }
  102. #endregion
  103. public static List<Theme> customThemes { get { return Instance.m_CustomThemes; } }
  104. private static XCSettings s_Instance;
  105. public static XCSettings Instance
  106. {
  107. get
  108. {
  109. if (s_Instance == null)
  110. {
  111. s_Instance = Resources.Load<XCSettings>("XCSettings");
  112. #if UNITY_EDITOR
  113. if (s_Instance == null)
  114. {
  115. var assetPath = GetSettingAssetPath();
  116. if (string.IsNullOrEmpty(assetPath))
  117. XCResourceImporterWindow.ShowPackageImporterWindow();
  118. else
  119. s_Instance = AssetDatabase.LoadAssetAtPath<XCSettings>(assetPath);
  120. }
  121. else
  122. {
  123. if (s_Instance.m_Lang == null)
  124. s_Instance.m_Lang = Resources.Load<Lang>("XCLang-EN");
  125. if (s_Instance.m_Lang == null)
  126. s_Instance.m_Lang = ScriptableObject.CreateInstance<Lang>();
  127. if (s_Instance.m_Font == null)
  128. s_Instance.m_Font = Resources.GetBuiltinResource<Font>("Arial.ttf");
  129. #if dUI_TextMeshPro
  130. if (s_Instance.m_TMPFont == null)
  131. s_Instance.m_TMPFont = Resources.Load<TMP_FontAsset>("LiberationSans SDF");
  132. #endif
  133. }
  134. #endif
  135. }
  136. return s_Instance;
  137. }
  138. }
  139. #if UNITY_EDITOR
  140. public static bool ExistAssetFile()
  141. {
  142. return System.IO.File.Exists("Assets/XCharts/Resources/XCSettings.asset");
  143. }
  144. public static string GetSettingAssetPath()
  145. {
  146. var path = "Assets/XCharts/Resources/XCSettings.asset";
  147. if (File.Exists(path)) return path;
  148. var dir = Application.dataPath;
  149. string[] matchingPaths = Directory.GetDirectories(dir);
  150. foreach (var match in matchingPaths)
  151. {
  152. if (match.Contains("XCharts"))
  153. {
  154. var jsonPath = string.Format("{0}/package.json", match);
  155. if (File.Exists(jsonPath))
  156. {
  157. var jsonText = File.ReadAllText(jsonPath);
  158. if (jsonText.Contains("\"displayName\": \"XCharts\""))
  159. {
  160. path = string.Format("{0}/Resources/XCSettings.asset", match.Replace('\\', '/'));
  161. if (File.Exists(path))
  162. return path.Substring(path.IndexOf("/Assets/") + 1);
  163. }
  164. }
  165. }
  166. }
  167. return null;
  168. }
  169. #endif
  170. public static bool AddCustomTheme(Theme theme)
  171. {
  172. if (theme == null) return false;
  173. if (Instance == null || Instance.m_CustomThemes == null) return false;
  174. if (!Instance.m_CustomThemes.Contains(theme))
  175. {
  176. Instance.m_CustomThemes.Add(theme);
  177. #if UNITY_EDITOR
  178. EditorUtility.SetDirty(Instance);
  179. AssetDatabase.SaveAssets();
  180. AssetDatabase.Refresh();
  181. #endif
  182. return true;
  183. }
  184. return false;
  185. }
  186. }
  187. }