Menu
CoddyTech

اركض مجددًا

جزء من قسم إنشاء ألعاب حقيقية في رحلة Unity على Coddy. الدرس 32 من 60.

challenge icon

التحدي

متوسط

اجعل زر «العب مجددًا» يبدأ جولة جديدة.

  1. في GameManager، اكتب void Restart() لإعادة تحميل المشهد: SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);.
  2. أضف void Start() مع restartButton.onClick.AddListener(Restart);.

اضغط على «العب»، واصطدم، ثم انقر على «العب مجددًا»: تبدأ جولة جديدة. ثم اضغط على «تحقق».

جرّب بنفسك

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;
    private float distance;

    void Awake()
    {
        Instance = this;
    }

    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);
    }
}

جميع دروس إنشاء ألعاب حقيقية

تدرّب بنفسك: Playground لـ Unity