Unity3D InputField.onValidateInput 输入验证

InputField.onValidateInput
Leave feedback
public UI.InputField.OnValidateInput onValidateInput; 

The function to call to validate the input characters.

using UnityEngine;
using System.Collections;
using UnityEngine.UI; // Required when Using UI elements.

public class Example : MonoBehaviour
{
    public InputField mainInputField;

    public void Start()
    {
        // Sets the MyValidate method to invoke after the input field's default input validation invoke (default validation happens every time a character is entered into the text field.)
        mainInputField.onValidateInput += delegate(string input, int charIndex, char addedChar) { return MyValidate(addedChar); };
    }

    private char MyValidate(char charToValidate)
    {
        //Checks if a dollar sign is entered....
        if (charToValidate == '$')
        {
            // ... if it is change it to an empty character.
            charToValidate = '\0';
        }
        return charToValidate;
    }
}

Custom:验证输入是否为数字

static char empty = '\0';
 private char ValidateFloat(int charIndex, char charToValidate)
    {
        if ((charToValidate >= '0' && charToValidate <= '9') || (charIndex > 0 && charToValidate == '.'))
        {
            return charToValidate;
            // ... if it is change it to an empty character.

        }
        return empty;
    }

你可能感兴趣的:(Unity,3D与编程语言,unity)