unity 3 d文字列の符号化フォーマットをチェックし、指定したバイト数の文字列を制限する
6440 ワード
汎用の符号化フォーマットはUTF 8で、codingTypeタイプを変更して所望の符号化フォーマットの結果を得ることもできます
using UnityEngine.UI;
using System;
using UnityEngine;
///
///
///
public enum CodingType
{
UTF8 = 1,
ASCII,
Unicode,
GB,
}
public class CheckCodingFormat : MonoBehaviour
{
///
///
///
///
public delegate void CallBackInputContent(string content);
public event CallBackInputContent onChangeValueCallBack;
public event CallBackInputContent onEndEditCallBack;
///
///
///
public int BYTE_LIMIT = 10;
public CodingType codingType = CodingType.UTF8;
private InputField inputField;
void Awake()
{
inputField = transform.GetComponent();
inputField.onEndEdit.AddListener(OnEndEdit);
}
///
///
///
///
private void OnChangedValue(string value)
{
value = GetCodingResult(codingType);
inputField.text = value;
if (onChangeValueCallBack != null)
{
onChangeValueCallBack(value);
}
}
///
///
///
///
private void OnEndEdit(string value)
{
value = GetCodingResult(codingType);
inputField.text = value;
if (onEndEditCallBack != null)
{
onEndEditCallBack(value);
}
}
///
///
///
///
///
public string GetCodingResult(CodingType checkType)
{
string value = "";
string temp = inputField.text.Substring(0, (inputField.text.Length < BYTE_LIMIT + 1) ? inputField.text.Length : BYTE_LIMIT + 1);
switch (checkType)
{
case CodingType.UTF8:
value = CodingByUTF8(temp);
break;
case CodingType.ASCII:
value = CodingByASCII(temp);
break;
case CodingType.Unicode:
value = CodingByUnicode(temp);
break;
case CodingType.GB:
value = CodingByGB(temp);
break;
}
return value;
}
///
/// UTF8
///
///
///
private string CodingByUTF8(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);
string output = "[" + temp + "]";
for (int byteIndex = 0; byteIndex < encodedBytes.Length; byteIndex++)
{
output += Convert.ToString((int)encodedBytes[byteIndex], 2) + " ";
}
int byteCount = System.Text.ASCIIEncoding.UTF8.GetByteCount(tempStr);
// 2 , 1
count = byteCount > 1 ? count += 2 : count += 1;
if (count <= BYTE_LIMIT)
outputStr += tempStr;
else
break;
}
return outputStr;
}
///
/// Unicode
///
///
///
private string CodingByUnicode(string temp)
{
string outputStr = "";
int count = 0;
for (int i = 0; i < temp.Length; i++)
{
string tempStr = temp.Substring(i, 1);
// Unicode
byte[] encodedBytes = System.Text.ASCIIEncoding.Unicode.GetBytes(tempStr);
if (encodedBytes.Length == 2)
{
int byteValue = (int)encodedBytes[1];
if (byteValue == 0)//
{
count += 1;
}
else
{
count += 2;
}
}
if (count <= BYTE_LIMIT)
{
outputStr += tempStr;
}
else
{
break;
}
}
return outputStr;
}
///
/// GB
///
///
///
private string CodingByGB(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 <= BYTE_LIMIT)
{
outputStr += tempStr;
}
else
{
break;
}
}
return outputStr;
}
///
/// ASCII
///
///
///
private string CodingByASCII(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 <= BYTE_LIMIT)
outputStr += temp.Substring(i, 1);
else if (count > BYTE_LIMIT)
break;
}
if (count <= BYTE_LIMIT)
{
outputStr = temp;
}
return outputStr;
}
}
//
public static int CharacterLength(string content)
{
content = content.Replace('?', 'a');
byte[] _byteArray = System.Text.Encoding.ASCII.GetBytes(content);
int _count = 0;
for (int i = 0; i < _byteArray.Length; ++i)
{
// ASCII ? - 63
if (63 == _byteArray[i])
{
_count += 2;
}
else
{
_count++;
}
}
return _count;
}