在开发网络游戏的聊天功能模块时,须要对字符长度进行限制以避免发送超长包,不过由于玩家输入了emoji后会致使长度没法正常处理,此时偷懒的办法就是限制emoji的输入git
在UGUI的InputField进行输入时执行该方法:github
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Text.RegularExpressions;
public class InputRestrictionEmoji : MonoBehaviour {网络
InputField mtext;code
private void Start()
{
mtext = GetComponent<InputField>();
if(mtext)
{
mtext.onValueChanged.AddListener(OnInputValue);
}
}blog
void OnInputValue(string value)
{
try
{
string msg = mtext.text;
//屏蔽emoji
string result = Regex.Replace(msg, @"\p{Cs}", "");
mtext.text = result;
}
catch
{
Debug.Log("输入异常文字,出错");
mtext.text = "";
}
}
}游戏
若是仍是不放心能够try-catch一下,记得还要限制一下字符长度啊开发
特别感谢:string
EmojiText https://github.com/zouchunyi/EmojiTextit
Unity-UI-emoji https://github.com/mcraiha/Unity-UI-emojiio
twemoji https://github.com/twitter/twemoji