유니티 폰트 변경 - yuniti ponteu byeongyeong

https://noonnu.cc/

눈누 - 상업용 무료한글폰트 사이트

상업적으로 이용할 수 있는 무료 한글 폰트를 모아 놓은 사이트 눈누

noonnu.cc

유니티 폰트 변경 - yuniti ponteu byeongyeong

유니티는 기본적으로 한국어로 된 폰트를 지원하지 않는다. 영어만 가능하게 디폴트 세팅되어 있기 때문에 한국어로 된 텍스트를 사용해주려면 몇가지 설정을 해주어야 한다.

먼저 한국어 폰트(확장자 OTF,TTF 등)를 다운로드 받아주고 유니티 상으로 임포트해준다. 

유니티 폰트 변경 - yuniti ponteu byeongyeong
유니티 폰트 변경 - yuniti ponteu byeongyeong

가져온 폰트 소스를 우클릭 - Create - TextMeshPro - Font Asset을 설정해주면 적용할 수 있게 폰트 에셋이 바뀐다.

유니티 폰트 변경 - yuniti ponteu byeongyeong

텍스트를 작성해주려면 씬 상에서 Text를 가져와야 하는데, UI에 보면 Text와 Text Mesh Pro(TMP) 2개가 있다. 해당 텍스트에 파란색 폰트 애셋을 매핑해주면 한글이 적용되는 것을 확인할 수 있다.

유니티 폰트 변경 - yuniti ponteu byeongyeong

다만 Text/TMP는 차이가 있다. 일반 Text(위)는 한국어 폰트 적용이 되긴 하지만 화질이 매우 깨져보인다. 반면, TMP(아래)로 제작한 한국어 폰트는 깔끔한 것을 확인할 수 있다.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;

public class testText : MonoBehaviour
{
    private TMP_Text m_TextComponent;
    private bool hasTextChanged;
    public float WaitTime1;
    public bool LoopFlg;
    bool LoopCheck;
    void Awake()
    {
        LoopCheck = true;
        m_TextComponent = gameObject.GetComponent<TMP_Text>();
    }


    void Start()
    {
        StartCoroutine(RevealCharacters(m_TextComponent));       
    }


    void OnEnable()
    {
        // Subscribe to event fired when text object has been regenerated.
        TMPro_EventManager.TEXT_CHANGED_EVENT.Add(ON_TEXT_CHANGED);
    }

    void OnDisable()
    {
        TMPro_EventManager.TEXT_CHANGED_EVENT.Remove(ON_TEXT_CHANGED);
    }


    // Event received when the text object has changed.
    void ON_TEXT_CHANGED(Object obj)
    {
        hasTextChanged = true;
    }


    /// <summary>
    /// Method revealing the text one character at a time.
    /// </summary>
    /// <returns></returns>
    IEnumerator RevealCharacters(TMP_Text textComponent)
    {
        textComponent.ForceMeshUpdate();

        TMP_TextInfo textInfo = textComponent.textInfo;

        int totalVisibleCharacters = textInfo.characterCount; // Get # of Visible Character in text object
        int visibleCount = 0;

        while (LoopCheck)
        {
            if (hasTextChanged)
            {
                totalVisibleCharacters = textInfo.characterCount; // Update visible character count.
                hasTextChanged = false;
            }

            if (visibleCount > totalVisibleCharacters)
            {
                yield return new WaitForSeconds(1.0f);
                visibleCount = 0;
                if(LoopFlg)
                {
                    LoopCheck = true;
                }
                else
                {
                    LoopCheck = false;
                }
               
            }

            textComponent.maxVisibleCharacters = visibleCount; // How many characters should TextMeshPro display?

            visibleCount += 1;

            yield return new WaitForSeconds(WaitTime1);

            
        }
    }
}
유니티 폰트 변경 - yuniti ponteu byeongyeong

추가적으로 폰트가 시간이 지남에 따라 한 글자씩 나오도록 하는 코드를 작성해 보았다. Public으로 선언된 Wait Time을 통해 시간 간격을 설정하고, Loop Flg 설정을 통해 반복 여부를 설정하면 된다.

+마우스를 클릭하면 n초 뒤 오브젝트(캔버스) 생성 코드

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class After3sec : MonoBehaviour
{
    public GameObject CanvasObj;
    public float waitSec;
    
    void Update()
    {
        if(Input.GetMouseButtonDown(0))
        {
            StartCoroutine(AfterFunc(waitSec));
        }
    }
    IEnumerator AfterFunc(float waitTime)
    {
        // A 동작
        Debug.Log("클릭 했음"); 
        yield return new WaitForSeconds(waitTime);
        // B 동작 
        CanvasObj.SetActive(true);
    }
}