Skip to content

Instantly share code, notes, and snippets.

@yasuakiohama
Last active January 19, 2016 03:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yasuakiohama/a6e023d9b6abd3fa407e to your computer and use it in GitHub Desktop.
Save yasuakiohama/a6e023d9b6abd3fa407e to your computer and use it in GitHub Desktop.
TimeScaleWindow.cs
//
// Unity5.2.3p3
// TimeScaleWindow.cs
// http://yasuaki-ohama.hatenablog.com/entry/2016/01/14/223207
//
// Created by yasuaki ohama on 2016/01/14.
// http://yasuaki-ohama.com/
//
using UnityEngine;
using UnityEditor;
/// <summary>
/// 実行中のみTimeScaleを自由に操作するエディタ
/// </summary>
public sealed class TimeScaleWindow : EditorWindow
{
/// <summary>
/// 実行中のみTimeScaleの値を書き換えられる
/// </summary>
private bool m_isActive = true;
/// <summary>
/// ポーズなどの処理で時間を止めた場合、TimeScaleの値を変更しない
/// </summary>
private bool m_doNotChangeTimeScale0 = false;
/// <summary>
/// 値の書き換えよう変数
/// </summary>
private float m_timeScale = 1;
/// <summary>
/// スライダーの変更できる最大値
/// </summary>
private float m_max = 3;
/// <summary>
/// フォントの色設定
/// </summary>
private GUIStyle m_GUIStyle = new GUIStyle ();
/// <summary>
/// TimeScaleWindowの現在の状態
/// </summary>
private string stateText = "";
/// <summary>
/// ソートウィンドウの表示
/// </summary>
[MenuItem("EditorWindow/TimeScaleWindow")]
private static void Init()
{
EditorWindow.GetWindow (typeof(TimeScaleWindow), false, "Time Scale Window");
}
void Awake()
{
InitGUIStyle ();
}
private void Update()
{
UpdateTimeScale ();
}
/// <summary>
/// GUIStyleの初期化
/// </summary>
void InitGUIStyle()
{
GUIStyle style = EditorStyles.numberField;
m_GUIStyle.border = style.border;
m_GUIStyle.contentOffset = style.contentOffset;
m_GUIStyle.normal.background = style.normal.background;
m_GUIStyle.padding = style.padding;
}
/// <summary>
/// TimeScaleの更新処理
/// TimeScaleWindowの現在の状態の更新
/// </summary>
private void UpdateTimeScale()
{
if (!EditorApplication.isPlaying) {
stateText = "実行中のみ操作可能";
m_GUIStyle.normal.textColor = Color.yellow;
return;
}
if (!m_isActive) {
stateText = "アクティブでない";
m_GUIStyle.normal.textColor = Color.yellow;
return;
}
if (m_doNotChangeTimeScale0 && Time.timeScale == 0) {
stateText = "Time.timeScale == 0 更新しない";
m_GUIStyle.normal.textColor = Color.yellow;
return;
}
Time.timeScale = m_timeScale;
stateText = "Time.timeScale: " + Time.timeScale;
m_GUIStyle.normal.textColor = Color.green;
}
private void OnGUI()
{
EditorGUILayout.LabelField (stateText, m_GUIStyle);
m_isActive = EditorGUILayout.Toggle ("isActive", m_isActive);
m_doNotChangeTimeScale0 = EditorGUILayout.Toggle ("DoNotChangeTimeScale0", m_doNotChangeTimeScale0);
//縦並び
EditorGUILayout.BeginVertical( GUI.skin.box );
{
EditorGUILayout.LabelField ("TimeScale Change");
//横並び
EditorGUILayout.BeginHorizontal (GUI.skin.box);
{
if (GUILayout.Button ("0.0")) {
m_timeScale = 0;
}
if (GUILayout.Button ("0.1")) {
m_timeScale = 0.1f;
}
if (GUILayout.Button ("0.5")) {
m_timeScale = 0.5f;
}
if (GUILayout.Button ("1.0")) {
m_timeScale = 1;
}
if (GUILayout.Button ("2.0")) {
if (m_max < 2) {
m_max = 2;
}
m_timeScale = 2;
}
if (GUILayout.Button ("max")) {
m_timeScale = m_max;
}
}
EditorGUILayout.EndHorizontal ();
}
EditorGUILayout.EndVertical ();
m_timeScale = EditorGUILayout.Slider ("TimeScale 0〜" + m_max.ToString (), m_timeScale, 0, m_max);
m_max = EditorGUILayout.Slider ("max", m_max, 1, 100);
UpdateTimeScale ();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment