ro-webgl/Assets/Src/Utils/CheckInputLength.cs
2021-12-21 09:40:39 +08:00

188 lines
4.9 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System;
[RequireComponent(typeof(InputField))]
public class CheckInputLength : MonoBehaviour
{
public int CHARACTER_LIMIT = 10;
private InputField input;
public CheckInputLength.SplitType m_SplitType = SplitType.ASCII;
public enum SplitType
{
ASCII = 1,
GB = 2,
Unicode = 3,
UTF8 = 4,
}
private void Start()
{
input = GetComponent<InputField>();
}
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;
}
}