유니티

유니티 공부(9)

MJ_119 2023. 9. 12. 18:28

출처 : 골드메탈 유니티 뱀서라이크 10

 

HUD 제작하기

 

1. UI 캔버스 - 캔버스 + 캔버스 스케일러 꼭 조절하기

 

2. 스크립트 준비

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

public class HUD : MonoBehaviour
{
    // 열거형 경험치, 레벨, 킬수, 시간, HP
    public enum InfoType {Exp, Level, Kill, Time, Health }
    public InfoType type; // 열거형을 담을 변수.

    Text myText;
    Slider mySlider;

    void Awake()
    {
        mySlider = GetComponent<Slider>();
        myText = GetComponent<Text>();
    }

    void LateUpdate()
    {
        switch (type)
        {
            case InfoType.Exp:

                break;
            case InfoType.Level:

                break;
            case InfoType.Kill:

                break;
            case InfoType.Time:

                break;
            case InfoType.Health:

                break;
        }
    }
}

3. 경험치 게이지

슬라이더에 적용할 값 : 현재 경험치 / 최대 경험치

 

4. 레벨 및 킬수 텍스트

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

public class HUD : MonoBehaviour
{
    // 열거형 경험치, 레벨, 킬수, 시간, HP
    public enum InfoType {Exp, Level, Kill, Time, Health }
    public InfoType type; // 열거형을 담을 변수.

    Text myText;
    Slider mySlider;

    void Awake()
    {
        mySlider = GetComponent<Slider>();
        myText = GetComponent<Text>();
    }

    void LateUpdate()
    {
        switch (type)
        {
            case InfoType.Exp:
                float curExp = GameManager.instance.exp; // 현재 경험치
                float maxExp = GameManager.instance.nextExp[GameManager.instance.level]; // 최대 경험치
                mySlider.value = curExp / maxExp;

                break;
            case InfoType.Level:
                myText.text = String.Format("Lv.{0:F0}", GameManager.instance.level);
                break;
            case InfoType.Kill:
                myText.text = String.Format("{0:F0}", GameManager.instance.kill);
                break;
            case InfoType.Time:

                break;
            case InfoType.Health:

                break;
        }
    }
}

5. 타이머 텍스트

float remainTime = GameManager.instance.maxGameTime - GameManager.instance.gameTime;
                int min = Mathf.FloorToInt(remainTime / 60);
                int sec = Mathf.FloorToInt(remainTime % 60);

6. 체력 게이지

float curHealth = GameManager.instance.health; // 현재 경험치
float maxHealth = GameManager.instance.maxHealth; // 최대 경험치
mySlider.value = curHealth / maxHealth;

체력 게이지가 캐릭터를 따라가도록 하는 스크립트

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

public class Follow : MonoBehaviour
{
    RectTransform rect;
    void Awake()
    {
        rect = GetComponent<RectTransform>();
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        rect.position = Camera.main.WorldToScreenPoint(GameManager.instance.player.transform.position); // WorldToScreenPoint : 월드 상의 오브젝트 위치를 스크린 좌표로 변환
    }
}