Created
December 15, 2016 10:32
-
-
Save fuqunaga/c8a3d102f7b83429e7f7f26002e52041 to your computer and use it in GitHub Desktop.
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
using UnityEngine; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System; | |
public class TimeDebugger : MonoBehaviour | |
{ | |
#region TypeDefine | |
[System.Serializable] | |
public class NamedFloat | |
{ | |
public string name; | |
public float time; | |
} | |
#endregion | |
public List<NamedFloat> _presets = new List<NamedFloat>(); | |
int _skipSec = 1; | |
bool _isSkipping; | |
float _startTime; | |
public float modeTime { get { return Time.time - _startTime; } } | |
void Start() | |
{ | |
Reset(); | |
} | |
public void Reset() | |
{ | |
_startTime = Time.time; | |
} | |
float? _beforePauseTimeScale; | |
// call from OnGUI() | |
public void DebugMenu() | |
{ | |
GUILayout.Label("TimeDebugger"); | |
Indent(() => | |
{ | |
GUI.enabled = !_isSkipping; | |
using (var h = new GUILayout.HorizontalScope()) | |
{ | |
GUILayout.Label("Time:[" + modeTime.ToString("f") + "]"); | |
GUILayout.Label("Speed"); | |
Time.timeScale = float.Parse(GUILayout.TextField(Time.timeScale.ToString(), GUILayout.MinWidth(70f))); | |
if (GUILayout.Button("Reset")) Time.timeScale = 1f; | |
var isPausing = _beforePauseTimeScale.HasValue; | |
if (GUILayout.Button(isPausing ? "Play" : "Pause", GUILayout.Width(100))) | |
{ | |
if (isPausing) | |
{ | |
Time.timeScale = _beforePauseTimeScale.Value; | |
_beforePauseTimeScale = null; | |
} | |
else | |
{ | |
_beforePauseTimeScale = Time.timeScale; | |
Time.timeScale = 0f; | |
} | |
} | |
} | |
using (var h2 = new GUILayout.HorizontalScope()) | |
{ | |
using (var v = new GUILayout.VerticalScope()) | |
{ | |
using (var h3 = new GUILayout.HorizontalScope()) | |
{ | |
GUILayout.Label("Slow", GUILayout.Width(50f)); | |
Time.timeScale = GUILayout.HorizontalSlider(Time.timeScale, 0f, 1f, GUILayout.MinWidth(200)); | |
} | |
using (var h4 = new GUILayout.HorizontalScope()) | |
{ | |
GUILayout.Label("FF", GUILayout.Width(50f)); | |
Time.timeScale = GUILayout.HorizontalSlider(Time.timeScale, 1f, 100f, GUILayout.MinWidth(200)); | |
} | |
} | |
} | |
{ | |
using (var h = new GUILayout.HorizontalScope()) | |
{ | |
if (GUILayout.Button("Skip[sec]")) | |
{ | |
StartCoroutine(Skip(_skipSec)); | |
} | |
_skipSec = (int)Slider(_skipSec, 0, 100, ""); | |
} | |
if (_presets.Any()) | |
{ | |
using (var h = new GUILayout.HorizontalScope()) | |
{ | |
GUILayout.Label("Preset:"); | |
_presets.ForEach(p => | |
{ | |
var enabled = GUI.enabled; | |
GUI.enabled = GUI.enabled && (p.time > modeTime); | |
if (GUILayout.Button(p.name + "(" + p.time + ")")) | |
{ | |
StartCoroutine(Skip(p.time - modeTime)); | |
} | |
GUI.enabled = enabled; | |
}); | |
} | |
} | |
} | |
GUI.enabled = true; | |
}); | |
} | |
IEnumerator Skip(float sec) | |
{ | |
_isSkipping = true; | |
{ | |
yield return new WaitForEndOfFrame(); | |
var audioAndTimes = FindObjectsOfType<AudioSource>() | |
.Where(a => a.isActiveAndEnabled && a.clip != null) | |
.Select(a => new { audio = a, targetSample = a.timeSamples + Mathf.CeilToInt(a.clip.samples * sec / a.clip.length) }) | |
.ToList(); | |
audioAndTimes.ForEach(at => at.audio.Pause()); | |
float remain = sec; | |
while (remain > 0f) | |
{ | |
Time.timeScale = Mathf.Min(100f, remain / Time.deltaTime); // timeScale range 0~100 | |
yield return new WaitForEndOfFrame(); | |
remain -= Time.deltaTime; | |
} | |
Time.timeScale = 1f; | |
audioAndTimes.ForEach(at => | |
{ | |
if (at.audio.clip.samples < at.targetSample) | |
{ | |
at.audio.Stop(); | |
} | |
else { | |
at.audio.timeSamples = at.targetSample; | |
at.audio.UnPause(); | |
} | |
}); | |
} | |
_isSkipping = false; | |
} | |
public static float Slider(float v, float min, float max, string label = "") | |
{ | |
using (var h = new GUILayout.HorizontalScope()) | |
{ | |
if (!string.IsNullOrEmpty(label)) GUILayout.Label(label); | |
v = GUILayout.HorizontalSlider(v, (float)min, (float)max, GUILayout.MinWidth(200)); | |
v = float.Parse(GUILayout.TextField(v.ToString(), GUILayout.MinWidth(70f))); | |
} | |
return v; | |
} | |
public static void Indent(Action action) | |
{ | |
const int TAB = 20; | |
using (var h = new GUILayout.HorizontalScope()) | |
{ | |
GUILayout.Space(TAB); | |
using (var v = new GUILayout.VerticalScope()) | |
{ | |
action(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment