Unity InputField IOS 繁体中文踩坑 附带各种常用语言unicode在代码中
背景
项目中需要去掉某些不能输入的字符,比如说emoji。但是单纯只是屏蔽emoji的unicode,是远不能处理干净的。于是考虑反向操作,控制可以输入的字符,于是就踩到了繁体中文注音输入法的坑
现象
- 完全不处理的话,注音输入法是无法打字的
- 简单处理后,声调无法使用
解决方法
- 先允许unicode范围 0x3100~0x312f
- 这是标准的注音范围
- 再允许unicode范围 0x31a0~0x31bf
- 这是扩展的注音范围
- 最后单独允许 0x02ca, 0x02c7, 0x02cb, 0x02d9, 0x02d8, 0x02c9
- 这是注音的声调
代码
public class MInputField : InputField
{
private static readonly List<int> s_supportPunctuation = new()
{
0x3002, 0xff0c, 0x3001, 0xff1a,
0xff1b, 0xff1f, 0xff01,
0x201c, 0x201d, 0x2018, 0x2019,
0x300a, 0x300b, 0x2026, 0x2014,
0xff08, 0xff09, 0x3010, 0x3011,
0x300c, 0x300d, 0x300e, 0x300f,
};
private static readonly List<char> s_notSupportChar = new()
{
'\n', '\r', '\t', '\\','\'','\"','\0','\a','\b','\f','\v',
};
protected override void Awake()
{
base.Awake();
onValidateInput += IsSupportChar;
}
private char IsSupportChar(string inputText, int charIndex, char addedChar)
{
// var str = $"{addedChar}";
var result = IsSupport(addedChar);
if (!result)
{
//log
}
return !result ? char.MinValue : addedChar;
}
private bool IsSupport(char inputChar)
{
if (s_notSupportChar.Contains(inputChar))
{
//不支持的字符
return false;
}
if (char.IsWhiteSpace(inputChar))
{
//空格
return true;
}
if (inputChar >= 0x00 && inputChar <= 0xff)
{
//ASCII
return true;
}
if ((inputChar >= 0x3040 && inputChar <= 0x309F) || (inputChar >= 0x30A0 && inputChar <= 0x30FF)) //inputChar >= 0x0800 && inputChar <= 0x4e00
{
//日文
return true;
}
if ((inputChar >= 0xAC00 && inputChar <= 0xD7AF) || (inputChar >= 0x3130 && inputChar <= 0x318F) || (inputChar >= 0x1100 && inputChar <= 0x11FF)) //inputChar >= 0x3131 && inputChar <= 0xD79D || (inputChar >= 0xA960 && inputChar <= 0xA97F) || (inputChar >= 0xD7B0 && inputChar <= 0xD7FF)
{
//韩文
return true;
}
if ((inputChar >= 0x0400 && inputChar <= 0x04FF) || (inputChar >= 0x0500 && inputChar <= 0x052F)) //inputChar >= 0x0400 && inputChar <= 0x04FF
{
//俄文
return true;
}
if (inputChar >= 0x4e00 && inputChar <= 0x9fa5)
{
//中文
return true;
}
if (s_supportPunctuation.Contains(inputChar))
{
//标点
return true;
}
if((inputChar == 0x02ca) || (inputChar == 0x02c7) || (inputChar == 0x02cb) || (inputChar == 0x02d9) || (inputChar == 0x02d8) || (inputChar == 0x02c9))
{
//注音声调
return true;
}
if ((inputChar >= 0x3100 && inputChar <= 0x312f) || (inputChar >= 0x31a0 && inputChar <= 0x31bf))
{
//注音符号
return true;
}
if (inputChar >= 0x2f00 && inputChar <= 0x2f03)
{
//笔画输入
return true;
}
return false;
}
}