在StopCoroutine之后重置倒计时时间

时间:2017-06-14 12:55:14

标签: c# unity3d counter countdown

我无法弄清楚如何重置计时器的不活动倒计时。我使用coroutine来处理倒计时,然后使用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);
    }
}

1 个答案:

答案 0 :(得分:1)

如果你想重置(它没有正确重置,只是一个新的协同程序)协程中的代码,你需要为counter分配一个新的引用。

即:

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