简体   繁体   English

如何在EditorWindow中暂停/继续计时器?

[英]How can I pause/continue a timer in EditorWindow?

void OnGUI()
    {
        pauseCounting = GUILayout.Toggle(pauseCounting, "Pause timer");
        Timer();
    }

Then: 然后:

private void Timer()
    {
        if (stopCounting == false)
        {
            EditorGUILayout.LabelField("Count Each:", countTime + " Secs");

            if (waitForUI)
                timeToCount = nextCountTime - EditorApplication.timeSinceStartup;

            EditorGUILayout.LabelField("Next Count:", timeToCount.ToString() + " Sec");
            if (waitForUI == false)
            {
                timeToCount = 30;
                waitForUI = true;
            }

            this.Repaint();

            if (EditorApplication.timeSinceStartup > nextCountTime)
            {
                nextCountTime = EditorApplication.timeSinceStartup + countTime;
            }

        }
        else
        {
            EditorGUILayout.LabelField("Counting:", countTime + " Secs");
            EditorGUILayout.LabelField("Next Count:", countToSave.ToString() + " Sec");
        }
    }

The problem is that I'm using now EditorApplication.timeSinceStartup and if I change the flag stopCounting to true then back to false it will calculate using the EditorApplication.timeSinceStartup and will not continue from the point it was stopped. 问题是我现在使用的是EditorApplication.timeSinceStartup,如果我将stopCounting标志更改为true,然后又返回false,它将使用EditorApplication.timeSinceStartup进行计算,并且不会从停止点继续。 I want that if stopCounting is true then false again continue the time from the last pause it was. 我希望如果stopCounting为true,那么false再次从上次暂停开始继续计时。

in your class, add a field to record the total ticking time: 在您的班级中,添加一个字段以记录总的滴答时间:

double timerRecord;

in your onEnable, init it: 在您的onEnable中,将其初始化:

timerRecord = EditorApplication.timeSinceStartup;

in your OnGUI(), add some logic to check the 'restart' action: 在您的OnGUI()中,添加一些逻辑以检查“重新启动”操作:

var shouldStop = GUILayout.Toggle(stopCounting, "Pause timer");
if(!shouldStop && stopCounting)
{
    // in this case, you restarted the timer.
    timerRecord = EditorApplication.timeSinceStartup;
}
stopCounting = shouldStop;
Timer();

and finally in your Time(), replace all 'EditorApplication.timeSinceStartup' with 'timerRecord', and at the end of it, update the value of 'timerRecord' with 'EditorApplication.timeSinceStartup'. 最后,在您的Time()中,将所有的'EditorApplication.timeSinceStartup'替换为'timerRecord',最后,将其'timerRecord'的值替换为'EditorApplication.timeSinceStartup'。

timerRecord = EditorApplication.timeSinceStartup;

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

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