Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using UnityEngine.UI;
- using TMPro;
- public class NumericInputField : MonoBehaviour
- {
- //I used a serialized field instead of getting component
- [SerializeField] TMP_InputField inputField;
- // private InputField inputField;
- private void Start()
- {
- // inputField = GetComponent<InputField>();
- // Add the validation function to the onValidateInput event
- inputField.onValidateInput += ValidateInput;
- }
- private char ValidateInput(string text, int charIndex, char addedChar)
- {
- // Allow digits 0-9
- if (char.IsDigit(addedChar))
- {
- return addedChar; // Accept the input
- }
- // Reject all other characters
- return '\0'; // Null character to block the input
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement