CheckInputLength.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.UI;
  4. using System;
  5. [RequireComponent(typeof(InputField))]
  6. public class CheckInputLength : MonoBehaviour
  7. {
  8. public int CHARACTER_LIMIT = 10;
  9. private InputField input;
  10. public CheckInputLength.SplitType m_SplitType = SplitType.ASCII;
  11. public enum SplitType
  12. {
  13. ASCII = 1,
  14. GB = 2,
  15. Unicode = 3,
  16. UTF8 = 4,
  17. }
  18. private void Start()
  19. {
  20. input = GetComponent<InputField>();
  21. }
  22. public void Check()
  23. {
  24. input.text = GetSplitName((int)m_SplitType);
  25. }
  26. public string GetSplitName(int checkType)
  27. {
  28. string temp = input.text.Substring(0, (input.text.Length < CHARACTER_LIMIT + 1) ? input.text.Length : CHARACTER_LIMIT + 1);
  29. if (checkType == (int)SplitType.ASCII)
  30. {
  31. return SplitNameByASCII(temp);
  32. }
  33. else if (checkType == (int)SplitType.GB)
  34. {
  35. return SplitNameByGB(temp);
  36. }
  37. else if (checkType == (int)SplitType.Unicode)
  38. {
  39. return SplitNameByUnicode(temp);
  40. }
  41. else if (checkType == (int)SplitType.UTF8)
  42. {
  43. return SplitNameByUTF8(temp);
  44. }
  45. return "";
  46. }
  47. //4、UTF8编码格式(汉字3byte,英文1byte),//UTF8编码格式,目前是最常用的
  48. private string SplitNameByUTF8(string temp)
  49. {
  50. string outputStr = "";
  51. int count = 0;
  52. for (int i = 0; i < temp.Length; i++)
  53. {
  54. string tempStr = temp.Substring(i, 1);
  55. byte[] encodedBytes = System.Text.ASCIIEncoding.UTF8.GetBytes(tempStr);//Unicode用两个字节对字符进行编码
  56. string output = "[" + temp + "]";
  57. for (int byteIndex = 0; byteIndex < encodedBytes.Length; byteIndex++)
  58. {
  59. output += Convert.ToString((int)encodedBytes[byteIndex], 2) + " ";//二进制
  60. }
  61. Debug.Log(output);
  62. int byteCount = System.Text.ASCIIEncoding.UTF8.GetByteCount(tempStr);
  63. Debug.Log("字节数=" + byteCount);
  64. if (byteCount > 1)
  65. {
  66. count += 2;
  67. }
  68. else
  69. {
  70. count += 1;
  71. }
  72. if (count <= CHARACTER_LIMIT)
  73. {
  74. outputStr += tempStr;
  75. }
  76. else
  77. {
  78. break;
  79. }
  80. }
  81. return outputStr;
  82. }
  83. private string SplitNameByUnicode(string temp)
  84. {
  85. string outputStr = "";
  86. int count = 0;
  87. for (int i = 0; i < temp.Length; i++)
  88. {
  89. string tempStr = temp.Substring(i, 1);
  90. byte[] encodedBytes = System.Text.ASCIIEncoding.Unicode.GetBytes(tempStr);//Unicode用两个字节对字符进行编码
  91. if (encodedBytes.Length == 2)
  92. {
  93. int byteValue = (int)encodedBytes[1];
  94. if (byteValue == 0)//这里是单个字节
  95. {
  96. count += 1;
  97. }
  98. else
  99. {
  100. count += 2;
  101. }
  102. }
  103. if (count <= CHARACTER_LIMIT)
  104. {
  105. outputStr += tempStr;
  106. }
  107. else
  108. {
  109. break;
  110. }
  111. }
  112. return outputStr;
  113. }
  114. private string SplitNameByGB(string temp)
  115. {
  116. string outputStr = "";
  117. int count = 0;
  118. for (int i = 0; i < temp.Length; i++)
  119. {
  120. string tempStr = temp.Substring(i, 1);
  121. byte[] encodedBytes = System.Text.ASCIIEncoding.Default.GetBytes(tempStr);
  122. if (encodedBytes.Length == 1)
  123. {
  124. //单字节
  125. count += 1;
  126. }
  127. else
  128. {
  129. //双字节
  130. count += 2;
  131. }
  132. if (count <= CHARACTER_LIMIT)
  133. {
  134. outputStr += tempStr;
  135. }
  136. else
  137. {
  138. break;
  139. }
  140. }
  141. return outputStr;
  142. }
  143. private string SplitNameByASCII(string temp)
  144. {
  145. byte[] encodedBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(temp);
  146. string outputStr = "";
  147. int count = 0;
  148. for (int i = 0; i < temp.Length; i++)
  149. {
  150. if ((int)encodedBytes[i] == 63)//双字节
  151. count += 2;
  152. else
  153. count += 1;
  154. if (count <= CHARACTER_LIMIT)
  155. outputStr += temp.Substring(i, 1);
  156. else if (count > CHARACTER_LIMIT)
  157. break;
  158. }
  159. if (count <= CHARACTER_LIMIT)
  160. {
  161. outputStr = temp;
  162. }
  163. return outputStr;
  164. }
  165. }