暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

TMP_InputFieldEditor.cs 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using UnityEditor;
  4. using UnityEditor.UI;
  5. using UnityEditor.AnimatedValues;
  6. namespace TMPro.EditorUtilities
  7. {
  8. [CanEditMultipleObjects]
  9. [CustomEditor(typeof(TMP_InputField), true)]
  10. public class TMP_InputFieldEditor : SelectableEditor
  11. {
  12. private struct m_foldout
  13. { // Track Inspector foldout panel states, globally.
  14. public static bool textInput = true;
  15. public static bool fontSettings = true;
  16. public static bool extraSettings = true;
  17. //public static bool shadowSetting = false;
  18. //public static bool materialEditor = true;
  19. }
  20. SerializedProperty m_TextViewport;
  21. SerializedProperty m_TextComponent;
  22. SerializedProperty m_Text;
  23. SerializedProperty m_ContentType;
  24. SerializedProperty m_LineType;
  25. SerializedProperty m_LineLimit;
  26. SerializedProperty m_InputType;
  27. SerializedProperty m_CharacterValidation;
  28. SerializedProperty m_InputValidator;
  29. SerializedProperty m_RegexValue;
  30. SerializedProperty m_KeyboardType;
  31. SerializedProperty m_CharacterLimit;
  32. SerializedProperty m_CaretBlinkRate;
  33. SerializedProperty m_CaretWidth;
  34. SerializedProperty m_CaretColor;
  35. SerializedProperty m_CustomCaretColor;
  36. SerializedProperty m_SelectionColor;
  37. SerializedProperty m_HideMobileKeyboard;
  38. SerializedProperty m_HideMobileInput;
  39. SerializedProperty m_Placeholder;
  40. SerializedProperty m_VerticalScrollbar;
  41. SerializedProperty m_ScrollbarScrollSensitivity;
  42. SerializedProperty m_OnValueChanged;
  43. SerializedProperty m_OnEndEdit;
  44. SerializedProperty m_OnSelect;
  45. SerializedProperty m_OnDeselect;
  46. SerializedProperty m_ReadOnly;
  47. SerializedProperty m_RichText;
  48. SerializedProperty m_RichTextEditingAllowed;
  49. SerializedProperty m_ResetOnDeActivation;
  50. SerializedProperty m_KeepTextSelectionVisible;
  51. SerializedProperty m_RestoreOriginalTextOnEscape;
  52. SerializedProperty m_ShouldActivateOnSelect;
  53. SerializedProperty m_OnFocusSelectAll;
  54. SerializedProperty m_GlobalPointSize;
  55. SerializedProperty m_GlobalFontAsset;
  56. AnimBool m_CustomColor;
  57. //TMP_InputValidator m_ValidationScript;
  58. protected override void OnEnable()
  59. {
  60. base.OnEnable();
  61. m_TextViewport = serializedObject.FindProperty("m_TextViewport");
  62. m_TextComponent = serializedObject.FindProperty("m_TextComponent");
  63. m_Text = serializedObject.FindProperty("m_Text");
  64. m_ContentType = serializedObject.FindProperty("m_ContentType");
  65. m_LineType = serializedObject.FindProperty("m_LineType");
  66. m_LineLimit = serializedObject.FindProperty("m_LineLimit");
  67. m_InputType = serializedObject.FindProperty("m_InputType");
  68. m_CharacterValidation = serializedObject.FindProperty("m_CharacterValidation");
  69. m_InputValidator = serializedObject.FindProperty("m_InputValidator");
  70. m_RegexValue = serializedObject.FindProperty("m_RegexValue");
  71. m_KeyboardType = serializedObject.FindProperty("m_KeyboardType");
  72. m_CharacterLimit = serializedObject.FindProperty("m_CharacterLimit");
  73. m_CaretBlinkRate = serializedObject.FindProperty("m_CaretBlinkRate");
  74. m_CaretWidth = serializedObject.FindProperty("m_CaretWidth");
  75. m_CaretColor = serializedObject.FindProperty("m_CaretColor");
  76. m_CustomCaretColor = serializedObject.FindProperty("m_CustomCaretColor");
  77. m_SelectionColor = serializedObject.FindProperty("m_SelectionColor");
  78. m_HideMobileKeyboard = serializedObject.FindProperty("m_HideSoftKeyboard");
  79. m_HideMobileInput = serializedObject.FindProperty("m_HideMobileInput");
  80. m_Placeholder = serializedObject.FindProperty("m_Placeholder");
  81. m_VerticalScrollbar = serializedObject.FindProperty("m_VerticalScrollbar");
  82. m_ScrollbarScrollSensitivity = serializedObject.FindProperty("m_ScrollSensitivity");
  83. m_OnValueChanged = serializedObject.FindProperty("m_OnValueChanged");
  84. m_OnEndEdit = serializedObject.FindProperty("m_OnEndEdit");
  85. m_OnSelect = serializedObject.FindProperty("m_OnSelect");
  86. m_OnDeselect = serializedObject.FindProperty("m_OnDeselect");
  87. m_ReadOnly = serializedObject.FindProperty("m_ReadOnly");
  88. m_RichText = serializedObject.FindProperty("m_RichText");
  89. m_RichTextEditingAllowed = serializedObject.FindProperty("m_isRichTextEditingAllowed");
  90. m_ResetOnDeActivation = serializedObject.FindProperty("m_ResetOnDeActivation");
  91. m_KeepTextSelectionVisible = serializedObject.FindProperty("m_KeepTextSelectionVisible");
  92. m_RestoreOriginalTextOnEscape = serializedObject.FindProperty("m_RestoreOriginalTextOnEscape");
  93. m_OnFocusSelectAll = serializedObject.FindProperty("m_OnFocusSelectAll");
  94. m_ShouldActivateOnSelect = serializedObject.FindProperty("m_ShouldActivateOnSelect");
  95. m_GlobalPointSize = serializedObject.FindProperty("m_GlobalPointSize");
  96. m_GlobalFontAsset = serializedObject.FindProperty("m_GlobalFontAsset");
  97. m_CustomColor = new AnimBool(m_CustomCaretColor.boolValue);
  98. m_CustomColor.valueChanged.AddListener(Repaint);
  99. }
  100. protected override void OnDisable()
  101. {
  102. base.OnDisable();
  103. m_CustomColor.valueChanged.RemoveListener(Repaint);
  104. }
  105. public override void OnInspectorGUI()
  106. {
  107. serializedObject.Update();
  108. base.OnInspectorGUI();
  109. EditorGUILayout.Space();
  110. EditorGUILayout.PropertyField(m_TextViewport);
  111. EditorGUILayout.PropertyField(m_TextComponent);
  112. TextMeshProUGUI text = null;
  113. if (m_TextComponent != null && m_TextComponent.objectReferenceValue != null)
  114. {
  115. text = m_TextComponent.objectReferenceValue as TextMeshProUGUI;
  116. //if (text.supportRichText)
  117. //{
  118. // EditorGUILayout.HelpBox("Using Rich Text with input is unsupported.", MessageType.Warning);
  119. //}
  120. }
  121. EditorGUI.BeginDisabledGroup(m_TextComponent == null || m_TextComponent.objectReferenceValue == null);
  122. // TEXT INPUT BOX
  123. EditorGUILayout.PropertyField(m_Text);
  124. // INPUT FIELD SETTINGS
  125. #region INPUT FIELD SETTINGS
  126. m_foldout.fontSettings = EditorGUILayout.Foldout(m_foldout.fontSettings, "Input Field Settings", true, TMP_UIStyleManager.boldFoldout);
  127. if (m_foldout.fontSettings)
  128. {
  129. EditorGUI.indentLevel++;
  130. EditorGUI.BeginChangeCheck();
  131. EditorGUILayout.PropertyField(m_GlobalFontAsset, new GUIContent("Font Asset", "Set the Font Asset for both Placeholder and Input Field text object."));
  132. if (EditorGUI.EndChangeCheck())
  133. {
  134. TMP_InputField inputField = target as TMP_InputField;
  135. inputField.SetGlobalFontAsset(m_GlobalFontAsset.objectReferenceValue as TMP_FontAsset);
  136. }
  137. EditorGUI.BeginChangeCheck();
  138. EditorGUILayout.PropertyField(m_GlobalPointSize, new GUIContent("Point Size", "Set the point size of both Placeholder and Input Field text object."));
  139. if (EditorGUI.EndChangeCheck())
  140. {
  141. TMP_InputField inputField = target as TMP_InputField;
  142. inputField.SetGlobalPointSize(m_GlobalPointSize.floatValue);
  143. }
  144. EditorGUI.BeginChangeCheck();
  145. EditorGUILayout.PropertyField(m_CharacterLimit);
  146. EditorGUILayout.Space();
  147. EditorGUILayout.PropertyField(m_ContentType);
  148. if (!m_ContentType.hasMultipleDifferentValues)
  149. {
  150. EditorGUI.indentLevel++;
  151. if (m_ContentType.enumValueIndex == (int)TMP_InputField.ContentType.Standard ||
  152. m_ContentType.enumValueIndex == (int)TMP_InputField.ContentType.Autocorrected ||
  153. m_ContentType.enumValueIndex == (int)TMP_InputField.ContentType.Custom)
  154. {
  155. EditorGUI.BeginChangeCheck();
  156. EditorGUILayout.PropertyField(m_LineType);
  157. if (EditorGUI.EndChangeCheck())
  158. {
  159. if (text != null)
  160. {
  161. if (m_LineType.enumValueIndex == (int)TMP_InputField.LineType.SingleLine)
  162. text.textWrappingMode = TextWrappingModes.PreserveWhitespaceNoWrap;
  163. else
  164. {
  165. text.textWrappingMode = TextWrappingModes.Normal;
  166. }
  167. }
  168. }
  169. if (m_LineType.enumValueIndex != (int)TMP_InputField.LineType.SingleLine)
  170. {
  171. EditorGUILayout.PropertyField(m_LineLimit);
  172. }
  173. }
  174. if (m_ContentType.enumValueIndex == (int)TMP_InputField.ContentType.Custom)
  175. {
  176. EditorGUILayout.PropertyField(m_InputType);
  177. EditorGUILayout.PropertyField(m_KeyboardType);
  178. EditorGUILayout.PropertyField(m_CharacterValidation);
  179. if (m_CharacterValidation.enumValueIndex == (int)TMP_InputField.CharacterValidation.Regex)
  180. {
  181. EditorGUILayout.PropertyField(m_RegexValue);
  182. }
  183. else if (m_CharacterValidation.enumValueIndex == (int)TMP_InputField.CharacterValidation.CustomValidator)
  184. {
  185. EditorGUILayout.PropertyField(m_InputValidator);
  186. }
  187. }
  188. EditorGUI.indentLevel--;
  189. }
  190. EditorGUILayout.Space();
  191. EditorGUILayout.PropertyField(m_Placeholder);
  192. EditorGUILayout.PropertyField(m_VerticalScrollbar);
  193. if (m_VerticalScrollbar.objectReferenceValue != null)
  194. EditorGUILayout.PropertyField(m_ScrollbarScrollSensitivity);
  195. EditorGUILayout.PropertyField(m_CaretBlinkRate);
  196. EditorGUILayout.PropertyField(m_CaretWidth);
  197. EditorGUILayout.PropertyField(m_CustomCaretColor);
  198. m_CustomColor.target = m_CustomCaretColor.boolValue;
  199. if (EditorGUILayout.BeginFadeGroup(m_CustomColor.faded))
  200. {
  201. EditorGUILayout.PropertyField(m_CaretColor);
  202. }
  203. EditorGUILayout.EndFadeGroup();
  204. EditorGUILayout.PropertyField(m_SelectionColor);
  205. EditorGUI.indentLevel--;
  206. }
  207. #endregion
  208. // CONTROL SETTINGS
  209. #region CONTROL SETTINGS
  210. m_foldout.extraSettings = EditorGUILayout.Foldout(m_foldout.extraSettings, "Control Settings", true, TMP_UIStyleManager.boldFoldout);
  211. if (m_foldout.extraSettings)
  212. {
  213. EditorGUI.indentLevel++;
  214. EditorGUILayout.PropertyField(m_OnFocusSelectAll, new GUIContent("OnFocus - Select All", "Should all the text be selected when the Input Field is selected?"));
  215. EditorGUILayout.PropertyField(m_ResetOnDeActivation, new GUIContent("Reset On Deactivation", "Should the Text and Caret position be reset when Input Field looses focus and is Deactivated?"));
  216. EditorGUI.indentLevel++;
  217. GUI.enabled = !m_ResetOnDeActivation.boolValue;
  218. EditorGUILayout.PropertyField(m_KeepTextSelectionVisible, new GUIContent("Keep Text Selection Visible", "Should the text selection remain visible when the input field looses focus and is deactivated?"));
  219. GUI.enabled = true;
  220. EditorGUI.indentLevel--;
  221. EditorGUILayout.PropertyField(m_RestoreOriginalTextOnEscape, new GUIContent("Restore On ESC Key", "Should the original text be restored when pressing ESC? (Property not applicable for HoloLens)"));
  222. EditorGUILayout.PropertyField(m_ShouldActivateOnSelect, new GUIContent("Should Activate On Select", "Determines if the Input Field will be activated when selected."));
  223. EditorGUILayout.PropertyField(m_HideMobileKeyboard, new GUIContent("Hide Soft Keyboard", "Controls the visibility of the mobile virtual keyboard."));
  224. EditorGUI.BeginDisabledGroup(m_HideMobileKeyboard.boolValue);
  225. EditorGUILayout.PropertyField(m_HideMobileInput, new GUIContent("Hide Mobile Input", "Controls the visibility of the editable text field above the mobile virtual keyboard."));
  226. EditorGUI.EndDisabledGroup();
  227. EditorGUILayout.PropertyField(m_ReadOnly);
  228. EditorGUILayout.PropertyField(m_RichText);
  229. EditorGUILayout.PropertyField(m_RichTextEditingAllowed, new GUIContent("Allow Rich Text Editing"));
  230. EditorGUI.indentLevel--;
  231. }
  232. #endregion
  233. EditorGUILayout.Space();
  234. EditorGUILayout.PropertyField(m_OnValueChanged);
  235. EditorGUILayout.PropertyField(m_OnEndEdit);
  236. EditorGUILayout.PropertyField(m_OnSelect);
  237. EditorGUILayout.PropertyField(m_OnDeselect);
  238. EditorGUI.EndDisabledGroup();
  239. serializedObject.ApplyModifiedProperties();
  240. }
  241. }
  242. }