繁体   English   中英

Unity:如何暂停我的游戏计时器并在“已完成游戏”屏幕上显示时间结果

[英]Unity: How to pause my game-timer and present time result on “finished game” screen

晚上好,伙计们!

我有一个小问题。 我正在创建一个 Pacman-ish 游戏,并且我创建了一个计时器,它会弹出并出现在屏幕的上角,显示自第一个场景开始以来经过的当前时间。

我正在努力让计时器在我的游戏结束场景中停止

您对可能有效的解决方案有任何提示吗?

我对 C# 和 Unity 非常陌生,所以请记住这一点,对于任何不便,我们深表歉意!

非常感谢您!

(哦,顺便说一句,我已经为计时器制作了另一个脚本以继续通过场景,并且在游戏重新启动时不会自我复制,所以这部分现在还可以)

public Text timerText;
private float startTime;
private bool finnished = false;

// Start is called before the first frame update
void Start()
{
    startTime = Time.time;

}

// Update is called once per frame
void Update()
{
    if (finnished)
        return;

    float t = Time.time - startTime;

    string minutes = ((int)t / 60).ToString();
    string seconds = (t % 60).ToString("f2");

    timerText.text = minutes + ":" + seconds;

}


public void Finnish()
{
    finnished = true;
    timerText.color = Color.yellow;
}

你可以尝试一个事件:

订阅 stopEvent 事件委托:

public Text timerText;
private float startTime;
private bool finnished = false;

GameManager.stopEvent += Finnish;

// Start is called before the first frame update
void Start()
{
    startTime = Time.time;
}

// Update is called once per frame
void Update()
{
    if (!finnished)
    {
        float t = Time.time - startTime;

        string minutes = ((int)t / 60).ToString();
        string seconds = (t % 60).ToString("f2");

        timerText.text = minutes + ":" + seconds;
    }
}


public void Finnish()
{
    finnished = true;
    timerText.color = Color.yellow;
}

在脚本(比如说,GameManager.cs 或其他东西)中,您创建事件并委托:

public delegate void EventHandler();
public static event EventHandler stopEvent;

您的场景加载代码(例如,可以从 GameManager.cs 中的加载代码中调用)

//// scene is done
stopEvent();

您可以将计时器结果存储在 singleton 中,也许是一个 GameManager.cs 文件,它是一个 singleton,它存在于各个场景中。

好的 Kale_Surfer_Dude,所以我想出了如何触发 Finnish(); 在进入我的两个完成/目标场景中的任何一个时,但不知何故,当我死亡或完成时它不会触发。

你明白为什么吗?

public Text timerText;
private float startTime;
private bool finnished = false;

// Start is called before the first frame update
void Start()
{
    startTime = Time.time;

    // Creating temporary reference to current scene
    Scene currentScene = SceneManager.GetActiveScene();

    // Retrieve the name of this scene.
    string sceneName = currentScene.name;

    if(sceneName == "Goal" || sceneName == "MenuOnCollision")
    {
        Finnish();
    }

    // Retrieve the index of the scene in the project's build setting
    int buildIndex = currentScene.buildIndex;

    // Check the scene name as a conditional
    switch (buildIndex)
    {
        case 4:
            Finnish();
            break;
        case 6:
            Finnish();
            break;
    }

}

// Update is called once per frame
void Update()
{
    if (!finnished) {

        float t = Time.time - startTime;


        string minutes = ((int)t / 60).ToString();
        string seconds = (t % 60).ToString("f2");

        timerText.text = minutes + ":" + seconds;
    }

}


public void Finnish()
{
    finnished = true;
    timerText.color = Color.yellow;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM