using System; using System.Collections; using System.Collections.Generic; using System.Globalization; using UnityEngine; using UnityEngine.UI; [RequireComponent(typeof(InputField))] public class InputSpecialWordLimit : MonoBehaviour { [SerializeField, Header("是否限制空格")] bool m_SpaceLimit = false; private void Awake() { var _input = GetComponent(); _input.onValidateInput = OnValidateInput; } private char OnValidateInput(string text, int charIndex, char addedChar) { if (char.GetUnicodeCategory(addedChar) == UnicodeCategory.Surrogate || char.GetUnicodeCategory(addedChar) == UnicodeCategory.OtherSymbol || char.GetUnicodeCategory(addedChar) == UnicodeCategory.OtherNumber || char.GetUnicodeCategory(addedChar) == UnicodeCategory.MathSymbol || char.GetUnicodeCategory(addedChar) == UnicodeCategory.OtherNotAssigned || addedChar == '\n') { return (char)0; } if (m_SpaceLimit) { if (addedChar == ' ' || addedChar == ' ' || char.GetUnicodeCategory(addedChar) == UnicodeCategory.SpaceSeparator) { return (char)0; } } return addedChar; } }