C# 当有';谁的输入?
在我的游戏中,我想在用户左键点击时启动计时器。以下是我目前掌握的代码:C# 当有';谁的输入?,c#,unity3d,timer,unity5,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;
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 ^";
}
}
}
使用UnityEngine;
使用系统集合;
使用UnityEngine.UI;
公共课倒计时:单一行为
{
浮动时间限制=30.0f;
公共文本;
公共文本;
公共文本最终核心;
公共音频源ping;
公共游戏物体球;
//用于初始化
无效开始()
{
finalscore.text=“”;
}
void倒计时函数()
{
timeLeft-=Time.deltaTime;
text.text=“剩余时间:”+Mathf.Round(timeLeft)+“秒”;
}
//每帧调用一次更新
无效更新()
{
倒计时函数();
如果(时间间隔<0)
{
ping=GetComponent();
text.text=“时间到了!”;
ping.Play();
ball.SetActive(假);
finalscore.text=“最终分数^”;
}
}
}
正如您所看到的,计时器会在游戏开始后立即启动,但我希望在用户单击鼠标左键时启动计时器,请告诉我是否有办法,谢谢。在更新函数中,使用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 ^";
}
}
}
使用UnityEngine;
使用系统集合;
使用UnityEngine.UI;
公共课倒计时:单一行为
{
浮动时间限制=30.0f;
公共文本;
公共文本;
公共文本最终核心;
公共音频源ping;
公共游戏物体球;
bool timerStarted=false;
//用于初始化
无效开始()
{
finalscore.text=“”;
}
void倒计时函数()
{
timeLeft-=Time.deltaTime;
text.text=“剩余时间:”+Mathf.Round(timeLeft)+“秒”;
}
//每帧调用一次更新
无效更新()
{
if(Input.GetMouseButtonDown(0))
timerStarted=true;
如果(计时器开始)
倒计时函数();
如果(时间间隔<0)
{
ping=GetComponent();
text.text=“时间到了!”;
ping.Play();
ball.SetActive(假);
finalscore.text=“最终分数^”;
}
}
}
最好将计时器与更新功能分开。使成为另一个功能。协同程序非常适合这种情况,因为您可以轻松地使用它等待一段时间,然后恢复操作。此外,如果要多次使用组件,请缓存它们。您将经常使用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 ^";
}
void Start()
{
finalscore.text=“”;
ping=GetComponent();
}
无效更新()
{
//检查是否单击了鼠标左键
if(输入。GetMouseButton(0))
{
StartCustomer(startTimer(30));
}
}
IEnumerator startTimer(浮动时间限制)
{
而(时间间隔>0)
{
timeLeft-=Time.deltaTime;
text.text=“剩余时间:”+Mathf.Round(timeLeft)+“秒”;
收益返回空;
}
text.text=“时间到了!”;
ping.Play();
ball.SetActive(假);
finalscore.text=“最终分数^”;
}
您需要使用onMouseDown
事件
将以下内容添加到现有代码中:
创建一个布尔变量,将其设置为false,在mousedown上将其设置为true,并仅在设置为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 ^";
}
}
private bool mouseClicked=false;
void OnMouseDown(){
mouseClicked=true;
}
无效更新(){
如果(鼠标单击){
倒计时函数();
}
如果(时间间隔<0)
{
ping=GetComponent();
text.text=“时间到了!”;
ping.Play();
ball.SetActive(假);
finalscore.text=“最终分数^”;
}
}
脱离主题,但调用每个帧的GetComponent()
有些效率低下。“你为什么不把它存放在某个地方呢?”米基德:好主意,我对Unity和C#很陌生,就像我昨天开始的那样。谢谢你给我的提示。不用担心,这只是一个友好的提示:)祝你玩得开心,谢谢!工作得很好。