본문 바로가기
개발/C#

유니티 C# 비속어 필터 적용 inputfield 간단 사용법

by SPNK 2022. 7. 14.
반응형

유니티 C# 비속어 필터 적용 inputfield 간단 사용법

 

Assets / Resoures / BadWord.txt 준비

BadWord.txt
0.02MB

 


코드 작성

using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using UnityEngine;
using UnityEngine.UI;
public class Example : MonoBehaviour
{
    public InputField inputField;

    public string[] lines;
    string LINE_SPLIT_RE = @"\r\n|\n\r|\n|\r";

    void Awake()
    {
        if (File.Exists(file))
        {
            StreamReader word = new StreamReader(file);
            source = word.ReadToEnd();
            word.Close();

            lines = Regex.Split(source, LINE_SPLIT_RE);
        }
    }

    public void ChangeNickName()
    {
        for (int i = 0; i < lines.Length; i++)
        {
            if (inputField.text.Contains(lines[i]))
            {
                Debug.Log("비속어는 사용할 수 없습니다.");
                return;
            }
        }

        string Check = Regex.Replace(inputField.text, @"[^a-zA-Z0-9가-힣 ]", "", RegexOptions.Singleline);

        if (inputField.text.Equals(Check) == true)
        {
            Debug.Log("닉네임이 변경되었습니다.");
        }
        else
        {
            Debug.Log("특수문자는 사용할 수 없습니다.");
        }
    }
}

참고할만한 글

 

유니티 C# 리소스 로드 Resource.Load 함수 간단 사용법

코드 작성 using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class ResourceLoad : MonoBehaviour { void Start() { GameObject obj = Resources.Load("GameObject") as GameObject; Sprite

parksh3641.tistory.com

 

유니티 C# 부모 자식 오브젝트 가져오기 GetChild 간단 사용법

자식 오브젝트는 씬에서 활성화되어 있어야합니다 코드 작성 (GetComponentsInChildren 활용) using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class GetChild : Mo

parksh3641.tistory.com

 

반응형

댓글