unity插件,从一段文字中提取中文并去重
using System.Collections.Generic; using System.Text.RegularExpressions; using UnityEditor; using UnityEngine; public class ChineseCharPicker : EditorWindow { string inString = "在这里输入"; string outString = ""; void OnGUI() { GUILayout.Label("原始内容", EditorStyles.boldLabel); inString = EditorGUILayout.TextArea(inString); GUILayout.Label("结果", EditorStyles.boldLabel); outString = EditorGUILayout.TextArea(outString); bool button = GUILayout.Button("提取", EditorStyles.miniButton); if (button) { Debug.Log("执行成功" + Time.time); convert(); } } [MenuItem("Window/中文提取器")] public static void ShowWindow() { GetWindow(typeof(ChineseCharPicker)); } public string pick(string str) { string rex = "[^\\一-\\龥]+"; return Regex.Replace(str, rex, ""); } public void convert() { outString = delete(pick(inString)); } private string delete(string str) { char[] chars = str.ToCharArray(); HashSet<string> result = new HashSet<string>(); foreach (char c in chars) { result.Add("" + c); } string sb = ""; foreach (string str1 in result) { sb += str1; } return sb; } }
把上面代码保存为ChineseCharPicker.cs放到Assets/Editor即可