挂载在需要屏蔽 emoji 的文本 Input 组件上即可.
核心处理代码就是对于输入单字符的剔除处理.
已经测试过 ios 和安卓设备,均可以生效.
可以有效禁止不可预期的 Emoji 输入.

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
using System.Globalization;
using UnityEngine;
using UnityEngine.UI;

public class InputMaskEmoji : MonoBehaviour
{
/// <summary>
/// 需要屏蔽Emoji的输入框
/// </summary>
private InputField m_inputField;

private void Awake()
{
m_inputField = gameObject.GetComponent<InputField>();

if (m_inputField == null)
{
LogUtils.LogError("[m_inputField] can't find:" + this.gameObject.name);
}
else
{
m_inputField.onValidateInput += (_text, _index, _addedChar) =>
{
// Filter out the Unicode categories you want (example below),
// keep in mind that, while this can filter out Emojis,
// it can also filter out chars you might want to keep
// (e.g. Asiatic languages, which I don't need to care about right now),
// so this is not a perfect solution.
var _unicodeCategory = char.GetUnicodeCategory(_addedChar);

switch (_unicodeCategory)
{
case UnicodeCategory.OtherSymbol:
case UnicodeCategory.Surrogate:
case UnicodeCategory.ModifierSymbol:
case UnicodeCategory.NonSpacingMark:
{
return char.MinValue;
}
default:
{
return _addedChar;
}
}
};
}
}
}