UGUI—InputField

作者:追风剑情 发布于:2021-10-12 15:01 分类:Unity3d

示例 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

标签: Unity3d

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号