Changyu Lee

Dev Log 05.28.24 : 유니티 한글 입력 로직 구현

Published at
2024/05/28
Last edited time
2024/05/28 14:18
Created
2024/05/28 14:02
Section
Dev Log
Status
Done
Series
Tags
Code
Programming
AI summary
Keywords
Unity
Language
using System.Collections; using System.Collections.Generic; using System.Text; using UnityEngine; using UnityEngine.UI; using TMPro; public class BuddyzKeyboardInputHandler : MonoBehaviour { public TextMeshProUGUI displayText; private StringBuilder inputBuffer = new StringBuilder(); bool _isStarted= false; private readonly char[] cho = { 'ㄱ', 'ㄲ', 'ㄴ', 'ㄷ', 'ㄸ', 'ㄹ', 'ㅁ', 'ㅂ', 'ㅃ', 'ㅅ', 'ㅆ', 'ㅇ', 'ㅈ', 'ㅉ', 'ㅊ', 'ㅋ', 'ㅌ', 'ㅍ', 'ㅎ' }; private readonly char[] jung = { 'ㅏ', 'ㅐ', 'ㅑ', 'ㅒ', 'ㅓ', 'ㅔ', 'ㅕ', 'ㅖ', 'ㅗ', 'ㅘ', 'ㅙ', 'ㅚ', 'ㅛ', 'ㅜ', 'ㅝ', 'ㅞ', 'ㅟ', 'ㅠ', 'ㅡ', 'ㅢ', 'ㅣ' }; private readonly char[] jong = { '\0', 'ㄱ', 'ㄲ', 'ㄳ', 'ㄴ', 'ㄵ', 'ㄶ', 'ㄷ', 'ㄹ', 'ㄺ', 'ㄻ', 'ㄼ', 'ㄽ', 'ㄾ', 'ㄿ', 'ㅀ', 'ㅁ', 'ㅂ', 'ㅄ', 'ㅅ', 'ㅆ', 'ㅇ', 'ㅈ', 'ㅊ', 'ㅋ', 'ㅌ', 'ㅍ', 'ㅎ', ' '}; void Update() { foreach (char c in Input.inputString) { if (c == '\b') // Backspace { if (inputBuffer.Length > 0) inputBuffer.Remove(inputBuffer.Length - 1, 1); } else { inputBuffer.Append(c); } } if (_isStarted) displayText.text = ComposeHangul(inputBuffer.ToString()); } string ComposeHangul(string input) { StringBuilder composedText = new StringBuilder(); int state = 0; // 0: 기본 상태, 1: 초성 입력 중, 2: 중성 입력 중, 3: 종성 입력 중 char choChar = '\0', jungChar = '\0', jongChar = '\0'; foreach (char c in input) { if (state == 0) { if (IsCho(c)) { choChar = c; state = 1; } else { composedText.Append(c); } } else if (state == 1) { if (IsJung(c)) { jungChar = c; state = 2; } else { composedText.Append(choChar); if (IsCho(c)) { choChar = c; } else { composedText.Append(c); state = 0; } } } else if (state == 2) { if (IsJong(c)) { jongChar = c; state = 3; } else { composedText.Append(ComposeSyllable(choChar, jungChar, '\0')); if (IsCho(c)) { choChar = c; state = 1; } else { composedText.Append(c); state = 0; } } } else if (state == 3) { if (IsCho(c)) { composedText.Append(ComposeSyllable(choChar, jungChar, jongChar)); choChar = c; state = 1; } else if (IsJung(c)) { composedText.Append(ComposeSyllable(choChar, jungChar, '\0')); // 이전 초 + 중은 합쳐서 보냄 if (IsCho(jongChar)) choChar = jongChar; jungChar = c; state = 2; } else // 종성이 들어왔다면 새로운 대기 상태 { composedText.Append(ComposeSyllable(choChar, jungChar, jongChar)); composedText.Append(c); state = 0; } } } if (state == 2) { composedText.Append(ComposeSyllable(choChar, jungChar, '\0')); } else if (state == 3) { composedText.Append(ComposeSyllable(choChar, jungChar, jongChar)); } else if (state == 1) { composedText.Append(choChar); } return composedText.ToString(); } bool IsCho(char c) { foreach (char choChar in cho) { if (c == choChar) return true; } return false; } bool IsJung(char c) { foreach (char jungChar in jung) { if (c == jungChar) return true; } return false; } bool IsJong(char c) { foreach (char jongChar in jong) { if (c == jongChar) return true; } return false; } char ComposeSyllable(char cho, char jung, char jong) { int choIndex = System.Array.IndexOf(this.cho, cho); int jungIndex = System.Array.IndexOf(this.jung, jung); int jongIndex = System.Array.IndexOf(this.jong, jong); if (jong != ' ' || jong == '\0') { int syllable = 0xAC00 + (choIndex * 21 * 28) + (jungIndex * 28) + jongIndex; return (char)syllable; } else { int syllable = 0xAC00 + (choIndex * 21 * 28) + (jungIndex * 28); return (char)syllable; } } public void InputChar(string str) { if (str == "back") // Backspace { if (this.inputBuffer.Length > 0) this.inputBuffer.Remove(this.inputBuffer.Length - 1, 1); return; } // string to char var c = str.ToCharArray(); this.inputBuffer.Append(c); _isStarted = true; } }
C#
복사