Last active
October 11, 2018 22:27
-
-
Save kankikuchi/8e9f1a85c688429a8f0077fdd1e79952 to your computer and use it in GitHub Desktop.
エディタ実行中にTime.timeScaleを任意の値に変更する【Unity】【エディタ拡張】
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// TimeScaleControlWindow.cs | |
// http://kan-kikuchi.hatenablog.com/entry/TimeScaleControlWindow | |
// | |
// Created by kan.kikuchi on 2018.03.27. | |
using System; | |
using UnityEngine; | |
using UnityEditor; | |
/// <summary> | |
/// Time.timeScaleを変更する用のウィンドウ | |
/// </summary> | |
public class TimeScaleControlWindow : EditorWindow { | |
//TimeScaleの設定値を保存するためのクラス | |
[Serializable] | |
private class TimeScaleSettingValue { | |
//保存時のKey | |
private const string SAVE_KEY = "TimeScaleSettingValueSaveKey"; | |
//最小、最大、初期値 | |
[SerializeField] | |
private float _min, _max; | |
public float Min { get { return _min; } } | |
public float Max { get { return _max; } } | |
//================================================================================= | |
//初期化 | |
//================================================================================= | |
public TimeScaleSettingValue(){ | |
Reset(); | |
} | |
public void Reset(){ | |
_min = 0; | |
_max = 1; | |
} | |
//================================================================================= | |
//取得 | |
//================================================================================= | |
/// <summary> | |
/// 設定値をロード(保存されていなければ新規作成) | |
/// </summary> | |
/// <returns>The load.</returns> | |
public static TimeScaleSettingValue Load() { | |
//Json読み込み | |
string json = EditorUserSettings.GetConfigValue(SAVE_KEY); | |
//Josnがあれば変換し、なければ新規作成 | |
if (!string.IsNullOrEmpty(json)) { | |
return JsonUtility.FromJson<TimeScaleSettingValue>(json); | |
} | |
return new TimeScaleSettingValue(); | |
} | |
//================================================================================= | |
//保存 | |
//================================================================================= | |
/// <summary> | |
/// 設定値が変更されていれば保存する | |
/// </summary> | |
public void SaveIfneeded(float min, float max) { | |
if (!Mathf.Approximately(_min, min) || !Mathf.Approximately(_max, max)) { | |
_min = min; | |
_max = max; | |
EditorUserSettings.SetConfigValue(SAVE_KEY, JsonUtility.ToJson(this)); | |
} | |
} | |
} | |
private TimeScaleSettingValue _settingValue = new TimeScaleSettingValue(); | |
//================================================================================= | |
//初期化 | |
//================================================================================= | |
//メニューからウィンドウを表示 | |
[MenuItem("Window/Time Scale Control Window")] | |
public static void Open() { | |
GetWindow(typeof(TimeScaleControlWindow)); | |
} | |
private void OnEnable() { | |
//ウィンドウを開いた時に保存してた設定値をロード(なければ新規作成) | |
_settingValue = TimeScaleSettingValue.Load(); | |
} | |
//================================================================================= | |
//表示するGUIの設定 | |
//================================================================================= | |
private void OnGUI() { | |
EditorGUILayout.BeginVertical(GUI.skin.box); | |
//最小、最大を設定する用のフィールド | |
float min = EditorGUILayout.FloatField("Min", _settingValue.Min); | |
float max = EditorGUILayout.FloatField("Max", _settingValue.Max); | |
EditorGUILayout.Space(); | |
//エラーが出ないように0 ~ 100に制限、maxはmin以上に制限 | |
min = Mathf.Clamp(min, 0, 100); | |
max = Mathf.Clamp(max, min, 100); | |
EditorGUILayout.BeginVertical(GUI.skin.box); | |
//TimeScaleを設定する用のスライダー | |
Time.timeScale = EditorGUILayout.Slider(Time.timeScale, min, max); | |
EditorGUILayout.EndHorizontal(); | |
EditorGUILayout.Space(); | |
//リセット用のボタン | |
if (GUILayout.Button("リセット")) { | |
_settingValue.Reset(); | |
min = _settingValue.Min; | |
max = _settingValue.Max; | |
Time.timeScale = 1; | |
} | |
EditorGUILayout.EndHorizontal(); | |
//変更があったら保存 | |
_settingValue.SaveIfneeded(min, max); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://kan-kikuchi.hatenablog.com/entry/TimeScaleControlWindow