鸟语天空
UGUI—InputField
post by:追风剑情 2021-10-12 15:01

示例 1: 密码输入框

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
/// <summary>
/// 密码输入框
/// </summary>
public class PasswordInputField : MonoBehaviour
{
    [SerializeField]
    private InputField m_InputField;

    private void Awake()
    {
        m_InputField.inputType = InputField.InputType.Password;
        m_InputField.contentType = InputField.ContentType.Password;
        m_InputField.onValidateInput = OnValidateInput;
    }

    // 校验输入字符
    private char OnValidateInput(string text, int charIndex, char addedChar)
    {
        int asciicode = addedChar;

        //字符 : ASCII码
        //_ : 95
        //[0-9] : [48-57]
        //[A-Z] : [65-90]
        //[a-z] : [97-122]

        //只允许输入: 下划线、数字、大写字母、小写字母
        if (asciicode == 95 ||
            asciicode >= 48 && asciicode <= 57 ||
            asciicode >= 65 && asciicode <= 90 ||
            asciicode >= 97 && asciicode <= 122
            )
            return addedChar;
        return '\0';
    }
}

11111.png

评论:
发表评论:
昵称

邮件地址 (选填)

个人主页 (选填)

内容