From 8180868a6aa9ad1435b3a238f00316186f734548 Mon Sep 17 00:00:00 2001 From: "DESKTOP-B4ME9GF\\Joshua Reason" Date: Fri, 8 Jun 2018 17:35:00 +1000 Subject: [PATCH] Started work on Time Scale Editor --- .../Editor/Editor AddOn/TimeScaleEditor.meta | 8 ++ .../TimeScaleEditor/TimeScaleWindow.cs | 94 +++++++++++++++++++ .../TimeScaleEditor/TimeScaleWindow.cs.meta | 11 +++ 3 files changed, 113 insertions(+) create mode 100644 Assets/Editor/Editor AddOn/TimeScaleEditor.meta create mode 100644 Assets/Editor/Editor AddOn/TimeScaleEditor/TimeScaleWindow.cs create mode 100644 Assets/Editor/Editor AddOn/TimeScaleEditor/TimeScaleWindow.cs.meta diff --git a/Assets/Editor/Editor AddOn/TimeScaleEditor.meta b/Assets/Editor/Editor AddOn/TimeScaleEditor.meta new file mode 100644 index 0000000..2fa3357 --- /dev/null +++ b/Assets/Editor/Editor AddOn/TimeScaleEditor.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: d4a830bb77ae79d4883e970695e3ac1f +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Editor/Editor AddOn/TimeScaleEditor/TimeScaleWindow.cs b/Assets/Editor/Editor AddOn/TimeScaleEditor/TimeScaleWindow.cs new file mode 100644 index 0000000..17d079d --- /dev/null +++ b/Assets/Editor/Editor AddOn/TimeScaleEditor/TimeScaleWindow.cs @@ -0,0 +1,94 @@ +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 float weightedTimeScale = 0; + + [MenuItem("AddOn/TimeScale")] + static void Init() + { + // Get existing open window or if none, make a new one: + TimeScaleWindow window = (TimeScaleWindow)EditorWindow.GetWindow(typeof(TimeScaleWindow)); + window.Show(); + } + + void OnGUI() + { + GUILayout.Label("Base Settings", EditorStyles.boldLabel); + + GUILayout.BeginHorizontal(); + + if (GUILayout.Button("<<")) NextPower(-1); + if (GUILayout.Button("<")) weightedTimeScale--; + + if (GUILayout.Button(">")) weightedTimeScale++; + if (GUILayout.Button(">>")) NextPower(1); + + GUILayout.EndHorizontal(); + + weightedTimeScale = GUILayout.HorizontalSlider(weightedTimeScale, -100f, 100f); + + Time.timeScale = ConvertWeightedToRaw(weightedTimeScale); + + GUILayout.BeginHorizontal(); + + if (GUILayout.Button("Pause")) weightedTimeScale = -100; + var centeredStyle = GUI.skin.GetStyle("Label"); + centeredStyle.alignment = TextAnchor.UpperCenter; + + GUILayout.Label(DisplayString(Time.timeScale), centeredStyle); + if (GUILayout.Button("Play")) Time.timeScale = 0; + GUILayout.EndHorizontal(); + + } + + private float ConvertWeightedToRaw(float input) + { + if (input > 0) + return Mathf.Lerp(1, 100, input / 100); + else + return Mathf.Lerp(1, 0, -input / 100); + } + + + + private void NextPower(int dir) + { + float sign = Mathf.Sign(weightedTimeScale); + Debug.Log("Sign: " + sign); + float value = Mathf.Abs(weightedTimeScale + dir); + Debug.Log("Value: " + value); + + value = Mathf.NextPowerOfTwo((int)(value)); + + if (Mathf.Sign(dir) != Mathf.Sign(weightedTimeScale)) + value /= 2; + + Debug.Log("retVal: " + value + " * " + sign); + weightedTimeScale = value * sign; + } + + + private void SetSpeed(float newSpeed) + { + Time.timeScale = newSpeed; + } + + private string DisplayString(float input) + { + return input.ToString() + " (" + weightedTimeScale + ")"; + + if (input > 1) + return Mathf.Floor(input).ToString(); + else + return ("1/" + Mathf.Floor(-weightedTimeScale)); + } + +} diff --git a/Assets/Editor/Editor AddOn/TimeScaleEditor/TimeScaleWindow.cs.meta b/Assets/Editor/Editor AddOn/TimeScaleEditor/TimeScaleWindow.cs.meta new file mode 100644 index 0000000..f136f79 --- /dev/null +++ b/Assets/Editor/Editor AddOn/TimeScaleEditor/TimeScaleWindow.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 9eb2610747faaf14689b38a264c25baa +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: