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();
}
}
}
我有一个下一个要用于滑块的变量:
next = EditorGUILayout.Slider(next, 1, 100);
我想做的是在计时器运行时更改滑块值时实时更改计时器值之一,例如变量s(seconds),并且每次更改s值都会使计时器从当前的新秒值。或者是m(分钟)或h(小时)。
如果在行后:
next = EditorGUILayout.Slider(next, 1, 100);
我在做
s = (int)next;
计时器将永远不会开始工作。 我想做的是实时更改计时器的递减计数值,并保持从当前新值更改开始递减计数。 不是停止计时器并重新开始,而是从下一个更改的值开始继续运行。
答案 0 :(得分:2)
仅在更改滑块时才应设置这些变量。
EditorGUI.BeginChangeCheck();
next = EditorGUILayout.Slider(next, 1, 100);
if(EditorGUI.EndChangeCheck())
{
//set h/m/s here
}