有输入时如何启动计时器?
How to start a timer when there's input?
在我的游戏中,我希望在用户左键单击时启动计时器。这是我到目前为止的代码:
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 ^";
}
}
}
如您所见,计时器会在游戏开始时立即启动,但我希望它在用户单击鼠标左键时启动,请告诉我是否有办法做到这一点,谢谢。
在您的更新函数中,使用 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 ^";
}
}
}
最好将计时器与更新功能分开。制作成另一个功能。协程非常适合这类事情,因为您可以轻松地使用它等待一段时间然后恢复操作。此外,如果您要多次使用它们,请缓存组件。您将经常使用 ping
变量,因此在 Awake
或 Start
函数中缓存是有意义的。
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 ^";
}
您需要使用 onMouseDown
事件
将以下内容添加到您现有的代码中:
创建一个布尔变量,将其设置为 false,将其设置为 true 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 ^";
}
}
在我的游戏中,我希望在用户左键单击时启动计时器。这是我到目前为止的代码:
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 ^";
}
}
}
如您所见,计时器会在游戏开始时立即启动,但我希望它在用户单击鼠标左键时启动,请告诉我是否有办法做到这一点,谢谢。
在您的更新函数中,使用 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 ^";
}
}
}
最好将计时器与更新功能分开。制作成另一个功能。协程非常适合这类事情,因为您可以轻松地使用它等待一段时间然后恢复操作。此外,如果您要多次使用它们,请缓存组件。您将经常使用 ping
变量,因此在 Awake
或 Start
函数中缓存是有意义的。
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 ^";
}
您需要使用 onMouseDown
事件
将以下内容添加到您现有的代码中: 创建一个布尔变量,将其设置为 false,将其设置为 true 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 ^";
}
}