なぜこのようなことが起こっているのか分かりませんが、StopTimerButton()を呼び出すボタンを押したときに停止するカウントダウンタイマーがあります。しかし、StopCoroutineを呼び出しても、タイマーはゼロに達するまでカウントダウンし続け、RestartGame()を呼び出します。私のコードでは何が分かりませんか?StopCoroutineの後にタイマーが停止しない
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class CountdownTimer : MonoBehaviour
{
public static CountdownTimer countdownTimerInstance = null; // create singleton
public Object startingScene;
public GameObject timeOutWarningDialog;
public float preCountdownLength;
public float countdownLength;
private GameObject timerDialogBoxInstance;
private GameObject timerDialogCountdownText;
private Text timerDialogCountdownTextTarget;
private GameObject canvas;
private IEnumerator warningCounter;
private IEnumerator preCounter;
private Button stopCountButton;
private float countdownInterval = 1.0f;
private bool preCountActive;
private bool warningCountActive;
void Awake()
{
ResetCountStates();
if (countdownTimerInstance == null)
countdownTimerInstance = this;
else if (countdownTimerInstance != null)
Destroy(gameObject);
DontDestroyOnLoad(gameObject);
}
void Update()
{
bool userActive = GameManager.userActive;
bool onIntroScreen = GameManager.onIntroScreen;
if (!userActive && !onIntroScreen && !preCountActive)
StartPreCountTimer(preCountdownLength);
else if (userActive && !onIntroScreen && preCountActive)
StopPreCountTimer();
}
void StartPreCountTimer(float length)
{
preCountActive = true;
preCounter = RunTimer(length);
StartCoroutine(preCounter);
Debug.Log("PreCount Started");
}
void StopPreCountTimer()
{
preCountActive = false;
StopCoroutine(preCounter);
Debug.Log("PreCount Stopped");
}
void WarningDialog(float length)
{
preCountActive = false;
warningCountActive = true;
canvas = GameObject.FindGameObjectWithTag("Canvas");
timerDialogBoxInstance = Instantiate(timeOutWarningDialog); // instantiate timeout warning dialog
timerDialogBoxInstance.transform.SetParent(canvas.transform, false);
timerDialogBoxInstance.SetActive(true);
timerDialogCountdownText = GameObject.FindGameObjectWithTag("CountdownText"); // get reference to countdown text GO
timerDialogCountdownTextTarget = timerDialogCountdownText.GetComponent<Text>(); // get countdown textfield component
stopCountButton = timerDialogBoxInstance.GetComponentInChildren<Button>(); // get reference to keep playing button
stopCountButton.onClick.AddListener(StopTimerButton); // add button listener
if (warningCountActive && !preCountActive && timerDialogBoxInstance != null)
{
warningCounter = RunTimer(length); // create new reference to counter, resets countdown to countdownLength
StartCoroutine(warningCounter);
}
}
IEnumerator RunTimer(float seconds)
{
// PRECOUNT TIMER
if (!warningCountActive)
{
float s = seconds;
while (s > 0)
{
yield return new WaitForSeconds(countdownInterval);
s -= countdownInterval;
Debug.Log("PreCount: " + s);
}
if (s == 0)
{
preCountActive = false;
warningCountActive = true;
WarningDialog(countdownLength);
}
}
// WARNING DIALOG TIMER
if (warningCountActive)
{
float s = seconds;
while (s > 0)
{
yield return new WaitForSeconds(countdownInterval);
if (timerDialogBoxInstance != null)
timerDialogCountdownTextTarget.text = s.ToString();
s -= countdownInterval;
Debug.Log("WarningCountdown: " + s);
}
if (s == 0)
if (timerDialogBoxInstance != null)
{
StopCoroutine(warningCounter);
Destroy(timerDialogBoxInstance);
RestartGame();
}
}
}
void StopTimerButton()
{
warningCountActive = false;
StopCoroutine(warningCounter);
if (timerDialogBoxInstance != null)
{
Destroy(timerDialogBoxInstance);
timerDialogBoxInstance = null;
}
Debug.Log("Restart Cancelled");
}
void ResetCountStates()
{
preCountActive = false;
warningCountActive = false;
}
void RestartGame()
{
ResetCountStates();
SceneManager.LoadScene(startingScene.name);
}
}
「WarningDialog」関数を呼び出すと、 – Programmer
PreCountTimerが0に達した後(Runtimerで)WarningDialog()を呼び出します。 – greyBow
そして 'StopTimerButton'関数で停止したいタイマーをどうやって起動しますか? – Programmer