Skip to content

Instantly share code, notes, and snippets.

@gemfile0
Last active April 24, 2017 10:39
Show Gist options
  • Save gemfile0/2796d8ee30a669202b59af660b958ea2 to your computer and use it in GitHub Desktop.
Save gemfile0/2796d8ee30a669202b59af660b958ea2 to your computer and use it in GitHub Desktop.
Time Scale Window in Unity

Time Scale Window in Unity

Just pulled out the time scale as a separate window(Tools > Time Scale Editor) from time manager window(Edit > Project Settings > Time). It also recovers the value if has any changes in PlayMode.

how to use

using UnityEngine;
using UnityEditor;
public class TimeScaleWindow: EditorWindow
{
float timeScale = 1.0f;
float timeScaleBefore = 0f;
[MenuItem("Tools/Time Scale Window")]
public static void OpenWindow()
{
var window = EditorWindow.GetWindow(typeof(TimeScaleWindow));
window.titleContent = new GUIContent("Time Scale Window");
}
void OnEnable()
{
EditorApplication.playmodeStateChanged += OnPlaymodeStateChange;
}
void OnDiable()
{
EditorApplication.playmodeStateChanged -= OnPlaymodeStateChange;
}
void OnGUI()
{
timeScale = EditorGUILayout.Slider("Time Scale", timeScale, 0.0f, 1.0f);
ChangeTimeScale(timeScale);
}
void OnPlaymodeStateChange()
{
if (EditorApplication.isPlayingOrWillChangePlaymode && EditorApplication.isPlaying)
{
timeScaleBefore = timeScale;
}
else if (!EditorApplication.isPlayingOrWillChangePlaymode && !EditorApplication.isPlaying)
{
timeScale = timeScaleBefore;
ChangeTimeScale(timeScale);
Repaint();
}
}
void ChangeTimeScale(float value)
{
if (timeScale != Time.timeScale) { Time.timeScale = timeScale; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment