如何使定时器在按键Unity3D上启动

时间:2019-02-22 15:13:59

标签: unity3d

我有以下代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Timer : MonoBehaviour
{
    public Text timerText;
    public Text votingResults;
    public float periodToVote;
    //public float periodToStartVoting;

    private bool objectDestroyed;
    private bool timerIsRunning;
    private bool timesUp = false;
    private float time;

    // Use this for initialization
    void Start ()
    {
        votingResults.enabled = false;
        timerText.text = "Press on Space for voting to start";
        timerIsRunning = false;
        objectDestroyed = false;
        timesUp = false;
        time = Time.time;
    }

    // Update is called once per frame
    void Update ()
    {
        if (Input.GetKeyDown("space"))
        {
            Debug.Log("The function is running correctly");
            time = Time.time;
            timerIsRunning = true;
            votingResults.enabled = true;
        }

        if (!objectDestroyed && timerIsRunning)
        {
            float votinEndingTime = periodToVote - Time.time;

            string minutes = ((int)votinEndingTime / 60).ToString();
            string seconds = (votinEndingTime % 60).ToString("f0");

            timerText.text = "Voting Ends in: " + "\n" + seconds;

            if (votinEndingTime < 0)
            {
                objectDestroyed = true;
                Destroy(timerText);
            }
        }
    }
}

正在添加到对象中。问题在于计时器在场景开始时启动,而不是在按下空格时启动。我不确定自己在做什么错。任何帮助将不胜感激。

如果有不清楚或需要更多解释的地方,请在评论中告诉我。

谢谢

2 个答案:

答案 0 :(得分:0)

因为您使用votingEndingTime(游戏中的当前时间)来计算Time.time,所以减去它是没有意义的。您必须像这样按下Space的时间,并在其中添加periodToVote来确定投票何时结束:

float votinEndingTime = periodToVote + time;

因为time是按下Space时的值。

然后您可以像这样检查时间是否到了:

if(Time.time > votingEndingTime)
{
    objectDestroyed = true;
    Destroy(timerText);
}

答案 1 :(得分:0)

我不建议在Update中执行此操作,而是推荐Coroutine,并且仅在满足条件的情况下才在Update中启动它。.这样可以节省一些时间,并且更易于控制。 / p>

public class Timer : MonoBehaviour
{
    public Text timerText;
    public Text votingResults;
    public float periodToVote;
    private bool isRunning;

    // Use this for initialization
    private void Start ()
    {
        votingResults.enabled = false;
        timerText.text = "Press on Space for voting to start";
    }

    private IEnumerator RunTimer()
    {
         isRunning = true;
         var passedTime = 0.0f;
         while(passedTime < periodToVote)
         {
              string minutes = Mathf.RoundToInt((periodToVote - passedTime) / 60).ToString();
              string seconds = Mathf.RoundToInt(periodToVote - passedTime).ToString("f0");
              timerText.text = "Voting Ends in: " + "\n" + seconds;

              passedTime += Time.deltaTime;
              yield return null;
         }

         Destroy(timerText);
    }

    // Update is called once per frame
    private void Update ()
    {
        if (if(!isRunning) && Input.GetKeyDown(KeyCode.Space))
        {
            Debug.Log("Sarting Timer");
            votingResults.enabled = true;
            StartCoroutine(RunTimer());
        }
    }
}