수업 중 진행된 내용은 생략한다. 추가적으로 구현한 기능만 기술함
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class ScoreUI : MonoBehaviour
{
[SerializeField] TMP_Text scoreOutput;
private void OnEnable()
{
SetScore();
}
private void Update()
{
}
void SetScore()
{
scoreOutput.text = $"SCORE: {GameManager.instance.score}";
}
}
UI의 OnEnabled() 가 GameManager의 Awake()보다 더 빨라서 instance가 존재하지 않는다스코어UI를 비활성화로 세팅하고, 게임 시작에 맞춰서 활성화 해서 표시하도록 한다public class ScoreUI : MonoBehaviour
{
[SerializeField] TMP_Text scoreOutput;
// 1초마다 점수 늘리기
YieldInstruction oneSecond;
Coroutine ScoreUpRoutine;
private void Awake()
{
oneSecond = new WaitForSeconds(1f);
}
private void OnEnable()
{
SetScore();
if (ScoreUpRoutine == null)
{
ScoreUpRoutine = StartCoroutine(ScoreUp());
}
}
private void OnDisable()
{
if (ScoreUpRoutine != null)
{
StopCoroutine(ScoreUpRoutine);
ScoreUpRoutine = null;
}
}
void SetScore()
{
if(GameManager.instance.maxScore!= 0)
{
scoreOutput.text = $"TOP SCORE: {GameManager.instance.maxScore}\nSCORE: {GameManager.instance.score}";
}
else
{
scoreOutput.text = $"SCORE: {GameManager.instance.score}";
}
}
IEnumerator ScoreUp()
{
// 게임 오버이면 점수가 오르지 않는다
while(!GameManager.instance.IsGameOver)
{
SetScore();
yield return oneSecond;
// 1초마다 점수 증가
GameManager.instance.score += 10;
}
}
}



스코어 위에 표현한다public class RecordUI : MonoBehaviour
{
[SerializeField] TMP_Text recordOutput;
private void OnEnable()
{
PrintRecord();
}
void PrintRecord()
{
if(GameManager.instance.score == GameManager.instance.maxScore)
{
recordOutput.text = $"NEW RECORD!!!\n{GameManager.instance.score}";
}
else
{
recordOutput.text = $"SCORE: {GameManager.instance.score}";
}
}
}
GameManager 스크립트 수정// 게임 끝낫을 때 작업 = 게임오버
public void GameOver()
{
// 게임오버 로직
IsGameOver = true;
// 플레이어 비활성화
playerObject.SetActive(false);
// 게임 오버 화면 띄우기
gameOverPanel.SetActive(true);
// 스코어 화면 비활성화
scoreText.SetActive(false);
// 최고 점수 갱신시 최신화
if (score > maxScore)
{
maxScore = score;
}
// 스코어 초기화
score = 0;
// hp 상태 비활성화
hpText.SetActive(false);
}
New Record가 된다OnHPChanged.AddListener(HPUI.PrintHP(playerHP));
public class HPUI : MonoBehaviour
{
[SerializeField] TMP_Text HPText;
// 체력을 받기 위한 필드
[SerializeField] GameObject playerObject;
private PlayerController player;
private int hPCount;
private void OnEnable()
{
player = playerObject.GetComponent<PlayerController>();
hPCount = player.PlayerHP;
PrintHP(hPCount);
}
private void Update()
{
if(hPCount != player.PlayerHP)
{
PrintHP(player.PlayerHP);
hPCount = player.PlayerHP;
}
}
public void PrintHP(int hp)
{
HPText.text = $"HP: {hp}";
}
}

private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Bullet") && playerHP > 0)
{
if (invincibleRoutine == null)
{
invincibleRoutine = StartCoroutine(TakeHit());
}
}
}
IEnumerator TakeHit()
{
playerHP--;
if (playerHP == 0)
{
GameManager.instance.OnGameOver.Invoke();
}
yield return invincibleTime;
invincibleRoutine = null;
}



