|
@ -9,7 +9,8 @@ public class TimeScaleWindow : EditorWindow |
|
|
|
|
|
|
|
|
// Add menu named "My Window" to the Window menu
|
|
|
// Add menu named "My Window" to the Window menu
|
|
|
|
|
|
|
|
|
private float weightedTimeScale = 0; |
|
|
|
|
|
|
|
|
private int weightedTimeScale = 1; |
|
|
|
|
|
private bool isPaused = false; |
|
|
|
|
|
|
|
|
[MenuItem("AddOn/TimeScale")] |
|
|
[MenuItem("AddOn/TimeScale")] |
|
|
static void Init() |
|
|
static void Init() |
|
@ -33,62 +34,80 @@ public class TimeScaleWindow : EditorWindow |
|
|
|
|
|
|
|
|
GUILayout.EndHorizontal(); |
|
|
GUILayout.EndHorizontal(); |
|
|
|
|
|
|
|
|
weightedTimeScale = GUILayout.HorizontalSlider(weightedTimeScale, -100f, 100f); |
|
|
|
|
|
|
|
|
|
|
|
Time.timeScale = ConvertWeightedToRaw(weightedTimeScale); |
|
|
|
|
|
|
|
|
weightedTimeScale = (int)GUILayout.HorizontalSlider(weightedTimeScale, -98f, 100f); |
|
|
|
|
|
weightedTimeScale = Mathf.Clamp(weightedTimeScale,-98,100); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
GUILayout.BeginHorizontal(); |
|
|
GUILayout.BeginHorizontal(); |
|
|
|
|
|
|
|
|
if (GUILayout.Button("Pause")) weightedTimeScale = -100; |
|
|
|
|
|
|
|
|
if (GUILayout.Button("Pause")) isPaused = true; |
|
|
var centeredStyle = GUI.skin.GetStyle("Label"); |
|
|
var centeredStyle = GUI.skin.GetStyle("Label"); |
|
|
centeredStyle.alignment = TextAnchor.UpperCenter; |
|
|
centeredStyle.alignment = TextAnchor.UpperCenter; |
|
|
|
|
|
centeredStyle.normal.textColor = Color.black; |
|
|
|
|
|
|
|
|
|
|
|
if (isPaused) |
|
|
|
|
|
centeredStyle.normal.textColor = Color.red; |
|
|
|
|
|
|
|
|
|
|
|
GUILayout.Label(DisplayString(weightedTimeScale), centeredStyle); |
|
|
|
|
|
|
|
|
GUILayout.Label(DisplayString(Time.timeScale), centeredStyle); |
|
|
|
|
|
if (GUILayout.Button("Play")) Time.timeScale = 0; |
|
|
|
|
|
|
|
|
if (GUILayout.Button("Play")) isPaused = false; |
|
|
GUILayout.EndHorizontal(); |
|
|
GUILayout.EndHorizontal(); |
|
|
|
|
|
|
|
|
|
|
|
Time.timeScale = ConvertWeightedToRaw(weightedTimeScale); |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private float ConvertWeightedToRaw(float input) |
|
|
private float ConvertWeightedToRaw(float input) |
|
|
{ |
|
|
{ |
|
|
if (input > 0) |
|
|
|
|
|
return Mathf.Lerp(1, 100, input / 100); |
|
|
|
|
|
|
|
|
if (isPaused) |
|
|
|
|
|
return 0; |
|
|
|
|
|
|
|
|
|
|
|
if (input >= 1) |
|
|
|
|
|
return Mathf.Lerp(0, 100, input / 100); |
|
|
else |
|
|
else |
|
|
return Mathf.Lerp(1, 0, -input / 100); |
|
|
|
|
|
|
|
|
return 1 / (-input + 2); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void NextPower(int dir) |
|
|
private void NextPower(int dir) |
|
|
{ |
|
|
{ |
|
|
float sign = Mathf.Sign(weightedTimeScale); |
|
|
|
|
|
Debug.Log("Sign: " + sign); |
|
|
|
|
|
float value = Mathf.Abs(weightedTimeScale + dir); |
|
|
|
|
|
Debug.Log("Value: " + value); |
|
|
|
|
|
|
|
|
int sign = (int)Mathf.Sign(weightedTimeScale - 1); |
|
|
|
|
|
int prevValue = (sign >= 0) ? Mathf.Abs(weightedTimeScale) : Mathf.Abs(weightedTimeScale) + 2; |
|
|
|
|
|
int nextValue = Mathf.NextPowerOfTwo(prevValue); |
|
|
|
|
|
|
|
|
value = Mathf.NextPowerOfTwo((int)(value)); |
|
|
|
|
|
|
|
|
if (weightedTimeScale == 0) |
|
|
|
|
|
{ |
|
|
|
|
|
weightedTimeScale = 1; |
|
|
|
|
|
if (dir < 0) |
|
|
|
|
|
weightedTimeScale = -2; |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
if (Mathf.Sign(dir) != Mathf.Sign(weightedTimeScale)) |
|
|
|
|
|
value /= 2; |
|
|
|
|
|
|
|
|
|
|
|
Debug.Log("retVal: " + value + " * " + sign); |
|
|
|
|
|
weightedTimeScale = value * sign; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
if (Mathf.Sign(dir) != Mathf.Sign(weightedTimeScale)) |
|
|
|
|
|
nextValue /= 2; |
|
|
|
|
|
else if (prevValue == nextValue) |
|
|
|
|
|
nextValue = Mathf.NextPowerOfTwo(prevValue + 1); |
|
|
|
|
|
|
|
|
|
|
|
if (sign <= 0) |
|
|
|
|
|
nextValue -= 2; |
|
|
|
|
|
|
|
|
private void SetSpeed(float newSpeed) |
|
|
|
|
|
{ |
|
|
|
|
|
Time.timeScale = newSpeed; |
|
|
|
|
|
|
|
|
weightedTimeScale = nextValue * sign; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private string DisplayString(float input) |
|
|
private string DisplayString(float input) |
|
|
{ |
|
|
{ |
|
|
return input.ToString() + " (" + weightedTimeScale + ")"; |
|
|
|
|
|
|
|
|
string retVal = ""; |
|
|
|
|
|
|
|
|
if (input > 1) |
|
|
|
|
|
return Mathf.Floor(input).ToString(); |
|
|
|
|
|
|
|
|
if (isPaused) |
|
|
|
|
|
retVal += "(Paused) "; |
|
|
|
|
|
if (input > 0) |
|
|
|
|
|
retVal += input.ToString(); |
|
|
else |
|
|
else |
|
|
return ("1/" + Mathf.Floor(-weightedTimeScale)); |
|
|
|
|
|
|
|
|
retVal += "1/" + (-input + 2); |
|
|
|
|
|
|
|
|
|
|
|
return retVal; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
} |