前言:
在Unity项目中,涉及文件的读写操作,还是比较常见的。所以有必要学习一下,IO流的操作。下面以 检测 游戏中的用户昵称是否含有敏感词汇 为例,进行具体实践操作。
如图,在Unity 编辑器中生成一个 文件读写工具,用来检测词库冲突问题。
代码实现:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Data;
using UnityEditor;
public class CheckStrifeWord : EditorWindow
{
private string m_nickPath = "/Text/test.txt";
private string m_fobidPath = "/Text/words.txt";
private string[] m_nickArray = null;
private string[] m_forbidArray = null;
private string[] m_tempArray = null;
string[] m_newArray = null;
private List<string> list = new List<string>();
List<string> m_testList = new List<string>();
string str = "";
// 添加 到Tool 菜单栏 : EditorWindow
[MenuItem("Tools/Check word")]
static public void OpenBMFontMaker()
{
EditorWindow.GetWindow<CheckStrifeWord>(false, "Check Word", true).Show();
}
private void OnGUI()
{
if (GUILayout.Button("词库检测"))
{
Debug.Log("\n==== 词库检测 =====");
Init(0);
}
if (GUILayout.Button("提取 昵称词库 重复字段"))
{
Debug.Log("\n==== 提取 昵称词库 重复字段 =====");
m_newArray = new string[0];
Debug.Log("\n==== 新字符串长度 =====" + m_newArray.Length);
Init(1);
}
if (GUILayout.Button("提取 敏感词库 重复字段"))
{
Debug.Log("\n==== 提取 敏感词库 重复字段 =====");
Init(2);
}
if (GUILayout.Button("提取 昵称词库 冲突敏感词组"))
{
Debug.Log("\n==== 提取 昵称词库 冲突敏感词组 =====");
Init(3