TMP_Settings.cs 17 KB

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