본문 바로가기

Unity/수업내용

2020-05-25 HudTextTest

허드텍스트는 사실 개발의 영역이 아니라, 아트의 영역이지만, 코드만으로도 어느정도 구현 가능하다.

 

구현하기 위해서는 어셋스토어에서 DOTween을 다운받아 임포트한뒤 셋업하고,

 

코드 맨위에 using DG.Tweening; 추가하고 사용.

 

UIHudText는 미리만들어 프리팹화 해줌.

 

 

 

유니티 화면 셋팅

 

 

HudTextTest.class

더보기
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class HudTextTest : MonoBehaviour
{
    public UIHudTextTest uIHudTextTest;
    public Transform pivotTrans;
    private void Start()
    {
        this.uIHudTextTest.btn.onClick.AddListener(() =>
        {
            this.uIHudTextTest.ShowHud(this.pivotTrans.position, 5890);
        });
    }
}
 
 

 

UIHudText.class

더보기
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
 
public class UIHudText : MonoBehaviour
{
    public Text text;
 
    public void Init(string strNum, Vector2 targetLocalPos) {
 
        this.text.text = strNum;
        this.transform.localPosition = targetLocalPos;
    }
}
 
 
 

 

UIHudTextTest.class

더보기
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;
 
public class UIHudTextTest : MonoBehaviour
{
    public Button btn;
    public GameObject uiHudTextPrefab;
 
    //targetPos : world position
    public void ShowHud(Vector3 targetPos, int num)
    {
        var strNum = string.Format("{0:#,0}", num);
        Debug.LogFormat("{0}", strNum);
        var hudTextGo = Instantiate<GameObject>(this.uiHudTextPrefab);
 
        //월드에서 생성된 게임오브젝트를 자식으로 (Canvas)
        hudTextGo.transform.SetParent(this.transform, false);
 
        var pos = Camera.main.WorldToScreenPoint(targetPos);
        var canvasPosX = pos.x - 1920 / 2;
        var canvasPosY = pos.y - 1080 / 2;
 
        //초기 위치 설정 
        var targetLocalPos = new Vector2(canvasPosX, canvasPosY);
        var uiHudText = hudTextGo.GetComponent<UIHudText>();
        uiHudText.Init(strNum, targetLocalPos);
 
        hudTextGo.transform.localScale = Vector3.zero;
        var targetPosY = hudTextGo.transform.localPosition.y + 100;
        hudTextGo.transform.DOScale(2f, 0.5f).OnComplete(() => {
            hudTextGo.transform.DOScale(00.5f).OnComplete(() => {
 
            });
        });
 
        hudTextGo.transform.DOLocalMoveY(targetPosY, 1f)
            .SetEase(Ease.OutQuint)
            .OnComplete(() => {
                Debug.Log("move complete");
                Object.Destroy(hudTextGo);
            });
 
    }
}
 
 
 
 

 

 

 

실행화면