일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- 백준
- 2022년
- 5월
- 2023년
- 코딩 테스트
- 3월
- 개인 프로젝트
- todolist
- 수학
- 자료 구조
- 2024년
- 다이나믹 프로그래밍
- 단계별로 풀어보기
- 코딩 기초 트레이닝
- 유니티
- 2025년
- 6월
- 개인 프로젝트 - 런앤건
- 1월
- 2월
- C/C++
- 게임 엔진 공부
- 기초
- 입문
- 골드메탈
- 프로그래머스
- 7월
- c++
- 4월
- 유니티 심화과정
- Today
- Total
기록 보관소
[Unity/유니티] 기초-뱀서라이크: HUD 제작하기[10] 본문
개요
유니티 독학을 위해 아래 링크의 골드메탈님의 영상들을 보고 직접 따라 해보면서 진행 상황을 쓰고 배웠던 점을 요약한다.
https://youtube.com/playlist?list=PLO-mt5Iu5TeYI4dbYwWP8JqZMC9iuUIW2
📚유니티 기초 강좌
유니티 게임 개발을 배우고 싶은 분들을 위한 기초 강좌
www.youtube.com
뱀서라이크: HUD 제작하기[10]
1. UI 캔버스
- RectTransform : 스크린 전용 Transform 역할 컴포넌트
- Render Mode
- Screen-Overlay : 스크린에 그대로 얹는 형태
- Screen-Camera : 스크린을 카메라에 맞추는 형태
- World Space : 월드 공간에 배치하는 형태
- UI Scale Mode
- Constant Pixel Size : 해상도에 상관없이 픽셀 크기 고정
- Scale With Screen Size : 해상도에 따라 픽셀 크기 변경됨
- Constant Physical Size
- Refernce Resolution : 해상도.
- Main Camera의 Pixel Perfect Camera 컴포넌트에도 동일한 설정 값이 있다.
2. 스크립트 준비
//HUD Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI; //Text
public class HUD : MonoBehaviour {
public enum InfoType { Exp, Level, Kill, Time, Health } //열거형
public InfoType type;
Text myText;
Slider mySlider;
void Awake() {
myText = GetComponent<Text>();
mySlider = GetComponent<Slider>();
}
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. 경험치 게이지
- Interactable : 플레이어가 임의로 수정할 수 있게 하는 설정. 체크시 슬라이더의 원을 마우스로 끌어서 값 변경이 가능해짐.
- Transition : 슬라이더 값에 따라 색상 변경
- Navigation : UI의 Tab 포커싱 순서.
//HUD Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI; //Text
public class HUD : MonoBehaviour {
public enum InfoType { Exp, Level, Kill, Time, Health } //열거형
public InfoType type;
Text myText;
Slider mySlider;
void Awake() {
myText = GetComponent<Text>();
mySlider = GetComponent<Slider>();
}
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:
break;
case InfoType.Kill:
break;
case InfoType.Time:
break;
case InfoType.Health:
break;
}
}
}
4. 레벨 및 킬수 텍스트
//HUD Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI; //Text
public class HUD : MonoBehaviour {
public enum InfoType { Exp, Level, Kill, Time, Health } //열거형
public InfoType type;
Text myText;
Slider mySlider;
void Awake() {
myText = GetComponent<Text>();
mySlider = GetComponent<Slider>();
}
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); //F0, F1, F2... : 소수점 자리를 지정
break;
case InfoType.Kill:
myText.text = string.Format("{0:F0}", GameManager.instance.kill);
break;
case InfoType.Time:
break;
case InfoType.Health:
break;
}
}
}
- String.Format : 각 숫자 인자값을 지정된 형태의 문자열로 만들어주는 함수
5. 타이머 텍스트
//HUD Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI; //Text
public class HUD : MonoBehaviour {
public enum InfoType { Exp, Level, Kill, Time, Health } //열거형
public InfoType type;
Text myText;
Slider mySlider;
void Awake() {
myText = GetComponent<Text>();
mySlider = GetComponent<Slider>();
}
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); //F0, F1, F2 ... : 소수점 자리를 지정
break;
case InfoType.Kill:
myText.text = string.Format("{0:F0}", GameManager.instance.kill);
break;
case InfoType.Time:
float remainTime = GameManager.instance.maxGameTime - GameManager.instance.gameTime;
int min = Mathf.FloorToInt(remainTime / 60);
int sec = Mathf.FloorToInt(remainTime % 60);
myText.text = string.Format("{0:D2}:{1:D2}", min, sec); //D0, D1, D2 ... : 자리수를 지정
break;
case InfoType.Health:
break;
}
}
}
6. 체력 게이지
//GameManager Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameManager : MonoBehaviour {
public static GameManager instance;
[Header("# Game Control")]
public float gameTime; //게임 시간 변수
public float maxGameTime = 2 * 10f; //최대 게임 시간 변수(20초).
[Header("# Player Info")]
public int health;
public int maxHealth = 100;
public int level;
public int kill;
public int exp;
public int[] nextExp = { 3, 5, 10, 100, 150, 210, 280, 360, 450, 600 };
[Header("# Game Object")]
public PoolManager pool;
public Player player;
void Awake() {
instance = this;
}
void Start() {
health = maxHealth;
}
void Update() {
gameTime += Time.deltaTime;
if (gameTime > maxGameTime) {
gameTime = maxGameTime;
}
}
public void GetExp() {
exp++;
if (exp == nextExp[level]) {
level++;
exp = 0;
}
}
}
//HUD Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI; //Text
public class HUD : MonoBehaviour {
public enum InfoType { Exp, Level, Kill, Time, Health } //열거형
public InfoType type;
Text myText;
Slider mySlider;
void Awake() {
myText = GetComponent<Text>();
mySlider = GetComponent<Slider>();
}
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); //F0, F1, F2 ... : 소수점 자리를 지정
break;
case InfoType.Kill:
myText.text = string.Format("{0:F0}", GameManager.instance.kill);
break;
case InfoType.Time:
float remainTime = GameManager.instance.maxGameTime - GameManager.instance.gameTime;
int min = Mathf.FloorToInt(remainTime / 60);
int sec = Mathf.FloorToInt(remainTime % 60);
myText.text = string.Format("{0:D2}:{1:D2}", min, sec); //D0, D1, D2 ... : 자리수를 지정
break;
case InfoType.Health:
float curHealth = GameManager.instance.health;
float maxHealth = GameManager.instance.maxHealth;
mySlider.value = curHealth / maxHealth;
break;
}
}
}
//Follow Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Follow : MonoBehaviour {
RectTransform rect;
void Awake() {
rect = GetComponent<RectTransform>();
}
void FixedUpdate() {
rect.position = Camera.main.WorldToScreenPoint(GameManager.instance.player.transform.position); //월드 좌표와 스크린 좌표는 서로 다르므로 WorldToScreenPoint 사용.
}
}
- Camera.main.WorldToScreenPoint() : 월드 상의 오브젝트 위치를 스크린 좌표로 변환.
'유니티 프로젝트 > 뱀서라이크' 카테고리의 다른 글
[Unity/유니티] 기초-뱀서라이크: 플레이어 무기 장착 표현하기[11+] (0) | 2023.07.13 |
---|---|
[Unity/유니티] 기초-뱀서라이크: 능력 업그레이드 구현[11] (0) | 2023.07.12 |
[Unity/유니티] 기초-뱀서라이크: 타격감 있는 몬스터 처치 만들기[09] (0) | 2023.07.10 |
[Unity/유니티] 기초-뱀서라이크: 자동 원거리 공격 구현[08] (0) | 2023.06.04 |
[Unity/유니티] 기초-뱀서라이크: 회전하는 근접무기 구현[07] (0) | 2023.06.01 |