Tüm Rehberler

Flappy Bird Klonu — Sıfırdan Bitişe

Başlangıç ⏱ 90 dk
BÖLÜM 8 / 11

GameManager — Oyun Durumu Yönetimi

Oyun başlangıcı, devam hali ve oyun sonu durumlarını yöneten merkezi GameManager scriptini yazıyoruz.

Application.Quit() Editor'da çalışmaz, sadece build'da.

Unity Editör Adımları

'+' > Create Empty → 'GameManager'
Canvas altına GameOverPanel (Restart + Quit butonları)
Panel başlangıçta gizle (SetActive false)
csharp
using UnityEngine;
using UnityEngine.SceneManagement;

public class GameManager : MonoBehaviour
{
    public static GameManager Instance;
    public GameObject gameOverPanel;
    public GameObject startPanel;
    private bool isPlaying;

    void Awake() => Instance = this;

    void Start()
    {
        Time.timeScale = 0f;
        startPanel.SetActive(true);
        gameOverPanel.SetActive(false);
    }

    public void StartGame()
    {
        isPlaying = true;
        Time.timeScale = 1f;
        startPanel.SetActive(false);
    }

    public void GameOver()
    {
        isPlaying = false;
        Time.timeScale = 0f;
        gameOverPanel.SetActive(true);
        int cur = ScoreManager.Instance.GetScore();
        int best = PlayerPrefs.GetInt("BestScore", 0);
        if (cur > best) PlayerPrefs.SetInt("BestScore", cur);
    }

    public void RestartGame()
    {
        Time.timeScale = 1f;
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
    }

    public void QuitGame() => Application.Quit();
    public bool IsPlaying() => isPlaying;
}

Time.timeScale = 0f oyunu tamamen dondurur.

Önceki Bölüm Sonraki Bölüm