Unity Javascript帮助停止和恢复功能

时间:2015-01-29 11:45:15

标签: unity3d unityscript

我在Unity JS中有以下代码。我需要的是当调用函数Count1时,函数CountTime停止并再次启动。与功能Count2和Count3类似。但是当调用函数Count4时,CountTime应该停止而不能再次恢复。

#pragma strict

import UnityEngine.UI;

private var textfield:Text;
private var textfield0:Text;
private var textfield1:Text;
private var textfield2:Text;
private var textfield3:Text;

var timer : float = 0;


function Start () {
    textfield = GameObject.Find("TimerMain").GetComponent(Text);
    textfield0 = GameObject.Find("Timer").GetComponent(Text);
    textfield1 = GameObject.Find("Timer1").GetComponent(Text);
    textfield2 = GameObject.Find("Timer2").GetComponent(Text);
    textfield3 = GameObject.Find("Timer3").GetComponent(Text);
}

function Update(){
    CountTime();
}

function CountTime()
{
        timer += Time.deltaTime*10;
        textfield.text = timer.ToString("0000");

}

function Count1(){
    textfield0.text = textfield.text;
}

function Count2(){
    textfield1.text = textfield.text;
}

function Count3(){
    textfield2.text = textfield.text;
}

function Count4(){
    textfield3.text = textfield.text;
}

2 个答案:

答案 0 :(得分:1)

请提供有关程序逻辑的更多详细信息。 如果要在button1-2-3功能中设置定时器0,可以设置timer = 0f;

也许您可以在Count4按钮事件中添加标记。并在Update上检查它的值。 我假设您要重置计时器Count1,Count2,Count3方法。

var timer : float = 0;
var count4IsPressed = false;

function Start () {
    textfield = GameObject.Find("TimerMain").GetComponent(Text);
    textfield0 = GameObject.Find("Timer").GetComponent(Text);
    textfield1 = GameObject.Find("Timer1").GetComponent(Text);
    textfield2 = GameObject.Find("Timer2").GetComponent(Text);
    textfield3 = GameObject.Find("Timer3").GetComponent(Text);
}

function Update(){
    if(!count4IsPressed){
        CountTime();
    }  
}

function CountTime()
{
        timer += Time.deltaTime*10;
        textfield.text = timer.ToString("0000");

}

function Count1(){
    textfield0.text = textfield.text;
    timer = 0;
}

function Count2(){
    textfield1.text = textfield.text;
}

function Count3(){
    textfield2.text = textfield.text;
    timer = 0;
}

function Count4(){
    count4IsPressed = true;
    textfield3.text = textfield.text;
}

答案 1 :(得分:1)

你能不能做一个简单的选择,比如添加一个布尔检查来查看时间是开启还是关闭?然后,您可以在需要时暂停它或将其设置为false并且永不重启。如果错误,请原谅我的语法,我只使用c#。

var timer : float = 0;
var isCounting : boolean = true;

function Update(){
    if (isCounting)
    {
        CountTime();
    }
}

function Count1(){
    isCounting = false;
    // Do some stuff...
    textfield0.text = textfield.text;
    isCounting = false;
}

// Other methods/functions...

function Count4(){
    isCounting = false;
    textfield3.text = textfield.text;
}

或者,如果您需要时间回到这些功能的0.0f,只需在您想要重新开始时间的地方插入语句timer = 0.0f;