简体   繁体   中英

How can I change the time left in the timer in real time while it's running?

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;
using UnityEngine.SceneManagement;
using System.Timers;
using System.Diagnostics;
using System.Threading;

public class Auto : EditorWindow
{
    private double next = 0;
    private double time = 0;
    private bool stop;
    private bool pause;
    private bool reset = false;
    private bool waitForUI = false;
    private bool minimizeToZero;
    private static Auto editor;
    private static int width = 300;
    private static int height = 120;
    private static int x = 0;
    private static int y = 0;
    private System.Timers.Timer timer;
    private int h = 5, m = 33, s = 5;
    private static float next = 0.0f;

    [MenuItem("Window/Auto")]
    static void ShowEditor()
    {
        editor = EditorWindow.GetWindow<Auto>();
        editor.Init();

        CenterWindow();
    }

    public void Init()
    {
        timer = new System.Timers.Timer();
        timer.Interval = 1000;//1s
        timer.Elapsed += T_Elapsed;
        timer.Start();
    }

    private void T_Elapsed(object sender, ElapsedEventArgs e)
    {
        if (s == 0)
        {
            s = 60;
            m -= 1;
        }
        if (m == 0)
        {
            m = 60;
            h -= 1;
        }

        s -= 1;
    }

    void OnGUI()
    {
        minimizeToZero = GUILayout.Toggle(minimizeToZero, "Minimize To Zero");

        if (pause)
        {
            GUI.enabled = false;
        }
        else
        {
            GUI.enabled = true;
        }
        stop = GUILayout.Toggle(stop, "Stop");

        if (stop)
        {
            GUI.enabled = false;
        }
        else
        {
            GUI.enabled = true;
        }
        pause = GUILayout.Toggle(pause, "Pause");
        GUILayout.Space(10);

        if (minimizeToZero)
        {
            MinimizeWindow();
        }
        else
        {
            CenterWindow();
        }

        next = EditorGUILayout.Slider(next, 1, 100);
        string t = string.Format("{0}:{1}:{2}", h.ToString().PadLeft(2, '0'), m.ToString().PadLeft(2, '0'), s.ToString().PadLeft(2, '0'));
        EditorGUILayout.LabelField("Next: ", t);

        if (stop)
        {
            timer.Stop();
            reset = true;
        }
        else
        {
            if (reset)
            {
                timer.Start();
                h = 5;
                m = 33;
                s = 5;
                reset = false;
            }
        }

        if (pause)
        {
            timer.Stop();
        }
        else
        {
            timer.Start();
        }
    }
}

I have the next variable that I'm using with it for a slider:

next = EditorGUILayout.Slider(next, 1, 100);

What I want to do is when changing the slider value while the timer is running change in real time one of the timer values for example the variable s(seconds) and each time it's changing the s value keep the timer counting down from the current new seconds value. Or if it's the m(minutes) or h(hours).

If after the line:

next = EditorGUILayout.Slider(next, 1, 100);

I'm doing:

s = (int)next;

The timer will never start working. What I want to do is to change the timer counting down value/s in real time and keep counting down from the current new value change. Not to stop the timer and to start over again but to keep running from the next changed value/s.

You should set these variables only if the slider is changed.

EditorGUI.BeginChangeCheck();
next = EditorGUILayout.Slider(next, 1, 100);
if(EditorGUI.EndChangeCheck())
{
    //set h/m/s here
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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