如何使协程定时器更流畅

时间:2019-06-21 18:46:50

标签: c# visual-studio unity3d

我正在构建一个有几个场景的空闲游戏 我想知道是否有更好的方法来做计时器,因为当我用计时器进入场景时,在文本更新之前我有一个延迟

我正在使用协程每1秒减去一次


public class CoalResarch : MonoBehaviour
{

    bool CreatingWords = false;
    public bool timerIsDone = false;
    public static float hour = 1;
    public static float min = 0;
    public static float sec = 1;
    void Update()
    {

        if (CreatingWords == false)
        {
            CreatingWords = true;
            StartCoroutine(DisplayWoodMinningSec());
        }

    }
    public IEnumerator DisplayWoodMinningSec()
    {
        //Timer
        if (SceneManager.GetActiveScene() == SceneManager.GetSceneByName("ResearchScene"))
        {
            GlobalResearch.CoalTimer.text = "0" + hour + ":" + min + ":" + Mathf.Round(sec);
        }
        yield return new WaitForSeconds(1);
        sec -= 1;

        if (timerIsDone == false && sec < 0)
        {
            if (min > 0)
            {
                min -= 1;
                sec = 59;
            }
            else
            {
                if (hour > 0)
                {
                    hour -= 1;
                    min = 59;
                    sec = 59;
                }
                else
                {
                    sec = 0;
                }
            }

            if (hour == 0 && min == 0 && sec == 0)
            {
                Debug.Log("Finish!");
                if (SceneManager.GetActiveScene() == SceneManager.GetSceneByName("ResearchScene"))
                {
                    //      GlobalResearch.CoalTimer.text = "Finished !";
                }
            }

        }

        if (SceneManager.GetActiveScene() == SceneManager.GetSceneByName("ResearchScene"))
        {
            GlobalResearch.CoalTimer.text = "0" + hour + ":" + min + ":" + Mathf.Round(sec);
        }
        CreatingWords = false;

    }
}

我只希望它工作更流畅,请帮忙:)

1 个答案:

答案 0 :(得分:0)

使用while循环,该循环在任意数字> 0时持续。另外,请使用int而不是float,因为您只对整数感兴趣。您还可以简化秒/分钟/小时的更新条件:

public class CoalResarch : MonoBehaviour
{

    bool CreatingWords = false;
    public bool timerIsDone = false;
    public static int hour = 1;
    public static int min = 0;
    public static int sec = 1;
    void Update()
    {

        if (CreatingWords == false)
        {
            CreatingWords = true;
            StartCoroutine(DisplayWoodMinningSec());
        }

    }
    public IEnumerator DisplayWoodMinningSec()
    { 
        while (hour>0 || min>0 || sec > 0)
        {
            //Timer
            if (SceneManager.GetActiveScene() == SceneManager.GetSceneByName("ResearchScene"))
            {
                GlobalResearch.CoalTimer.text = "0" + hour + ":" + min + ":" + Mathf.Round(sec);
            }
            yield return new WaitForSeconds(1);
            sec -= 1;

            if (sec < 0)
            {
                sec = 59;
                min -= 1;
                if (min < 0)
                {
                    hour -= 1;
                    min = 59;
                }
            }
        }

        timerIsDone = true;
        Debug.Log("Finish!");

        if (SceneManager.GetActiveScene() == SceneManager.GetSceneByName("ResearchScene"))
        {
            GlobalResearch.CoalTimer.text = "0" + hour + ":" + min + ":" + Mathf.Round(sec);
        }
        // GlobalResearch.CoalTimer.text = "Finished !";
        CreatingWords = false;

    }
}