unity/기술개발일지

3) Unity Text 모자이크 처리 - 2D 게임

rimugiri 2025. 1. 27. 03:47
728x90

Shader를 사용해 모자이크 처리를 해보았다면 mask된 글자를 사용하여 모자이크 처리를 해보자

 

예를 들으면 #$%@#%#@!의 글자에서 "안녕 나는 rimugi야" 이런글자로 특정 시간동안 변화하게 만든다면 될거라고 예상하였다

 

1. 랜덤 모자이크 생성

주목할 점은 2가지다

1. StringBuilder를 이용한 최적와

2. maskCharacter를 사용한 랜덤 모자이크 글자 생성

[SerializeField] private string[] _maskCharacters = { "#", "*", "@", "$", "%", "&", "!" }; // 특수문자 집합

private string GetRandomMaskedText(int length)
{
    StringBuilder randomText = new StringBuilder(length);
    for (int i = 0; i < length; i++)
    {
        randomText.Append(_maskCharacters[UnityEngine.Random.Range(0, _maskCharacters.Length)]);
    }
    return randomText.ToString();
}

 

 

2. 모자이크 효과

1. totalDuration의 시간에 글자가 원본 글자로 변화하는것은 보장된다

    // 텍스트 효과 
    private IEnumerator ShowText()
    {

        int textLength = _originalText.Length;
        float totalDuration = 1.0f; // 전체 변환 시간
        float delayPerChar = totalDuration / textLength; // 각 글자당 딜레이

        // StringBuilder를 사용하여 효율적으로 텍스트 구성
        StringBuilder currentText = new StringBuilder(_bubbleText.text);

        for (int i = 0; i < textLength; i++)
        {
            // 원래 텍스트의 i번째 글자를 업데이트
            currentText[i] = _originalText[i];
            
            _bubbleText.text = currentText.ToString();
            yield return new WaitForSeconds(delayPerChar);
        }
    }

 

3. 효과

 

 

 

728x90