420 lines
13 KiB

  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. #pragma warning disable 0649 // Disabled warnings related to serialized fields not assigned in this script but used in the editor.
  5. namespace TMPro
  6. {
  7. [System.Serializable]
  8. [ExecuteInEditMode]
  9. public class TMP_Settings : ScriptableObject
  10. {
  11. private static TMP_Settings s_Instance;
  12. /// <summary>
  13. /// Returns the release version of the product.
  14. /// </summary>
  15. public static string version
  16. {
  17. get { return "1.3.0"; }
  18. }
  19. /// <summary>
  20. /// Controls if Word Wrapping will be enabled on newly created text objects by default.
  21. /// </summary>
  22. public static bool enableWordWrapping
  23. {
  24. get { return instance.m_enableWordWrapping; }
  25. }
  26. [SerializeField]
  27. private bool m_enableWordWrapping;
  28. /// <summary>
  29. /// Controls if Kerning is enabled on newly created text objects by default.
  30. /// </summary>
  31. public static bool enableKerning
  32. {
  33. get { return instance.m_enableKerning; }
  34. }
  35. [SerializeField]
  36. private bool m_enableKerning;
  37. /// <summary>
  38. /// Controls if Extra Padding is enabled on newly created text objects by default.
  39. /// </summary>
  40. public static bool enableExtraPadding
  41. {
  42. get { return instance.m_enableExtraPadding; }
  43. }
  44. [SerializeField]
  45. private bool m_enableExtraPadding;
  46. /// <summary>
  47. /// Controls if TintAllSprites is enabled on newly created text objects by default.
  48. /// </summary>
  49. public static bool enableTintAllSprites
  50. {
  51. get { return instance.m_enableTintAllSprites; }
  52. }
  53. [SerializeField]
  54. private bool m_enableTintAllSprites;
  55. /// <summary>
  56. /// Controls if Escape Characters will be parsed in the Text Input Box on newly created text objects.
  57. /// </summary>
  58. public static bool enableParseEscapeCharacters
  59. {
  60. get { return instance.m_enableParseEscapeCharacters; }
  61. }
  62. [SerializeField]
  63. private bool m_enableParseEscapeCharacters;
  64. /// <summary>
  65. /// The character the will be used as a replacement for missing glyphs in a font asset.
  66. /// </summary>
  67. public static int missingGlyphCharacter
  68. {
  69. get { return instance.m_missingGlyphCharacter; }
  70. }
  71. [SerializeField]
  72. private int m_missingGlyphCharacter;
  73. /// <summary>
  74. /// Controls the display of warning message in the console.
  75. /// </summary>
  76. public static bool warningsDisabled
  77. {
  78. get { return instance.m_warningsDisabled; }
  79. }
  80. [SerializeField]
  81. private bool m_warningsDisabled;
  82. /// <summary>
  83. /// Returns the Default Font Asset to be used by newly created text objects.
  84. /// </summary>
  85. public static TMP_FontAsset defaultFontAsset
  86. {
  87. get { return instance.m_defaultFontAsset; }
  88. }
  89. [SerializeField]
  90. private TMP_FontAsset m_defaultFontAsset;
  91. /// <summary>
  92. /// The relative path to a Resources folder in the project.
  93. /// </summary>
  94. public static string defaultFontAssetPath
  95. {
  96. get { return instance.m_defaultFontAssetPath; }
  97. }
  98. [SerializeField]
  99. private string m_defaultFontAssetPath;
  100. /// <summary>
  101. /// The Default Point Size of newly created text objects.
  102. /// </summary>
  103. public static float defaultFontSize
  104. {
  105. get { return instance.m_defaultFontSize; }
  106. }
  107. [SerializeField]
  108. private float m_defaultFontSize;
  109. /// <summary>
  110. /// The multiplier used to computer the default Min point size when Text Auto Sizing is used.
  111. /// </summary>
  112. public static float defaultTextAutoSizingMinRatio
  113. {
  114. get { return instance.m_defaultAutoSizeMinRatio; }
  115. }
  116. [SerializeField]
  117. private float m_defaultAutoSizeMinRatio;
  118. /// <summary>
  119. /// The multiplier used to computer the default Max point size when Text Auto Sizing is used.
  120. /// </summary>
  121. public static float defaultTextAutoSizingMaxRatio
  122. {
  123. get { return instance.m_defaultAutoSizeMaxRatio; }
  124. }
  125. [SerializeField]
  126. private float m_defaultAutoSizeMaxRatio;
  127. /// <summary>
  128. /// The Default Size of the Text Container of a TextMeshPro object.
  129. /// </summary>
  130. public static Vector2 defaultTextMeshProTextContainerSize
  131. {
  132. get { return instance.m_defaultTextMeshProTextContainerSize; }
  133. }
  134. [SerializeField]
  135. private Vector2 m_defaultTextMeshProTextContainerSize;
  136. /// <summary>
  137. /// The Default Width of the Text Container of a TextMeshProUI object.
  138. /// </summary>
  139. public static Vector2 defaultTextMeshProUITextContainerSize
  140. {
  141. get { return instance.m_defaultTextMeshProUITextContainerSize; }
  142. }
  143. [SerializeField]
  144. private Vector2 m_defaultTextMeshProUITextContainerSize;
  145. /// <summary>
  146. /// Set the size of the text container of newly created text objects to match the size of the text.
  147. /// </summary>
  148. public static bool autoSizeTextContainer
  149. {
  150. get { return instance.m_autoSizeTextContainer; }
  151. }
  152. [SerializeField]
  153. private bool m_autoSizeTextContainer;
  154. /// <summary>
  155. /// Returns the list of Fallback Fonts defined in the TMP Settings file.
  156. /// </summary>
  157. public static List<TMP_FontAsset> fallbackFontAssets
  158. {
  159. get { return instance.m_fallbackFontAssets; }
  160. }
  161. [SerializeField]
  162. private List<TMP_FontAsset> m_fallbackFontAssets;
  163. /// <summary>
  164. /// Controls whether or not TMP will create a matching material preset or use the default material of the fallback font asset.
  165. /// </summary>
  166. public static bool matchMaterialPreset
  167. {
  168. get { return instance.m_matchMaterialPreset; }
  169. }
  170. [SerializeField]
  171. private bool m_matchMaterialPreset;
  172. /// <summary>
  173. /// The Default Sprite Asset to be used by default.
  174. /// </summary>
  175. public static TMP_SpriteAsset defaultSpriteAsset
  176. {
  177. get { return instance.m_defaultSpriteAsset; }
  178. }
  179. [SerializeField]
  180. private TMP_SpriteAsset m_defaultSpriteAsset;
  181. /// <summary>
  182. /// The relative path to a Resources folder in the project.
  183. /// </summary>
  184. public static string defaultSpriteAssetPath
  185. {
  186. get { return instance.m_defaultSpriteAssetPath; }
  187. }
  188. [SerializeField]
  189. private string m_defaultSpriteAssetPath;
  190. /// <summary>
  191. /// The relative path to a Resources folder in the project that contains Color Gradient Presets.
  192. /// </summary>
  193. public static string defaultColorGradientPresetsPath
  194. {
  195. get { return instance.m_defaultColorGradientPresetsPath; }
  196. }
  197. [SerializeField]
  198. private string m_defaultColorGradientPresetsPath;
  199. /// <summary>
  200. /// Determines if Emoji support is enabled in the Input Field TouchScreenKeyboard.
  201. /// </summary>
  202. public static bool enableEmojiSupport
  203. {
  204. get { return instance.m_enableEmojiSupport; }
  205. set { instance.m_enableEmojiSupport = value; }
  206. }
  207. [SerializeField]
  208. private bool m_enableEmojiSupport;
  209. /// <summary>
  210. /// The Default Style Sheet used by the text objects.
  211. /// </summary>
  212. public static TMP_StyleSheet defaultStyleSheet
  213. {
  214. get { return instance.m_defaultStyleSheet; }
  215. }
  216. [SerializeField]
  217. private TMP_StyleSheet m_defaultStyleSheet;
  218. /// <summary>
  219. /// Text file that contains the leading characters used for line breaking for Asian languages.
  220. /// </summary>
  221. public static TextAsset leadingCharacters
  222. {
  223. get { return instance.m_leadingCharacters; }
  224. }
  225. [SerializeField]
  226. private TextAsset m_leadingCharacters;
  227. /// <summary>
  228. /// Text file that contains the following characters used for line breaking for Asian languages.
  229. /// </summary>
  230. public static TextAsset followingCharacters
  231. {
  232. get { return instance.m_followingCharacters; }
  233. }
  234. [SerializeField]
  235. private TextAsset m_followingCharacters;
  236. /// <summary>
  237. ///
  238. /// </summary>
  239. public static LineBreakingTable linebreakingRules
  240. {
  241. get
  242. {
  243. if (instance.m_linebreakingRules == null)
  244. LoadLinebreakingRules();
  245. return instance.m_linebreakingRules;
  246. }
  247. }
  248. [SerializeField]
  249. private LineBreakingTable m_linebreakingRules;
  250. /// <summary>
  251. /// Get a singleton instance of the settings class.
  252. /// </summary>
  253. public static TMP_Settings instance
  254. {
  255. get
  256. {
  257. if (TMP_Settings.s_Instance == null)
  258. {
  259. TMP_Settings.s_Instance = Resources.Load<TMP_Settings>("TMP Settings");
  260. #if UNITY_EDITOR
  261. // Make sure TextMesh Pro UPM packages resources have been added to the user project
  262. if (TMP_Settings.s_Instance == null)
  263. {
  264. // Open TMP Resources Importer
  265. PackageResourceImporterWindow.ShowPackageImporterWindow();
  266. }
  267. #endif
  268. }
  269. return TMP_Settings.s_Instance;
  270. }
  271. }
  272. /// <summary>
  273. /// Static Function to load the TMP Settings file.
  274. /// </summary>
  275. /// <returns></returns>
  276. public static TMP_Settings LoadDefaultSettings()
  277. {
  278. if (s_Instance == null)
  279. {
  280. // Load settings from TMP_Settings file
  281. TMP_Settings settings = Resources.Load<TMP_Settings>("TMP Settings");
  282. if (settings != null)
  283. s_Instance = settings;
  284. }
  285. return s_Instance;
  286. }
  287. /// <summary>
  288. /// Returns the Sprite Asset defined in the TMP Settings file.
  289. /// </summary>
  290. /// <returns></returns>
  291. public static TMP_Settings GetSettings()
  292. {
  293. if (TMP_Settings.instance == null) return null;
  294. return TMP_Settings.instance;
  295. }
  296. /// <summary>
  297. /// Returns the Font Asset defined in the TMP Settings file.
  298. /// </summary>
  299. /// <returns></returns>
  300. public static TMP_FontAsset GetFontAsset()
  301. {
  302. if (TMP_Settings.instance == null) return null;
  303. return TMP_Settings.instance.m_defaultFontAsset;
  304. }
  305. /// <summary>
  306. /// Returns the Sprite Asset defined in the TMP Settings file.
  307. /// </summary>
  308. /// <returns></returns>
  309. public static TMP_SpriteAsset GetSpriteAsset()
  310. {
  311. if (TMP_Settings.instance == null) return null;
  312. return TMP_Settings.instance.m_defaultSpriteAsset;
  313. }
  314. /// <summary>
  315. /// Returns the Sprite Asset defined in the TMP Settings file.
  316. /// </summary>
  317. /// <returns></returns>
  318. public static TMP_StyleSheet GetStyleSheet()
  319. {
  320. if (TMP_Settings.instance == null) return null;
  321. return TMP_Settings.instance.m_defaultStyleSheet;
  322. }
  323. public static void LoadLinebreakingRules()
  324. {
  325. //Debug.Log("Loading Line Breaking Rules for Asian Languages.");
  326. if (TMP_Settings.instance == null) return;
  327. if (s_Instance.m_linebreakingRules == null)
  328. s_Instance.m_linebreakingRules = new LineBreakingTable();
  329. s_Instance.m_linebreakingRules.leadingCharacters = GetCharacters(s_Instance.m_leadingCharacters);
  330. s_Instance.m_linebreakingRules.followingCharacters = GetCharacters(s_Instance.m_followingCharacters);
  331. }
  332. /// <summary>
  333. /// Get the characters from the line breaking files
  334. /// </summary>
  335. /// <param name="file"></param>
  336. /// <returns></returns>
  337. private static Dictionary<int, char> GetCharacters(TextAsset file)
  338. {
  339. Dictionary<int, char> dict = new Dictionary<int, char>();
  340. string text = file.text;
  341. for (int i = 0; i < text.Length; i++)
  342. {
  343. char c = text[i];
  344. // Check to make sure we don't include duplicates
  345. if (dict.ContainsKey((int)c) == false)
  346. {
  347. dict.Add((int)c, c);
  348. //Debug.Log("Adding [" + (int)c + "] to dictionary.");
  349. }
  350. //else
  351. // Debug.Log("Character [" + text[i] + "] is a duplicate.");
  352. }
  353. return dict;
  354. }
  355. public class LineBreakingTable
  356. {
  357. public Dictionary<int, char> leadingCharacters;
  358. public Dictionary<int, char> followingCharacters;
  359. }
  360. }
  361. }