转载请保留原文链接:http://blog.youkuaiyun.com/andyhebear/article/details/51361747
void Start() {
if (this.Button == null) {
this.Button = this.GetComponentInChildren<UI_ButtonClick>();
}
if (this.InputText == null) {
this.InputText = this.GetComponentInChildren<InputField>();
}
this.Button.OnClickEvent += Button_OnClickEvent;
this.InputText.onValidateInput = _OnValidateInput;
this.InputText.onEndEdit.AddListener(input_OnEndEdit);
}
char _OnValidateInput(string text, int charIndex, char addedChar) {
if (!_isValidChar(addedChar)) return '\0';//返回空
return addedChar;
}
//非法字符列表
private bool _isValidChar(char addedChar) {
//char[] invalid1 =System.IO.Path.GetInvalidFileNameChars();
//for (int i = 0; i < invalid1.Length; i++) {
// if (invalid1[i] == addedChar) {
// return false;
// }
//}
//char[] invalid2=System.IO.Path.GetInvalidPathChars();
//for (int i = 0; i < invalid2.Length; i++) {
// if (invalid2[i] == addedChar) {
// return false;
// }
//}
for (int i = 0; i < InvalidFilePathNameChars.Length; i++) {
if (((int)addedChar) == (int)InvalidFilePathNameChars[i]) {
return false;
}
}
for (int i = 0; i < InvalidCustomChars.Length; i++) {
if (addedChar == InvalidCustomChars[i]) {
return false;
}
}
return true;
}
/// <summary>
/// 非法文件名路径 字符char值
/// GetInvalidFileNameChars(),GetInvalidPathChars()
/// </summary>
private static readonly byte[] InvalidFilePathNameChars = new byte[]{
34,60,62,124,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,
21,22,23,24,25,26,27,28,29,30,31,58,42,63,92,47
};
/// <summary>
/// 自定义非法字符
/// </summary>
private static readonly char[] InvalidCustomChars = new char[]{
'\'','"','~','`','!','@','#','$','%','^','&','*','(',')','+','=','>','<',
'|','{','}','/','\\',':',';',',','?'
};
/// <summary>
/// 判断字符的Unicode值是否是汉字
/// </summary>
/// <param name="code">字符的Unicode</param>
/// <returns></returns>
protected static bool IsChineseLetter(int code) {
int chfrom = System.Convert.ToInt32("4e00", 16); //范围(0x4e00~0x9fff)转换成int(chfrom~chend)
int chend = System.Convert.ToInt32("9fff", 16);
if (code >= chfrom && code <= chend) {
return true; //当code在中文范围内返回true
}
else {
return false; //当code不在中文范围内返回false
}
return false;
}