From f261c57aa5eac512e22fe708540b22793c9b21bc Mon Sep 17 00:00:00 2001 From: "DESKTOP-B4ME9GF\\Joshua Reason" Date: Mon, 9 Jul 2018 11:04:09 +1000 Subject: [PATCH] Updated TimeScale Edifor --- .../TimeScaleEditor/TimeScaleWindow.cs | 73 ++++++++++++------- ProjectSettings/TimeManager.asset | 2 +- 2 files changed, 47 insertions(+), 28 deletions(-) diff --git a/Assets/Editor/Editor AddOn/TimeScaleEditor/TimeScaleWindow.cs b/Assets/Editor/Editor AddOn/TimeScaleEditor/TimeScaleWindow.cs index 17d079d..9722274 100644 --- a/Assets/Editor/Editor AddOn/TimeScaleEditor/TimeScaleWindow.cs +++ b/Assets/Editor/Editor AddOn/TimeScaleEditor/TimeScaleWindow.cs @@ -9,7 +9,8 @@ public class TimeScaleWindow : EditorWindow // Add menu named "My Window" to the Window menu - private float weightedTimeScale = 0; + private int weightedTimeScale = 1; + private bool isPaused = false; [MenuItem("AddOn/TimeScale")] static void Init() @@ -33,62 +34,80 @@ public class TimeScaleWindow : EditorWindow 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(); - if (GUILayout.Button("Pause")) weightedTimeScale = -100; + if (GUILayout.Button("Pause")) isPaused = true; var centeredStyle = GUI.skin.GetStyle("Label"); 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(); + Time.timeScale = ConvertWeightedToRaw(weightedTimeScale); + } 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 - return Mathf.Lerp(1, 0, -input / 100); + return 1 / (-input + 2); } 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) { - 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 - return ("1/" + Mathf.Floor(-weightedTimeScale)); + retVal += "1/" + (-input + 2); + + return retVal; } } diff --git a/ProjectSettings/TimeManager.asset b/ProjectSettings/TimeManager.asset index 035ddca..9cb90a9 100644 --- a/ProjectSettings/TimeManager.asset +++ b/ProjectSettings/TimeManager.asset @@ -5,5 +5,5 @@ TimeManager: m_ObjectHideFlags: 0 Fixed Timestep: 0.0167 Maximum Allowed Timestep: 0.1 - m_TimeScale: 1 + m_TimeScale: 8 Maximum Particle Timestep: 0.03