- Home /
Get the functionality of the Editor's 'Step' and 'Pause' at runtime?
Greetings.
My tester wanted the ability to step frame by frame on the actual device (Android/iOS) cause it would help catch bugs.
I know you could easily build your game with single frame stepping in mind and it would work but I'm in a point where it's hard to add that feature in, I would have to mold everything to play nicely with it.
I thought it would be nice to have the built-in Editor Pause/Step which works perfectly. I saw that the functions are in EditorApplication.Pause() and EditorApplication.Step() but obviously that's editor only.
Is there anyway to get that same functionality off of Unity's runtime libraries?
Thanks.
EDIT: Thanks to @gorsefan here's the final code I used with the timeScale, works very well as long as you're Time.deltaTime in your game and not an unscaled deltaTime.
Just attach this script to a GameObject and call Toggle
(turns it on/off) or Step
to step a single frame.
using UnityEngine;
using System.Collections;
public class FrameStepper : MonoBehaviour
{
bool IsOn;
bool IsStepping;
public void Toggle()
{
IsOn = !IsOn;
}
public void Step()
{
IsStepping = true;
}
void Update()
{
if (Input.GetKeyDown(KeyCode.S))
{
Toggle();
}
else if (Input.GetKeyDown(KeyCode.N))
{
Step();
}
if (IsStepping)
{
Time.timeScale = 1;
IsStepping = false;
}
else
{
Time.timeScale = IsOn ? 0 : 1;
}
}
}
Answer by gorsefan · Jul 08, 2016 at 08:19 PM
Here's some of my code which should give you the idea
public void OnPlayPause ()
{
if (Time.timeScale == 1)
{
// We are running, so pause
playPauseIcon.sprite = playSprite;
Time.timeScale = 0;
return;
}
playPauseIcon.sprite = pauseSprite;
Time.timeScale = 1;
}
To go frame-by-frame, on entering that mode i would pause the game. Then on "next step" input I would have a coroutine calling OnPlayPause(), yielding a frame, then calling it again, then finally returning.
Thanks for the reply. I'm doing something similar except I was calling our game's clock pause/resume functions thinking that they were setting time scale to 0/1 but it was actually just a isPaused boolean flag which is why it wasn't giving me the effect I wanted exactly. timeScale is much better definitely assu$$anonymous$$g the code uses deltaTime and not some unscaled deltaTime which we don't.