zl程序教程

您现在的位置是:首页 >  工具

当前栏目

Unity 之UGUI InputField 组件的使用

组件 Unity Ugui 使用
2023-09-11 14:20:51 时间

Unity InputField 组件的面板

常用属性

常用属性面板简析:
面板
Interactable :当前输入框是否可用

Text Component:此输入域的文本显示组件,任何带有Text组件的物体。

Text(文本):此输入域的初始值。

Character Limit(字符数量限制):限定此输入域最大输入的字符数,0为不限制。

Content Type(内容类型):限定此输入域的内容类型,包括数字、密码等,常用的类型如下:

Standard(标准类型):什么字符都能输入,只要是当前字体支持的。
Integer Number(整数类型):只能输入一个整数。
Decimal Number(十进制数):能输入整数或小数。
Alpha numeric(文字和数字):能输入数字和字母。
Name(姓名类型):能输入英文及其他文字,当输入英文时自动姓名化(首字母大写)。
Password(密码类型):输入的字符隐藏为星号。

Line Type(换行方式):当输入的内容超过输入域边界时:

single Line(单一行):超过边界也不换行,继续延伸此行,输入域中的内容只有一行;
multi Line Submit(多行):超过边界则换行,输入域中内容有多行;
multi Line Newline(多行):超过边界则新建换行,输入域中内容有多行。

Placeholder(位置标示):此输入域的输入位控制符,任何带有Text组件的物体。

Caret blink rate(光标闪烁速度):标示输入光标的闪烁速度。

Hide mobile input : 手机隐藏输入

On Value Changed:值改变时触发消息。

End Edit:结束编辑时触发消息。


代码简例:

	public InputField match_input;
    private string match_JiangLiNum;
    void Init() //注册监听
    {
        match_input.onValueChanged.AddListener(Input_OnChanged);
        //控制只可以输入数字
        match_input.contentType = InputField.ContentType.IntegerNumber;
        match_input.onEndEdit.AddListener(Input_End);
    }
    
    public void Input_OnChanged(string context)  
    {
       
    }
    
    public void Input_End() //输入数字完成后显示 
    {
        match_input.contentType = InputField.ContentType.Standard;

        Debug.Log(match_input.text);
        if (match_input.text.Length > 5)//限制长度
        {
            match_input.text = match_input.text.Substring(0, 5);
        }
        //避免带入上次的文字显示
        string str = GetNumberForString(match_input.text);
        match_JiangLiNum = (str != "" ? str : "1");
        match_input.text = "前" + match_JiangLiNum + "名";
    }

    /// <summary>
    /// 获取字符串中的数字
    /// </summary>
    /// <param name="str"></param>
    /// <returns></returns>
    public string GetNumberForString(string str)
    {
        string num = "";
        char[] ch = new char[str.Length];
        ch = str.ToCharArray();
        for (int i = 0; i < str.Length; i++)
        {
            if (ch[i] >= 48 && ch[i] <= 57)
                num += ch[i];
        }
        return num;
    }


2019-11-28 :: 遇到一个要限制输入框文本长度的问题,需求是中文字符长度算1数字则算1/2,想通过Character Limit属性来限制则不能达到想要的效果【需要输入时校验文本长度,多的不让输入,而不是完成输入时截取到限制长度】,思虑再三也没想到什么好的方法来解决,上网查了下还真找到一个好用的方法。

原文链接:https://blog.csdn.net/yangchunnoodles/article/details/52985441#commentBox

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System;

public class CheckNameLengthScript : MonoBehaviour {

    public InputField input;
    private const int CHARACTER_LIMIT = 10;
    public CheckNameLengthScript.SplitType m_SplitType = SplitType.ASCII;

    public enum SplitType
    {
        ASCII=1,
        GB=2,
        Unicode=3,
        UTF8=4,
    }

    public void Check()
    {
        input.text = GetSplitName((int)m_SplitType);
    }

    public string GetSplitName(int checkType)
    {
        string temp = input.text.Substring(0, (input.text.Length < CHARACTER_LIMIT + 1) ? input.text.Length : CHARACTER_LIMIT + 1);
        if (checkType == (int)SplitType.ASCII)
        {
            return SplitNameByASCII(temp);
        }
        else if (checkType == (int)SplitType.GB)
        {
            return SplitNameByGB(temp);
        }
        else if (checkType == (int)SplitType.Unicode)
        {
            return SplitNameByUnicode(temp);
        }
        else if (checkType == (int)SplitType.UTF8)
        {
            return SplitNameByUTF8(temp);
        }

        return "";
    }
    //4、UTF8编码格式(汉字3byte,英文1byte),//UTF8编码格式,目前是最常用的 
    private string SplitNameByUTF8(string temp)
    {
        string outputStr = "";
        int count = 0;

        for (int i = 0; i < temp.Length; i++)
        {
            string tempStr = temp.Substring(i, 1);
            byte[] encodedBytes = System.Text.ASCIIEncoding.UTF8.GetBytes(tempStr);//Unicode用两个字节对字符进行编码
            string output = "[" + temp + "]";
            for (int byteIndex = 0; byteIndex < encodedBytes.Length; byteIndex++)
            {
                output += Convert.ToString((int)encodedBytes[byteIndex], 2)+"  ";//二进制
            }
            Debug.Log(output);

            int byteCount = System.Text.ASCIIEncoding.UTF8.GetByteCount(tempStr);
            Debug.Log("字节数=" + byteCount);

            if (byteCount>1)
            {
                count += 2;
            }
            else
            {
                count += 1;
            }
            if (count <= CHARACTER_LIMIT)
            {
                outputStr += tempStr;
            }
            else
            {
                break;
            }
        }
        return outputStr;
    }

    private string SplitNameByUnicode(string temp)
    {
        string outputStr = "";
        int count = 0;

        for (int i = 0; i < temp.Length; i++)
        {
            string tempStr = temp.Substring(i, 1);
            byte[] encodedBytes = System.Text.ASCIIEncoding.Unicode.GetBytes(tempStr);//Unicode用两个字节对字符进行编码
            if(encodedBytes.Length==2)
            {
                int byteValue = (int)encodedBytes[1];
                if (byteValue == 0)//这里是单个字节
                {
                    count += 1;
                }
                else
                {
                    count += 2;
                }
            }
            if (count <= CHARACTER_LIMIT)
            {
                outputStr += tempStr;
            }
            else
            {
                break;
            }
        }
        return outputStr;
    }

    private string SplitNameByGB(string temp)
    {
        string outputStr = "";
        int count = 0;

        for (int i = 0; i < temp.Length; i++)
        {
            string tempStr = temp.Substring(i, 1);
            byte[] encodedBytes = System.Text.ASCIIEncoding.Default.GetBytes(tempStr);
            if (encodedBytes.Length == 1)
            {
                //单字节
                count += 1;
            }
            else
            {
                //双字节
                count += 2;
            }

            if (count <= CHARACTER_LIMIT)
            {
                outputStr += tempStr;
            }
            else
            {
                break;
            }
        }
        return outputStr;
    }

    private string SplitNameByASCII(string temp)
    {
        byte[] encodedBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(temp);

        string outputStr = "";
        int count = 0;

        for (int i = 0; i < temp.Length; i++)
        {
            if ((int)encodedBytes[i] == 63)//双字节
                count += 2;
            else
                count += 1;

            if (count <= CHARACTER_LIMIT)
                outputStr += temp.Substring(i, 1);
            else if (count > CHARACTER_LIMIT)
                break;
        }

        if (count <= CHARACTER_LIMIT)
        {
            outputStr = temp;

        }

        return outputStr;
    }

}




怎么样看了大佬的代码是不是感觉这个问题已经解决了呢,下面简述下使用大佬代码的过程。

  1. 挂载代码到InputField的控件
    111
  2. 代码设置限制字节长度,也就是代码中的CHARACTER_LIMIT 这个常量。
 private const int CHARACTER_LIMIT = 10;
  1. 监听OnValueChange 方法(CheckInputLength)
    2222

这样就可以达到我要的效果了,相当于:中文算一个字符,两个数字算一个字符。