TMP_Settings.cs 14 KB

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