Unity 강의 8일차 (4) - 난이도 설정
본 강의는 다음 문서를 참고하여 제작하였습니다. 자세한 내용은 하단 링크를 참조하시거나 댓글로 질문 남겨주시면 성심성의껏 답변 드리겠습니다.
5.4강 - 난이도 설정 - Unity Learn
개요: 이제 마지막 수업입니다. 게임을 완성하기 위해 메뉴와 타이틀 화면 등을 추가합니다. 나만의 타이틀을 제작하고 스타일을 적용하여 텍스트를 보기 좋게 표현해 보겠습니다. 또한 게임 난
learn.unity.com
게임 시작 화면을 만들고 난이도 설정을 제작해봅시다.
GameOver 텍스트를 Ctrl + D 로 복사한 뒤, Title Text로 이름을 변경합니다.
또한 폰트와 텍스트를 변경하여 게임 타이틀로 변경해봅시다.
이제 Restart 버튼을 3개 복사하여 각각 Easy Buttton, Mideum Button, Hard Button으로 제작합니다.
새 스크립트 DifficultyButton를 생성하고 스크립트를 다음과 같이 작성합니다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class DifficultyButton : MonoBehaviour
{
private Button button;
void Start()
{
button = GetComponent<Button>();
button.onClick.AddListener(SetDifficulty);
}
void SetDifficulty()
{
Debug.Log(gameObject.name + " was clicked");
}
// Update is called once per frame
void Update()
{
}
}
이제 각 난이도 버튼의 On Click에 할당되어있는 Restart()는 - 버튼을 눌러 제거하고, DifficultyButton 스크립트 컴포넌트를 각 버튼에 추가합니다.
이제 Canvas에 빈 오브젝트를 추가 한 뒤, 이름을 Title Screen로 변경합니다. 안에 타이틀 텍스트와 3개의 버튼을 자식으로 옮깁니다.
각 난이도 버튼은 게임 시작 버튼과 같은 역할입니다.
즉, 버튼을 누르면 게임이 시작되야 하고, 해당 기능은 GameManager 스크립트에 속해있습니다. GameManager 스크립트를 다음과 같이 수정합니다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class GameManager : MonoBehaviour
{
public List<GameObject> targets;
private int score;
public TextMeshProUGUI scoreText;
public TextMeshProUGUI gameOverText;
private float spawnRate = 1.0f;
public bool isGameActive;
public Button restartButton;
public GameObject titleScreen;
void Start()
{
}
public void StartGame()
{
isGameActive = true;
score = 0;
StartCoroutine(SpawnTarget());
UpdateScore(score);
titleScreen.gameObject.SetActive(false);
}
IEnumerator SpawnTarget()
{
while (isGameActive)
{
yield return new WaitForSeconds(spawnRate);
int index = Random.Range(0, targets.Count);
Instantiate(targets[index]);
}
}
public void UpdateScore(int scoreToAdd)
{
score += scoreToAdd;
scoreText.text = "Score : " + score;
}
public void GameOver()
{
gameOverText.gameObject.SetActive(true);
isGameActive = false;
restartButton.gameObject.SetActive(true);
}
public void RestartGame()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
// Update is called once per frame
void Update()
{
}
}
단지 게임이 시작 될 때 시작되던 것들을 Start 버튼이 눌렸을 때 시행되도록 하기 위해서 Start() 안에 있던 기능들을 StartGame() 함수를 만들어 옮긴 것 입니다.
또한 titleScreen 을 사용해서 게임이 시작 될 때 시작 화면이 꺼지도록 하였습니다.
GameManager 스크립트에 TitleScreen 게임 오브젝트를 할당합니다.
DifficultyButton 스크립트를 다음과 같이 수정합니다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class DifficultyButton : MonoBehaviour
{
private Button button;
private GameManager gameManager;
void Start()
{
button = GetComponent<Button>();
button.onClick.AddListener(SetDifficulty);
gameManager = GameObject.Find("GameManager").GetComponent<GameManager>();
}
void SetDifficulty()
{
Debug.Log(gameObject.name + " was clicked");
gameManager.StartGame();
}
// Update is called once per frame
void Update()
{
}
}
이제 게임이 시작되면 난이도 버튼들에는 GameManager 스크립트와 연관관계를 가지도록 만듭니다. 또한 SetDifficulty가 실행 되면 게임이 시작되도록 하였습니다.
이제 난이도를 고르고, 난이도 버튼에 게임시작 함수를 할당해보겠습니다.
DifficultyButton 스크립트를 다음과 같이 수정합니다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class DifficultyButton : MonoBehaviour
{
private Button button;
private GameManager gameManager;
public int difficulty;
void Start()
{
button = GetComponent<Button>();
button.onClick.AddListener(SetDifficulty);
gameManager = GameObject.Find("GameManager").GetComponent<GameManager>();
}
void SetDifficulty()
{
Debug.Log(gameObject.name + " was clicked");
gameManager.StartGame(difficulty);
}
// Update is called once per frame
void Update()
{
}
}
GameManager의 StartGame 은 인자를 전달받지 않기 때문에 오류가 날 것입니다.
GameManager 스크립트의 StartGame()를 다음과 같이 수정합니다.
public void StartGame(int difficulty)
{
isGameActive = true;
score = 0;
StartCoroutine(SpawnTarget());
UpdateScore(score);
titleScreen.gameObject.SetActive(false);
spawnRate /= difficulty;
}
이제 각 버튼에 difficulty 프로퍼티를 잘 조절하여 게임을 실행해봅니다. (5는 난이도가 매우 높네요..!)