TMP_DigitValidator.cs 726 B

123456789101112131415161718192021222324252627
  1. using UnityEngine;
  2. using System;
  3. namespace TMPro
  4. {
  5. /// <summary>
  6. /// EXample of a Custom Character Input Validator to only allow digits from 0 to 9.
  7. /// </summary>
  8. [Serializable]
  9. //[CreateAssetMenu(fileName = "InputValidator - Digits.asset", menuName = "TextMeshPro/Input Validators/Digits", order = 100)]
  10. public class TMP_DigitValidator : TMP_InputValidator
  11. {
  12. // Custom text input validation function
  13. public override char Validate(ref string text, ref int pos, char ch)
  14. {
  15. if (ch >= '0' && ch <= '9')
  16. {
  17. text += ch;
  18. pos += 1;
  19. return ch;
  20. }
  21. return (char)0;
  22. }
  23. }
  24. }