【问题标题】:how to make Coroutine Timer more Smooth如何让 Coroutine Timer 更流畅
【发布时间】:2019-06-22 04:02:51
【问题描述】:

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

我正在使用协程每 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;

    }
}

我只是想让它工作得更顺利,请帮助:)

【问题讨论】:

    标签: c# visual-studio unity3d


    【解决方案1】:

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

    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;
    
        }
    }
    

    【讨论】:

    • 谢谢,但我的延迟怎么办?我将 GlobalResearch.CoalTimer.text 放在 Update() 中我得到一个错误,因为游戏对象已被销毁
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-12
    • 2018-09-12
    • 1970-01-01
    • 2012-02-17
    • 2010-10-01
    相关资源
    最近更新 更多