Geen omschrijving
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

XCSettings.cs 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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][Range(1, 20)] protected float m_PieTooltipExtraRadius = 8f;
  58. [SerializeField][Range(1, 20)] protected float m_PieSelectedOffset = 8f;
  59. [SerializeField] protected List<Theme> m_CustomThemes = new List<Theme>();
  60. public static Lang lang { get { return Instance.m_Lang; } }
  61. public static Font font { get { return Instance.m_Font; } }
  62. #if dUI_TextMeshPro
  63. public static TMP_FontAsset tmpFont { get { return Instance.m_TMPFont; } }
  64. #endif
  65. /// <summary>
  66. /// 一级字体大小。
  67. /// </summary>
  68. public static int fontSizeLv1 { get { return Instance.m_FontSizeLv1; } }
  69. public static int fontSizeLv2 { get { return Instance.m_FontSizeLv2; } }
  70. public static int fontSizeLv3 { get { return Instance.m_FontSizeLv3; } }
  71. public static int fontSizeLv4 { get { return Instance.m_FontSizeLv4; } }
  72. public static LineStyle.Type axisLineType { get { return Instance.m_AxisLineType; } }
  73. public static float axisLineWidth { get { return Instance.m_AxisLineWidth; } }
  74. public static LineStyle.Type axisSplitLineType { get { return Instance.m_AxisSplitLineType; } }
  75. public static float axisSplitLineWidth { get { return Instance.m_AxisSplitLineWidth; } }
  76. public static float axisTickWidth { get { return Instance.m_AxisTickWidth; } }
  77. public static float axisTickLength { get { return Instance.m_AxisTickLength; } }
  78. public static float gaugeAxisLineWidth { get { return Instance.m_GaugeAxisLineWidth; } }
  79. public static float gaugeAxisSplitLineWidth { get { return Instance.m_GaugeAxisSplitLineWidth; } }
  80. public static float gaugeAxisSplitLineLength { get { return Instance.m_GaugeAxisSplitLineLength; } }
  81. public static float gaugeAxisTickWidth { get { return Instance.m_GaugeAxisTickWidth; } }
  82. public static float gaugeAxisTickLength { get { return Instance.m_GaugeAxisTickLength; } }
  83. public static float tootipLineWidth { get { return Instance.m_TootipLineWidth; } }
  84. public static float dataZoomBorderWidth { get { return Instance.m_DataZoomBorderWidth; } }
  85. public static float dataZoomDataLineWidth { get { return Instance.m_DataZoomDataLineWidth; } }
  86. public static float visualMapBorderWidth { get { return Instance.m_VisualMapBorderWidth; } }
  87. #region serie
  88. public static float serieLineWidth { get { return Instance.m_SerieLineWidth; } }
  89. public static float serieLineSymbolSize { get { return Instance.m_SerieLineSymbolSize; } }
  90. public static float serieScatterSymbolSize { get { return Instance.m_SerieScatterSymbolSize; } }
  91. public static float serieSelectedRate { get { return Instance.m_SerieSelectedRate; } }
  92. public static float serieCandlestickBorderWidth { get { return Instance.m_SerieCandlestickBorderWidth; } }
  93. #endregion
  94. #region editor
  95. public static bool editorShowAllListData { get { return Instance.m_EditorShowAllListData; } }
  96. #endregion
  97. #region graphic
  98. public static int maxPainter { get { return Instance.m_MaxPainter; } }
  99. public static float lineSmoothStyle { get { return Instance.m_LineSmoothStyle; } }
  100. public static float lineSmoothness { get { return Instance.m_LineSmoothness; } }
  101. public static float lineSegmentDistance { get { return Instance.m_LineSegmentDistance; } }
  102. public static float cicleSmoothness { get { return Instance.m_CicleSmoothness; } }
  103. public static float visualMapTriangeLen { get { return Instance.m_VisualMapTriangeLen; } }
  104. public static float pieTooltipExtraRadius { get { return Instance.m_PieTooltipExtraRadius; } }
  105. public static float pieSelectedOffset { get { return Instance.m_PieSelectedOffset; } }
  106. #endregion
  107. public static List<Theme> customThemes { get { return Instance.m_CustomThemes; } }
  108. private static XCSettings s_Instance;
  109. public static XCSettings Instance
  110. {
  111. get
  112. {
  113. if (s_Instance == null)
  114. {
  115. s_Instance = Resources.Load<XCSettings>("XCSettings");
  116. #if UNITY_EDITOR
  117. if (s_Instance == null)
  118. {
  119. var assetPath = GetSettingAssetPath();
  120. if (string.IsNullOrEmpty(assetPath))
  121. XCResourceImporterWindow.ShowPackageImporterWindow();
  122. else
  123. s_Instance = AssetDatabase.LoadAssetAtPath<XCSettings>(assetPath);
  124. }
  125. else
  126. {
  127. if (s_Instance.m_Lang == null)
  128. s_Instance.m_Lang = Resources.Load<Lang>("XCLang-EN");
  129. if (s_Instance.m_Lang == null)
  130. s_Instance.m_Lang = ScriptableObject.CreateInstance<Lang>();
  131. if (s_Instance.m_Font == null)
  132. s_Instance.m_Font = Resources.GetBuiltinResource<Font>("Arial.ttf");
  133. #if dUI_TextMeshPro
  134. if (s_Instance.m_TMPFont == null)
  135. s_Instance.m_TMPFont = Resources.Load<TMP_FontAsset>("LiberationSans SDF");
  136. #endif
  137. }
  138. #endif
  139. }
  140. return s_Instance;
  141. }
  142. }
  143. #if UNITY_EDITOR
  144. public static bool ExistAssetFile()
  145. {
  146. return System.IO.File.Exists("Assets/XCharts/Resources/XCSettings.asset");
  147. }
  148. public static string GetSettingAssetPath()
  149. {
  150. var path = "Assets/XCharts/Resources/XCSettings.asset";
  151. if (File.Exists(path)) return path;
  152. var dir = Application.dataPath;
  153. string[] matchingPaths = Directory.GetDirectories(dir);
  154. foreach (var match in matchingPaths)
  155. {
  156. if (match.Contains("XCharts"))
  157. {
  158. var jsonPath = string.Format("{0}/package.json", match);
  159. if (File.Exists(jsonPath))
  160. {
  161. var jsonText = File.ReadAllText(jsonPath);
  162. if (jsonText.Contains("\"displayName\": \"XCharts\""))
  163. {
  164. path = string.Format("{0}/Resources/XCSettings.asset", match.Replace('\\', '/'));
  165. if (File.Exists(path))
  166. return path.Substring(path.IndexOf("/Assets/") + 1);
  167. }
  168. }
  169. }
  170. }
  171. return null;
  172. }
  173. #endif
  174. public static bool AddCustomTheme(Theme theme)
  175. {
  176. if (theme == null) return false;
  177. if (Instance == null || Instance.m_CustomThemes == null) return false;
  178. if (!Instance.m_CustomThemes.Contains(theme))
  179. {
  180. Instance.m_CustomThemes.Add(theme);
  181. #if UNITY_EDITOR
  182. EditorUtility.SetDirty(Instance);
  183. AssetDatabase.SaveAssets();
  184. AssetDatabase.Refresh();
  185. #endif
  186. return true;
  187. }
  188. return false;
  189. }
  190. }
  191. }