如何在输入时启动计时器?

时间:2016-06-22 00:41:29

标签: c# unity3d timer unity5

在我的游戏中,我希望在用户点击时启动计时器。这是我到目前为止的代码:

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

public class Countdown : MonoBehaviour
{
    float timeLeft = 30.0f;
    public Text text;
    public Text scoretext;
    public Text finalscore;
    public AudioSource ping;
    public GameObject ball;
    // Use this for initialization
    void Start ()
    {
        finalscore.text = "";        
    }

    void countdownfunction()
    {
        timeLeft -= Time.deltaTime;
        text.text = "Time Left: " + Mathf.Round(timeLeft) + " seconds";
    }

    // Update is called once per frame
    void Update ()
    {
        countdownfunction();

        if (timeLeft < 0)
        {
            ping = GetComponent<AudioSource>();
            text.text = "Time's up!";
            ping.Play();
            ball.SetActive(false);
            finalscore.text = "Final score ^";            
        }
    }
}

正如你所看到的,一旦游戏开始,计时器就会启动,但我希望在用户左键点击时启动计时器,请告诉我是否有办法执行此操作,谢谢。

3 个答案:

答案 0 :(得分:2)

将计时器与更新功能分开是件好事。进入另一个功能。 Coroutine非常适合这种东西,因为你可以轻松地使用它等待一段时间然后恢复操作。此外,如果您要多次使用它们,请缓存组件。您将经常使用ping变量,因此缓存在AwakeStart函数中是有意义的。

void Start()
{
    finalscore.text = "";
    ping = GetComponent<AudioSource>();
}

void Update()
{
    //Check if left mouse button is clicked
    if (Input.GetMouseButton(0))
    {
        StartCoroutine(startTimer(30));
    }
}

IEnumerator startTimer(float timeLeft)
{
    while (timeLeft > 0)
    {
        timeLeft -= Time.deltaTime;
        text.text = "Time Left: " + Mathf.Round(timeLeft) + " seconds";
        yield return null;
    }

    text.text = "Time's up!";
    ping.Play();
    ball.SetActive(false);
    finalscore.text = "Final score ^";
}

答案 1 :(得分:1)

在Update Function中,使用Input.GetMouseButtonDown(0)函数检查用户是否左键单击。 您的代码如下所示:

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

public class Countdown : MonoBehaviour
{
    float timeLeft = 30.0f;
    public Text text;
    public Text scoretext;
    public Text finalscore;
    public AudioSource ping;
    public GameObject ball;
    bool timerStarted = false;
    // Use this for initialization
    void Start ()
    {
        finalscore.text = "";        
    }

    void countdownfunction()
    {
        timeLeft -= Time.deltaTime;
        text.text = "Time Left: " + Mathf.Round(timeLeft) + " seconds";
    }

    // Update is called once per frame
    void Update ()
    {
        if(Input.GetMouseButtonDown(0))
            timerStarted = true;
        if(timerStarted)
            countdownfunction();

        if (timeLeft < 0)
        {
            ping = GetComponent<AudioSource>();
            text.text = "Time's up!";
            ping.Play();
            ball.SetActive(false);
            finalscore.text = "Final score ^";            
        }
    }
}

答案 2 :(得分:0)

您需要使用onMouseDown事件

将以下内容添加到现有代码中: 创建一个布尔变量,将其设置为false,将其设置为onMouseDown,并且仅当它设置为true时启动计时器:

private bool mouseClicked=false;

void OnMouseDown(){
 mouseClicked = true;
}

void Update () {

    if (mouseClicked){
        countdownfunction();
    }

    if (timeLeft < 0)
    {
        ping = GetComponent<AudioSource>();
        text.text = "Time's up!";
        ping.Play();
        ball.SetActive(false);
        finalscore.text = "Final score ^";


    }
}