Menu
CoddyTech

최고 기록

Coddy Unity 여정의 실제 게임 만들기 섹션에 포함된 레슨. 60개 중 33번째.

Endless Runner의 마지막 단계: 최고 기록은 최고 점수가 저장되었던 방식과 마찬가지로 PlayerPrefs에 저장되므로, 게임을 다시 실행하거나 종료해도 유지됩니다.

challenge icon

챌린지

중급

최고 기록을 BestRun 키에 저장하세요.

  1. private int best;를 추가하세요. Start()에서 PlayerPrefs.GetInt("BestRun", 0)으로 값을 불러오고 bestText에 "Best: " + best + " m"을 표시하세요.
  2. GameOver()의 끝에서 Mathf.FloorToInt(distance)가 best보다 크면 값을 저장하고 표시한 다음 newBestText.SetActive(true);를 호출하세요.

‘플레이’를 누르고 최고 기록을 세운 다음 그 기록을 깨 보세요. New best!가 표시됩니다. 그런 다음 ‘확인’을 누르세요.

직접 해보기

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using TMPro;

public enum GameState { Playing, GameOver }

public class GameManager : MonoBehaviour
{
    public static GameManager Instance;

    public float speed = 10f;
    public float speedUp = 0.5f;
    public float maxSpeed = 20f;
    public TMP_Text distanceText;
    public GameObject gameOverPanel;
    public Button restartButton;
    public GameState state = GameState.Playing;
    public TMP_Text bestText;
    public GameObject newBestText;
    private float distance;

    void Awake()
    {
        Instance = this;
    }

    void Start()
    {
        restartButton.onClick.AddListener(Restart);
    }

    void Update()
    {
        if (state != GameState.Playing) return;

        speed = Mathf.Min(speed + speedUp * Time.deltaTime, maxSpeed);
        distance += speed * Time.deltaTime;
        distanceText.text = "Distance: " + Mathf.FloorToInt(distance) + " m";
    }

    public void GameOver()
    {
        state = GameState.GameOver;
        speed = 0f;
        gameOverPanel.SetActive(true);
    }

    void Restart()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
    }
}
quiz icon실력 점검

이 레슨에는 짧은 퀴즈가 포함되어 있습니다. 레슨을 시작해 문제를 풀고 진행 상황을 기록하세요.

실제 게임 만들기의 모든 레슨

직접 연습해 보세요: Unity 플레이그라운드