多语言展示
当前在线:1825今日阅读:2今日分享:38

Unity 实用教程 之 数据的合法性屏蔽字检测

Unity 实用教程 之 数据的合法性屏蔽字检测。本节介绍,在Unity开发中,用户输入的数据进行必要的合法性屏蔽字检测,包括用户名、密码等,具体如下
工具/原料

Unity

方法/步骤
1

打开Unity,新建一个空工程,具体如下图

2

在场景中,新建一个 InputField,和 Text,布局设置具体如下图

3

在工程中,新建一个脚本,命名为 ShieldedWordData,然后打开脚本,进行编辑,具体如下图

4

ShieldedWordData 脚本具体代码和代码解释如下图

5

ShieldedWordData  脚本具体内容如下:using System.Collections.Generic;using System.Text.RegularExpressions;public class ShieldedWordData{    //把该类设置为单例    protected static ShieldedWordData _Instance;    public static ShieldedWordData Instance()    {        //单例有则返回,没有则 new 一个并返回        return _Instance ?? (_Instance = new ShieldedWordData());    }    private List _regexList;    //用于后期屏蔽字的拆分,注意后期屏蔽使用 | 隔开    public void Check(string Id2Desc)    {        this._regexList = new List();        string txt = Id2Desc;        string[] lines = txt.Split('|');        for (int i = 0; i < lines.Length; i++)        {            string word = lines[i];            Regex reg = new Regex(word);            this._regexList.Add(reg);        }    }    //检查输入文本中是否包含屏蔽字    public bool isWord(string name)    {        int len = this._regexList.Count;        for (int i = 0; i < len; i++)        {            Regex regex = this._regexList[i];            if (regex.IsMatch(name))            {                return false;            }        }        return true;    }}

6

回到Unity界面,在工程中添加 LitJson插件(网址输入:download.csdn.net/download/u/10340009,可进行下载),并新建一个脚本 Test ,然后打开脚本,具体如下图

7

Test 脚本具体代码和代码解释如下图

8

Test 脚本具体内容如下:using LitJson;using System.Linq;using UnityEngine;using UnityEngine.UI;public class Test : MonoBehaviour{    public TextAsset txt;    public InputField testTxt;    public Text resultTxt;    void Awake()    {        JsonData data = JsonMapper.ToObject(txt.text);        ShieldedWordData.Instance().Check(data[0].ToString());    }    private void Update()    {        if (Input.GetKeyDown(KeyCode.A)) {            if (testTxt.text != null) {                //检测输入框的有效性,并包括特殊字符                bool isValid = CheckStringValid(testTxt.text, true);                if (isValid)                {                    resultTxt.text = '输入字符串合法';                }                else {                    resultTxt.text = '输入字符串不合法';                }            }        }    }    ///

    /// 监测特殊字符    ///     ///     ///     public static bool ValidCheckSpecialChara(string value)    {        string InValidChara = '\''{}[]:,';        for (int i = 0; i < InValidChara.Length; i++)        {            char chara = InValidChara[i];            if (value.Contains(chara))            {                return true;            }        }        return false;    }    ///     /// 检查是否含有非法字符    ///     ///     ///     public static bool CheckStringValid(string value, bool ignoreSpecialCharac = false)    {        if (!ignoreSpecialCharac && !ValidCheckSpecialChara(value))        {            return false;        }        if (!ShieldedWordData.Instance().isWord(value))        {            return false;        }        return true;    }}

9

脚本编译正确,回到Unity界面,在场景中 添加一个 GameObject ,并把脚本挂载上去,并赋值,具体如下图

10

其中,Txt 的具体内容如下(用来检测是否包含此类屏蔽字,可以自行添加):{'word':'Bad|Girl'}

11

运行场景,对应输入,具体结果如下图

12

到此,《Unity 实用教程 之 数据的合法性屏蔽字检测》讲解结束,谢谢

注意事项

您的支持,是我们不断坚持知识分享的动力,若帮到您,还请帮忙投票有得;若有疑问,请留言

推荐信息