Oyun Kütüphanesi

Zamanlayıcı (Countdown Timer) Sistemi

Etkileşim & Input Unity C# 2
ZamanlayıcıCountdownTimerSistemi .cs
C#
using UnityEngine;
using UnityEngine.UI;
using System;

public class CountdownTimer : MonoBehaviour
{
    public float totalTime = 60f;
    public Text timerText;
    public event Action OnTimerEnd;

    private float remaining;
    private bool running;

    void Start() { remaining = totalTime; running = true; }

    void Update()
    {
        if (!running) return;
        remaining -= Time.deltaTime;
        if (remaining <= 0)
        {
            remaining = 0;
            running = false;
            OnTimerEnd?.Invoke();
        }
        UpdateDisplay();
    }

    void UpdateDisplay()
    {
        int mins = (int)(remaining / 60);
        int secs = (int)(remaining % 60);
        if (timerText) timerText.text = $"{mins:00}:{secs:00}";
    }

    public void Pause() => running = false;
    public void Resume() => running = true;
    public void Reset() { remaining = totalTime; running = true; }
}

Açıklama

Geriye sayan, bitince olay tetikleyen ve UI Text'e yazan countdown timer sistemi.

Etiketler

Timer Zamanlayıcı Countdown UI Text

Nasıl Kullanılır?

1. Sahnede UI Text objesi oluştur.

2. Bu scripti boş bir GameObject'e ekle, timerText'i ata.

3. Bitmesini dinlemek için:

timer.OnTimerEnd += GameOver;

4. Pause() / Resume() / Reset() metodları mevcut.

5. TextMeshPro için Text yerine TMP_Text kullan.

Unity 2022+ ve Unity 6 ile uyumludur.

MonoBehaviour tabanlı scriptleri Assets klasörüne .cs olarak kaydedin.