StopCoroutine 后重置倒计时时间

Reset countdown time after StopCoroutine

我无法弄清楚如何重置计时器的不活动倒计时。我正在使用协程来处理倒计时,然后使用 StopCoroutine 通过按下按钮来停止倒计时。但是当倒计时对话框恢复并再次开始备份时(在不活动之后),计数从它之前停止的值开始。每次我的计数器实例化时,如何重置计数以从完整倒计时开始?

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using UnityEngine.SceneManagement;

public class GameManager : MonoBehaviour 
{
    public static GameManager gameManagerInstance = null; // Create Singleton
    public Color defaultBackgroundColor;                 
    public Object startingScene;
    public GameObject timeOutWarningDialog;

    private float countdownLength = 15;
    private float countdownDelay = 5;
    private float countdownInterval = 1;
    private IEnumerator counter;
    private Button stopCountButton;
    private Text timerTextField;
    private Vector3 prevMousePosition;
    private Scene currentScene;
    private GameObject gameManager;
    private GameObject canvas;
    private GameObject timerInstance;

    void Awake()
    {
        if (gameManagerInstance == null)
            gameManagerInstance = this;
        else if (gameManagerInstance != null)
            Destroy(gameObject);
        DontDestroyOnLoad(gameObject);
        gameManager = GameObject.FindGameObjectWithTag("GameManager");
    }

    void Start()
    {
        counter = RunTimer(countdownLength);
        prevMousePosition = Input.mousePosition;
        currentScene = SceneManager.GetActiveScene();
    }

    void Update()
    {
        if (Input.anyKeyDown || Input.mousePosition != prevMousePosition)
        {
            currentScene = SceneManager.GetActiveScene();

            if (currentScene.name != startingScene.name)
                StartPreCountTimer();
                if (timeOutWarningDialog != null)
                    timeOutWarningDialog.SetActive(false);
        }
        prevMousePosition = Input.mousePosition;
    }

    // GAME TIMER

    public void StartPreCountTimer()
    {
        CancelInvoke();
        if (GameObject.FindGameObjectWithTag("Timer") == null)
            Invoke("ShowRestartWarning", countdownDelay);
    }

    void ShowRestartWarning()
    {
        canvas = GameObject.FindGameObjectWithTag("Canvas");

        timerInstance = Instantiate(timeOutWarningDialog); // instantiate timeout warning dialog
        timerInstance.transform.SetParent(canvas.transform, false);
        timerInstance.SetActive(true);

        Text[] textFields = timerInstance.GetComponentsInChildren<Text>(true); // get reference to timer textfields
        timerTextField = textFields[1]; // access and assign countdown textfield

        stopCountButton = timerInstance.GetComponentInChildren<Button>(); // get reference to keep playing button
        stopCountButton.onClick.AddListener(StopTimer); // add button listener

        if (timerInstance.activeSelf == true)
            StartCoroutine(counter);
    }

    IEnumerator RunTimer(float seconds)
    {
        float s = seconds;
        while (s > 0)
        {
            if (timerTextField != null)
                timerTextField.text = s.ToString();
            yield return new WaitForSeconds(countdownInterval);
            s -= 1;
        }

        if (s == 0)
        {
            RestartGame();
        }
    }

    void StopTimer()
    {
        StopCoroutine(counter);
        Destroy(timerInstance);
    }

    void RestartGame()
    {
        SceneManager.LoadScene(startingScene.name);
    }
}

如果你想重置(这不是正确的重置,而是一个新的协程)协程中的代码,你需要为 counter 分配一个新的引用。

即:

void StopTimer() {
    StopCoroutine(counter);
    counter = RunTimer(countdownLength);
    Destroy(timerInstance);
}