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