TMP_SettingsEditor.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using UnityEditor;
  4. using UnityEditorInternal;
  5. #pragma warning disable 0414 // Disabled a few warnings for not yet implemented features.
  6. namespace TMPro.EditorUtilities
  7. {
  8. [CustomEditor(typeof(TMP_Settings))]
  9. public class TMP_SettingsEditor : Editor
  10. {
  11. internal class Styles
  12. {
  13. public static readonly GUIContent defaultFontAssetLabel = new GUIContent("Default Font Asset", "The Font Asset that will be assigned by default to newly created text objects when no Font Asset is specified.");
  14. public static readonly GUIContent defaultFontAssetPathLabel = new GUIContent("Path: Resources/", "The relative path to a Resources folder where the Font Assets and Material Presets are located.\nExample \"Fonts & Materials/\"");
  15. public static readonly GUIContent fallbackFontAssetsLabel = new GUIContent("Fallback Font Assets", "The Font Assets that will be searched to locate and replace missing characters from a given Font Asset.");
  16. public static readonly GUIContent fallbackFontAssetsListLabel = new GUIContent("Fallback Font Assets List", "The Font Assets that will be searched to locate and replace missing characters from a given Font Asset.");
  17. public static readonly GUIContent fallbackMaterialSettingsLabel = new GUIContent("Fallback Material Settings");
  18. public static readonly GUIContent matchMaterialPresetLabel = new GUIContent("Match Material Presets");
  19. public static readonly GUIContent containerDefaultSettingsLabel = new GUIContent("Text Container Default Settings");
  20. public static readonly GUIContent textMeshProLabel = new GUIContent("TextMeshPro");
  21. public static readonly GUIContent textMeshProUiLabel = new GUIContent("TextMeshPro UI");
  22. public static readonly GUIContent enableRaycastTarget = new GUIContent("Enable Raycast Target");
  23. public static readonly GUIContent autoSizeContainerLabel = new GUIContent("Auto Size Text Container", "Set the size of the text container to match the text.");
  24. public static readonly GUIContent textComponentDefaultSettingsLabel = new GUIContent("Text Component Default Settings");
  25. public static readonly GUIContent defaultFontSize = new GUIContent("Default Font Size");
  26. public static readonly GUIContent autoSizeRatioLabel = new GUIContent("Text Auto Size Ratios");
  27. public static readonly GUIContent minLabel = new GUIContent("Min");
  28. public static readonly GUIContent maxLabel = new GUIContent("Max");
  29. public static readonly GUIContent wordWrappingLabel = new GUIContent("Word Wrapping");
  30. public static readonly GUIContent kerningLabel = new GUIContent("Kerning");
  31. public static readonly GUIContent extraPaddingLabel = new GUIContent("Extra Padding");
  32. public static readonly GUIContent tintAllSpritesLabel = new GUIContent("Tint All Sprites");
  33. public static readonly GUIContent parseEscapeCharactersLabel = new GUIContent("Parse Escape Sequence");
  34. public static readonly GUIContent dynamicFontSystemSettingsLabel = new GUIContent("Dynamic Font System Settings");
  35. public static readonly GUIContent getFontFeaturesAtRuntime = new GUIContent("Get Font Features at Runtime", "Determines if Glyph Adjustment Data will be retrieved from font files at runtime when new characters and glyphs are added to font assets.");
  36. public static readonly GUIContent missingGlyphLabel = new GUIContent("Replacement Character", "The character to be displayed when the requested character is not found in any font asset or fallbacks.");
  37. public static readonly GUIContent disableWarningsLabel = new GUIContent("Disable warnings", "Disable warning messages in the Console.");
  38. public static readonly GUIContent defaultSpriteAssetLabel = new GUIContent("Default Sprite Asset", "The Sprite Asset that will be assigned by default when using the <sprite> tag when no Sprite Asset is specified.");
  39. public static readonly GUIContent enableEmojiSupportLabel = new GUIContent("iOS Emoji Support", "Enables Emoji support for Touch Screen Keyboards on target devices.");
  40. public static readonly GUIContent spriteAssetsPathLabel = new GUIContent("Path: Resources/", "The relative path to a Resources folder where the Sprite Assets are located.\nExample \"Sprite Assets/\"");
  41. public static readonly GUIContent defaultStyleSheetLabel = new GUIContent("Default Style Sheet", "The Style Sheet that will be used for all text objects in this project.");
  42. public static readonly GUIContent colorGradientPresetsLabel = new GUIContent("Color Gradient Presets", "The relative path to a Resources folder where the Color Gradient Presets are located.\nExample \"Color Gradient Presets/\"");
  43. public static readonly GUIContent colorGradientsPathLabel = new GUIContent("Path: Resources/", "The relative path to a Resources folder where the Color Gradient Presets are located.\nExample \"Color Gradient Presets/\"");
  44. public static readonly GUIContent lineBreakingLabel = new GUIContent("Line Breaking for Asian languages", "The text assets that contain the Leading and Following characters which define the rules for line breaking with Asian languages.");
  45. }
  46. SerializedProperty m_PropFontAsset;
  47. SerializedProperty m_PropDefaultFontAssetPath;
  48. SerializedProperty m_PropDefaultFontSize;
  49. SerializedProperty m_PropDefaultAutoSizeMinRatio;
  50. SerializedProperty m_PropDefaultAutoSizeMaxRatio;
  51. SerializedProperty m_PropDefaultTextMeshProTextContainerSize;
  52. SerializedProperty m_PropDefaultTextMeshProUITextContainerSize;
  53. SerializedProperty m_PropAutoSizeTextContainer;
  54. SerializedProperty m_PropEnableRaycastTarget;
  55. SerializedProperty m_PropSpriteAsset;
  56. SerializedProperty m_PropSpriteAssetPath;
  57. SerializedProperty m_PropEnableEmojiSupport;
  58. SerializedProperty m_PropStyleSheet;
  59. ReorderableList m_List;
  60. SerializedProperty m_PropColorGradientPresetsPath;
  61. SerializedProperty m_PropMatchMaterialPreset;
  62. SerializedProperty m_PropWordWrapping;
  63. SerializedProperty m_PropKerning;
  64. SerializedProperty m_PropExtraPadding;
  65. SerializedProperty m_PropTintAllSprites;
  66. SerializedProperty m_PropParseEscapeCharacters;
  67. SerializedProperty m_PropMissingGlyphCharacter;
  68. SerializedProperty m_GetFontFeaturesAtRuntime;
  69. SerializedProperty m_PropWarningsDisabled;
  70. SerializedProperty m_PropLeadingCharacters;
  71. SerializedProperty m_PropFollowingCharacters;
  72. public void OnEnable()
  73. {
  74. if (target == null)
  75. return;
  76. m_PropFontAsset = serializedObject.FindProperty("m_defaultFontAsset");
  77. m_PropDefaultFontAssetPath = serializedObject.FindProperty("m_defaultFontAssetPath");
  78. m_PropDefaultFontSize = serializedObject.FindProperty("m_defaultFontSize");
  79. m_PropDefaultAutoSizeMinRatio = serializedObject.FindProperty("m_defaultAutoSizeMinRatio");
  80. m_PropDefaultAutoSizeMaxRatio = serializedObject.FindProperty("m_defaultAutoSizeMaxRatio");
  81. m_PropDefaultTextMeshProTextContainerSize = serializedObject.FindProperty("m_defaultTextMeshProTextContainerSize");
  82. m_PropDefaultTextMeshProUITextContainerSize = serializedObject.FindProperty("m_defaultTextMeshProUITextContainerSize");
  83. m_PropAutoSizeTextContainer = serializedObject.FindProperty("m_autoSizeTextContainer");
  84. m_PropEnableRaycastTarget = serializedObject.FindProperty("m_EnableRaycastTarget");
  85. m_PropSpriteAsset = serializedObject.FindProperty("m_defaultSpriteAsset");
  86. m_PropSpriteAssetPath = serializedObject.FindProperty("m_defaultSpriteAssetPath");
  87. m_PropEnableEmojiSupport = serializedObject.FindProperty("m_enableEmojiSupport");
  88. m_PropStyleSheet = serializedObject.FindProperty("m_defaultStyleSheet");
  89. m_PropColorGradientPresetsPath = serializedObject.FindProperty("m_defaultColorGradientPresetsPath");
  90. m_List = new ReorderableList(serializedObject, serializedObject.FindProperty("m_fallbackFontAssets"), true, true, true, true);
  91. m_List.drawElementCallback = (rect, index, isActive, isFocused) =>
  92. {
  93. var element = m_List.serializedProperty.GetArrayElementAtIndex(index);
  94. rect.y += 2;
  95. EditorGUI.PropertyField(new Rect(rect.x, rect.y, rect.width, EditorGUIUtility.singleLineHeight), element, GUIContent.none);
  96. };
  97. m_List.drawHeaderCallback = rect =>
  98. {
  99. EditorGUI.LabelField(rect, Styles.fallbackFontAssetsListLabel);
  100. };
  101. m_PropMatchMaterialPreset = serializedObject.FindProperty("m_matchMaterialPreset");
  102. m_PropWordWrapping = serializedObject.FindProperty("m_enableWordWrapping");
  103. m_PropKerning = serializedObject.FindProperty("m_enableKerning");
  104. m_PropExtraPadding = serializedObject.FindProperty("m_enableExtraPadding");
  105. m_PropTintAllSprites = serializedObject.FindProperty("m_enableTintAllSprites");
  106. m_PropParseEscapeCharacters = serializedObject.FindProperty("m_enableParseEscapeCharacters");
  107. m_PropMissingGlyphCharacter = serializedObject.FindProperty("m_missingGlyphCharacter");
  108. m_PropWarningsDisabled = serializedObject.FindProperty("m_warningsDisabled");
  109. m_GetFontFeaturesAtRuntime = serializedObject.FindProperty("m_GetFontFeaturesAtRuntime");
  110. m_PropLeadingCharacters = serializedObject.FindProperty("m_leadingCharacters");
  111. m_PropFollowingCharacters = serializedObject.FindProperty("m_followingCharacters");
  112. }
  113. public override void OnInspectorGUI()
  114. {
  115. serializedObject.Update();
  116. float labelWidth = EditorGUIUtility.labelWidth;
  117. float fieldWidth = EditorGUIUtility.fieldWidth;
  118. // TextMeshPro Font Info Panel
  119. EditorGUI.indentLevel = 0;
  120. // FONT ASSET
  121. EditorGUILayout.BeginVertical(EditorStyles.helpBox);
  122. GUILayout.Label(Styles.defaultFontAssetLabel, EditorStyles.boldLabel);
  123. EditorGUI.indentLevel = 1;
  124. EditorGUILayout.PropertyField(m_PropFontAsset, Styles.defaultFontAssetLabel);
  125. EditorGUILayout.PropertyField(m_PropDefaultFontAssetPath, Styles.defaultFontAssetPathLabel);
  126. EditorGUI.indentLevel = 0;
  127. EditorGUILayout.Space();
  128. EditorGUILayout.EndVertical();
  129. // FALLBACK FONT ASSETs
  130. EditorGUILayout.BeginVertical(EditorStyles.helpBox);
  131. GUILayout.Label(Styles.fallbackFontAssetsLabel, EditorStyles.boldLabel);
  132. m_List.DoLayoutList();
  133. GUILayout.Label(Styles.fallbackMaterialSettingsLabel, EditorStyles.boldLabel);
  134. EditorGUI.indentLevel = 1;
  135. EditorGUILayout.PropertyField(m_PropMatchMaterialPreset, Styles.matchMaterialPresetLabel);
  136. EditorGUI.indentLevel = 0;
  137. EditorGUILayout.Space();
  138. EditorGUILayout.EndVertical();
  139. // MISSING GLYPHS
  140. EditorGUILayout.BeginVertical(EditorStyles.helpBox);
  141. GUILayout.Label(Styles.dynamicFontSystemSettingsLabel, EditorStyles.boldLabel);
  142. EditorGUI.indentLevel = 1;
  143. EditorGUILayout.PropertyField(m_GetFontFeaturesAtRuntime, Styles.getFontFeaturesAtRuntime);
  144. EditorGUILayout.PropertyField(m_PropMissingGlyphCharacter, Styles.missingGlyphLabel);
  145. EditorGUILayout.PropertyField(m_PropWarningsDisabled, Styles.disableWarningsLabel);
  146. EditorGUI.indentLevel = 0;
  147. EditorGUILayout.Space();
  148. EditorGUILayout.EndVertical();
  149. // TEXT OBJECT DEFAULT PROPERTIES
  150. EditorGUILayout.BeginVertical(EditorStyles.helpBox);
  151. GUILayout.Label(Styles.containerDefaultSettingsLabel, EditorStyles.boldLabel);
  152. EditorGUI.indentLevel = 1;
  153. EditorGUILayout.PropertyField(m_PropDefaultTextMeshProTextContainerSize, Styles.textMeshProLabel);
  154. EditorGUILayout.PropertyField(m_PropDefaultTextMeshProUITextContainerSize, Styles.textMeshProUiLabel);
  155. EditorGUILayout.PropertyField(m_PropEnableRaycastTarget, Styles.enableRaycastTarget);
  156. EditorGUILayout.PropertyField(m_PropAutoSizeTextContainer, Styles.autoSizeContainerLabel);
  157. EditorGUI.indentLevel = 0;
  158. EditorGUILayout.Space();
  159. GUILayout.Label(Styles.textComponentDefaultSettingsLabel, EditorStyles.boldLabel);
  160. EditorGUI.indentLevel = 1;
  161. EditorGUILayout.PropertyField(m_PropDefaultFontSize, Styles.defaultFontSize);
  162. EditorGUILayout.BeginHorizontal();
  163. {
  164. EditorGUILayout.PrefixLabel(Styles.autoSizeRatioLabel);
  165. EditorGUIUtility.labelWidth = 32;
  166. EditorGUIUtility.fieldWidth = 10;
  167. EditorGUI.indentLevel = 0;
  168. EditorGUILayout.PropertyField(m_PropDefaultAutoSizeMinRatio, Styles.minLabel);
  169. EditorGUILayout.PropertyField(m_PropDefaultAutoSizeMaxRatio, Styles.maxLabel);
  170. EditorGUI.indentLevel = 1;
  171. }
  172. EditorGUILayout.EndHorizontal();
  173. EditorGUIUtility.labelWidth = labelWidth;
  174. EditorGUIUtility.fieldWidth = fieldWidth;
  175. EditorGUILayout.PropertyField(m_PropWordWrapping, Styles.wordWrappingLabel);
  176. EditorGUILayout.PropertyField(m_PropKerning, Styles.kerningLabel);
  177. EditorGUILayout.PropertyField(m_PropExtraPadding, Styles.extraPaddingLabel);
  178. EditorGUILayout.PropertyField(m_PropTintAllSprites, Styles.tintAllSpritesLabel);
  179. EditorGUILayout.PropertyField(m_PropParseEscapeCharacters, Styles.parseEscapeCharactersLabel);
  180. EditorGUI.indentLevel = 0;
  181. EditorGUILayout.Space();
  182. EditorGUILayout.EndVertical();
  183. // SPRITE ASSET
  184. EditorGUILayout.BeginVertical(EditorStyles.helpBox);
  185. GUILayout.Label(Styles.defaultSpriteAssetLabel, EditorStyles.boldLabel);
  186. EditorGUI.indentLevel = 1;
  187. EditorGUILayout.PropertyField(m_PropSpriteAsset, Styles.defaultSpriteAssetLabel);
  188. EditorGUILayout.PropertyField(m_PropEnableEmojiSupport, Styles.enableEmojiSupportLabel);
  189. EditorGUILayout.PropertyField(m_PropSpriteAssetPath, Styles.spriteAssetsPathLabel);
  190. EditorGUI.indentLevel = 0;
  191. EditorGUILayout.Space();
  192. EditorGUILayout.EndVertical();
  193. // STYLE SHEET
  194. EditorGUILayout.BeginVertical(EditorStyles.helpBox);
  195. GUILayout.Label(Styles.defaultStyleSheetLabel, EditorStyles.boldLabel);
  196. EditorGUI.indentLevel = 1;
  197. EditorGUI.BeginChangeCheck();
  198. EditorGUILayout.PropertyField(m_PropStyleSheet, Styles.defaultStyleSheetLabel);
  199. if (EditorGUI.EndChangeCheck())
  200. {
  201. serializedObject.ApplyModifiedProperties();
  202. TMP_StyleSheet.UpdateStyleSheet();
  203. }
  204. EditorGUI.indentLevel = 0;
  205. EditorGUILayout.Space();
  206. EditorGUILayout.EndVertical();
  207. // COLOR GRADIENT PRESETS
  208. EditorGUILayout.BeginVertical(EditorStyles.helpBox);
  209. GUILayout.Label(Styles.colorGradientPresetsLabel, EditorStyles.boldLabel);
  210. EditorGUI.indentLevel = 1;
  211. EditorGUILayout.PropertyField(m_PropColorGradientPresetsPath, Styles.colorGradientsPathLabel);
  212. EditorGUI.indentLevel = 0;
  213. EditorGUILayout.Space();
  214. EditorGUILayout.EndVertical();
  215. // LINE BREAKING RULE
  216. EditorGUILayout.BeginVertical(EditorStyles.helpBox);
  217. GUILayout.Label(Styles.lineBreakingLabel, EditorStyles.boldLabel);
  218. EditorGUI.indentLevel = 1;
  219. EditorGUILayout.PropertyField(m_PropLeadingCharacters);
  220. EditorGUILayout.PropertyField(m_PropFollowingCharacters);
  221. EditorGUI.indentLevel = 0;
  222. EditorGUILayout.Space();
  223. EditorGUILayout.EndVertical();
  224. if (serializedObject.ApplyModifiedProperties())
  225. {
  226. EditorUtility.SetDirty(target);
  227. TMPro_EventManager.ON_TMP_SETTINGS_CHANGED();
  228. }
  229. }
  230. }
  231. #if UNITY_2018_3_OR_NEWER
  232. class TMP_ResourceImporterProvider : SettingsProvider
  233. {
  234. TMP_PackageResourceImporter m_ResourceImporter;
  235. public TMP_ResourceImporterProvider()
  236. : base("Project/TextMesh Pro", SettingsScope.Project)
  237. {
  238. }
  239. public override void OnGUI(string searchContext)
  240. {
  241. // Lazy creation that supports domain reload
  242. if (m_ResourceImporter == null)
  243. m_ResourceImporter = new TMP_PackageResourceImporter();
  244. m_ResourceImporter.OnGUI();
  245. }
  246. public override void OnDeactivate()
  247. {
  248. if (m_ResourceImporter != null)
  249. m_ResourceImporter.OnDestroy();
  250. }
  251. static UnityEngine.Object GetTMPSettings()
  252. {
  253. return Resources.Load<TMP_Settings>("TMP Settings");
  254. }
  255. [SettingsProviderGroup]
  256. static SettingsProvider[] CreateTMPSettingsProvider()
  257. {
  258. var providers = new List<SettingsProvider> { new TMP_ResourceImporterProvider() };
  259. if (GetTMPSettings() != null)
  260. {
  261. var provider = new AssetSettingsProvider("Project/TextMesh Pro/Settings", GetTMPSettings);
  262. provider.PopulateSearchKeywordsFromGUIContentProperties<TMP_SettingsEditor.Styles>();
  263. providers.Add(provider);
  264. }
  265. return providers.ToArray();
  266. }
  267. }
  268. #endif
  269. }