[UNITY] UI TEXT MESH PRO, 점수, 자원 표시하기

     


게임에서는 이런 저런 이유로 텍스트를 표시해야하는 경우가 많다. 스타크래프트만 봐도 화면 오른쪽 상단에 미네랄과 가스, 인구의 숫자가 텍스트로 표시되어 있다. UNITY에서 간단하게 텍스트 UI를 사용하는 방법이 있다. 그냥 TEXT를 사용하면 밋밋하니까, TEXT MESH PRO 라는 강력한 툴을 사용해서 폰트도 멋들어지게 바꿔보자.


일단 최근 UNITY에는 TEXT MESH PRO 라이브러리가 깔려있는것 같다. 따로 설치는 안해도 되지만 프로젝트에 사용하려면 프로젝트에 설치하는 과정이 있긴 하다.



Mac용 Unity 기준으로 Window 메뉴에 위와같이 TextMeshPro(이하 TMPro) 탭이 있다. TMPro는 기존 폰트파일을 사용할 수 없고 따로 바꿔줘야 하는데, Font Asset Creator로 만들 수 있다. 만약 패키지에 TMPro를 설치하지 않았다면 설치하는 창이 뜰것이다. 버튼이 두 개 보일텐데 하나는 Import TMP Essential Resources 이고 다른 하나는 Import TMP Examples and Extras이다.(그렇다. 위 메뉴에도 있다 'ㅁ')


Font Asset Creator를 실행시키면 아래와 같은 화면이 나타난다.



여기서 우리가 만들고자 하는 폰트를 사각형 박스쪽으로 드래그하면 된다. 괜찮은 무료폰트는 dafont.com에서 구할 수 있다. 폰트를 드래그한다음 Generate Font Atlas를 하면 TMPro에서 사용할 수 있는 폰트를 만들어준다.



위와같이 만들어졌다면 Save를 눌러 TMPro용 폰트를 저장한다. 아마 뒤에 SDF라는 단어가 붙을 것이다.



이제 Scene에 UI->TextMeshPro - Text객체를 생성한다. 생성하면 기본적인 텍스트 뷰가 생기는데 TextMeshProUGUI라는 Component를 가지고 있다.



만들어진 TMPro 폰트를 TextMeshProUGUI component에 있는 Font Asset에 드래그 하면 끝!


자 이제 폰트가 맛깔나게 나오는 것을 볼 수 있다. 그 밖에도 TextMeshProUGUI component에 있는 여러 옵션으로 폰트를 꾸밀 수 있다.


이제 만든 TMPro의 TEXT를 scripts에서 컨트롤 해보자


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
 
public class Resource : MonoBehaviour
{
    TextMeshProUGUI resourceText;
    private int resource;
    // Start is called before the first frame update
    void Start()
    {
        resourceText = GetComponent<TextMeshProUGUI>();
        resource = 500;
    }
 
    // Update is called once per frame
    void Update()
    {
        resourceText.text = resource.ToString();
    }
 
 
}
cs


소스는 생각보다 간단하다. using TMPro를 선언하고 TextMeshProUGUI객체를 만들어서 .text에다가 원하는 값을 집어넣으면 끝!


이제 public 메소드를 몇개 만들어서 밖에서 이 텍스트를 수정할 수 있게 만들어 보자.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
 
public class Resource : MonoBehaviour
{
    TextMeshProUGUI resourceText;
    private int resource;
    // Start is called before the first frame update
    void Start()
    {
        resourceText = GetComponent<TextMeshProUGUI>();
        resource = 500;
    }
 
    // Update is called once per frame
    void Update()
    {
        resourceText.text = resource.ToString();
    }
 
    public int GetResource()
    {
        return resource;
    }
 
    public void AddResource(int addition)
    {
        resource += addition;
    }
 
}
cs


예압. 이제 Get과 Add 메소드를 호출해서 resource를 가져오거나 수정할 수 있고, update()에서 변동되는 값을 계속해서 보여줄 것이다. 진짜 끝!









반응형

댓글

Designed by JB FACTORY