unity 倒计时
- 作者: 幸福一生19550346
- 来源: 51数据库
- 2020-09-23
using UnityEngine;
using System.Collections;
public class test : MonoBehaviour {
private float allTime = 10.999f;
private int showNumber;
private bool gameOver = false;
private bool isBegin = false;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
showNumber = (int)allTime;
if (allTime > 0) {
if(isBegin){
allTime -= Time.deltaTime;
}
}else{
gameOver = true;
}
}
void OnGUI(){
GUI.Button (new Rect (10, 10, 100, 100), showNumber+"");
if (GUI.Button (new Rect (120, 10, 100, 100), "开始计时")) {
isBegin = true;
}
if (gameOver) {
GUI.Button(new Rect(Screen.width/2-50,Screen.height/2-50,100,100),"游戏结束");
}
}
}
一个思路
在update里累计每帧时间就行了
float?i=90f;
void?start()
{
?????debug.log("倒计时"+i.tostring()+"秒");
}
void?update()
{
i-=time.delttime;
if(1<=0) ????debug.log("倒计时时间到");="">=0)>
using System.Collections;
public class test : MonoBehaviour {
private float allTime = 10.999f;
private int showNumber;
private bool gameOver = false;
private bool isBegin = false;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
showNumber = (int)allTime;
if (allTime > 0) {
if(isBegin){
allTime -= Time.deltaTime;
}
}else{
gameOver = true;
}
}
void OnGUI(){
GUI.Button (new Rect (10, 10, 100, 100), showNumber+"");
if (GUI.Button (new Rect (120, 10, 100, 100), "开始计时")) {
isBegin = true;
}
if (gameOver) {
GUI.Button(new Rect(Screen.width/2-50,Screen.height/2-50,100,100),"游戏结束");
}
}
}
一个思路
在update里累计每帧时间就行了
float?i=90f;
void?start()
{
?????debug.log("倒计时"+i.tostring()+"秒");
}
void?update()
{
i-=time.delttime;
if(1<=0) ????debug.log("倒计时时间到");="">=0)>
推荐阅读
