暫無描述
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.

XCThemeMgr.cs 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using UnityEngine;
  4. #if UNITY_EDITOR
  5. using UnityEditor;
  6. #endif
  7. #if dUI_TextMeshPro
  8. using TMPro;
  9. #endif
  10. namespace XCharts.Runtime
  11. {
  12. public static class XCThemeMgr
  13. {
  14. /// <summary>
  15. /// 重新加载主题列表
  16. /// </summary>
  17. public static void ReloadThemeList()
  18. {
  19. XChartsMgr.themes.Clear();
  20. XChartsMgr.themeNames.Clear();
  21. AddTheme(LoadTheme(ThemeType.Default));
  22. AddTheme(LoadTheme(ThemeType.Dark));
  23. if (XCSettings.Instance != null)
  24. {
  25. foreach (var theme in XCSettings.customThemes)
  26. {
  27. AddTheme(theme);
  28. }
  29. }
  30. }
  31. public static void CheckReloadTheme()
  32. {
  33. if (XChartsMgr.themeNames.Count < 0)
  34. ReloadThemeList();
  35. }
  36. public static void AddTheme(Theme theme)
  37. {
  38. if (theme == null) return;
  39. if (!XChartsMgr.themes.ContainsKey(theme.themeName))
  40. {
  41. XChartsMgr.themes.Add(theme.themeName, theme);
  42. XChartsMgr.themeNames.Add(theme.themeName);
  43. XChartsMgr.themeNames.Sort();
  44. }
  45. }
  46. public static Theme GetTheme(ThemeType type)
  47. {
  48. return GetTheme(type.ToString());
  49. }
  50. public static Theme GetTheme(string themeName)
  51. {
  52. if (!XChartsMgr.themes.ContainsKey(themeName))
  53. {
  54. ReloadThemeList();
  55. if (XChartsMgr.themes.ContainsKey(themeName))
  56. return XChartsMgr.themes[themeName];
  57. else
  58. return null;
  59. }
  60. else
  61. {
  62. return XChartsMgr.themes[themeName];
  63. }
  64. }
  65. public static Theme LoadTheme(ThemeType type)
  66. {
  67. return LoadTheme(type.ToString());
  68. }
  69. public static Theme LoadTheme(string themeName)
  70. {
  71. var theme = Resources.Load<Theme>(XCSettings.THEME_ASSET_NAME_PREFIX + themeName);
  72. if (theme == null)
  73. theme = Resources.Load<Theme>(themeName);
  74. return theme;
  75. }
  76. public static List<string> GetAllThemeNames()
  77. {
  78. return XChartsMgr.themeNames;
  79. }
  80. public static List<Theme> GetThemeList()
  81. {
  82. var list = new List<Theme>();
  83. foreach (var theme in XChartsMgr.themes.Values)
  84. {
  85. list.Add(theme);
  86. }
  87. return list;
  88. }
  89. public static bool ContainsTheme(string themeName)
  90. {
  91. return XChartsMgr.themeNames.Contains(themeName);
  92. }
  93. public static void SwitchTheme(BaseChart chart, string themeName)
  94. {
  95. #if UNITY_EDITOR
  96. if (XChartsMgr.themes.Count == 0)
  97. {
  98. ReloadThemeList();
  99. }
  100. #endif
  101. if (!XChartsMgr.themes.ContainsKey(themeName))
  102. {
  103. Debug.LogError("SwitchTheme ERROR: not exist theme:" + themeName);
  104. return;
  105. }
  106. var target = XChartsMgr.themes[themeName];
  107. chart.UpdateTheme(target);
  108. }
  109. public static bool ExportTheme(Theme theme, string themeNewName)
  110. {
  111. #if UNITY_EDITOR
  112. var newtheme = Theme.EmptyTheme;
  113. newtheme.CopyTheme(theme);
  114. newtheme.themeType = ThemeType.Custom;
  115. newtheme.themeName = themeNewName;
  116. ExportTheme(newtheme);
  117. return true;
  118. #else
  119. return false;
  120. #endif
  121. }
  122. public static bool ExportTheme(Theme theme)
  123. {
  124. #if UNITY_EDITOR
  125. var themeAssetName = XCSettings.THEME_ASSET_NAME_PREFIX + theme.themeName;
  126. var themeAssetPath = Application.dataPath + "/../" + XCSettings.THEME_ASSET_FOLDER;
  127. if (!Directory.Exists(themeAssetPath))
  128. {
  129. Directory.CreateDirectory(themeAssetPath);
  130. }
  131. var themeAssetFilePath = string.Format("{0}/{1}.asset", XCSettings.THEME_ASSET_FOLDER, themeAssetName);
  132. AssetDatabase.CreateAsset(theme, themeAssetFilePath);
  133. AssetDatabase.SaveAssets();
  134. AssetDatabase.Refresh();
  135. return true;
  136. #else
  137. return false;
  138. #endif
  139. }
  140. public static string GetThemeAssetPath(string themeName)
  141. {
  142. return string.Format("{0}/{1}{2}.asset", XCSettings.THEME_ASSET_FOLDER,
  143. XCSettings.THEME_ASSET_NAME_PREFIX, themeName);
  144. }
  145. }
  146. }