using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEditor;
|
|
using UnityEngine.UI;
|
|
|
|
public class TimeScaleWindow : EditorWindow
|
|
{
|
|
|
|
// Add menu named "My Window" to the Window menu
|
|
|
|
private int weightedTimeScale = 1;
|
|
private bool isPaused = false;
|
|
private Color defaultColor;
|
|
|
|
[MenuItem("Tools/TimeScale")]
|
|
static void Init()
|
|
{
|
|
// Get existing open window or if none, make a new one:
|
|
TimeScaleWindow window = (TimeScaleWindow)EditorWindow.GetWindow(typeof(TimeScaleWindow),false,"Time Scale");
|
|
|
|
|
|
Rect rect = window.position;
|
|
rect.width = 300;
|
|
rect.height = EditorGUIUtility.singleLineHeight * 4;
|
|
window.minSize = new Vector2(rect.width, rect.height);
|
|
window.maxSize = new Vector2(rect.width, rect.height);
|
|
window.position = rect;
|
|
|
|
window.Show();
|
|
}
|
|
|
|
void OnGUI()
|
|
{
|
|
GUILayout.BeginHorizontal();
|
|
|
|
if (GUILayout.Button("<<")) NextPower(-1);
|
|
if (GUILayout.Button("<")) weightedTimeScale--;
|
|
|
|
if (GUILayout.Button(">")) weightedTimeScale++;
|
|
if (GUILayout.Button(">>")) NextPower(1);
|
|
|
|
GUILayout.EndHorizontal();
|
|
weightedTimeScale = (int)GUILayout.HorizontalSlider(weightedTimeScale, -98f, 100f);
|
|
weightedTimeScale = Mathf.Clamp(weightedTimeScale,-98,100);
|
|
GUILayout.Space(EditorGUIUtility.singleLineHeight);
|
|
|
|
GUILayout.BeginHorizontal();
|
|
|
|
if (GUILayout.Button("Pause")) isPaused = true;
|
|
var centeredStyle = new GUIStyle(EditorStyles.label);
|
|
centeredStyle.alignment = TextAnchor.UpperCenter;
|
|
centeredStyle.normal.textColor = EditorStyles.label.normal.textColor;
|
|
|
|
if (isPaused)
|
|
centeredStyle.normal.textColor = Color.red;
|
|
|
|
GUILayout.Label(DisplayString(weightedTimeScale), centeredStyle);
|
|
|
|
if (GUILayout.Button("Play")) isPaused = false;
|
|
GUILayout.EndHorizontal();
|
|
|
|
Time.timeScale = ConvertWeightedToRaw(weightedTimeScale);
|
|
|
|
}
|
|
|
|
private float ConvertWeightedToRaw(float input)
|
|
{
|
|
if (isPaused)
|
|
return 0;
|
|
|
|
if (input >= 1)
|
|
return Mathf.Lerp(0, 100, input / 100);
|
|
else
|
|
return 1 / (-input + 2);
|
|
}
|
|
|
|
|
|
|
|
private void NextPower(int dir)
|
|
{
|
|
int sign = (int)Mathf.Sign(weightedTimeScale - 1);
|
|
int prevValue = (sign >= 0) ? Mathf.Abs(weightedTimeScale) : Mathf.Abs(weightedTimeScale) + 2;
|
|
int nextValue = Mathf.NextPowerOfTwo(prevValue);
|
|
|
|
if (weightedTimeScale == 0)
|
|
{
|
|
weightedTimeScale = 1;
|
|
if (dir < 0)
|
|
weightedTimeScale = -2;
|
|
return;
|
|
}
|
|
|
|
|
|
if (Mathf.Sign(dir) != Mathf.Sign(weightedTimeScale))
|
|
nextValue /= 2;
|
|
else if (prevValue == nextValue)
|
|
nextValue = Mathf.NextPowerOfTwo(prevValue + 1);
|
|
|
|
if (sign <= 0)
|
|
nextValue -= 2;
|
|
|
|
weightedTimeScale = nextValue * sign;
|
|
}
|
|
|
|
private string DisplayString(float input)
|
|
{
|
|
string retVal = "";
|
|
|
|
if (isPaused)
|
|
retVal += "(Paused) ";
|
|
if (input > 0)
|
|
retVal += input.ToString();
|
|
else
|
|
retVal += "1/" + (-input + 2);
|
|
|
|
return retVal;
|
|
}
|
|
|
|
}
|