No Description
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.

TMP_Settings.cs 20KB

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