How to manually pause/unpause by script?

So Unity editor has button of [Pause] beside of [Play] button.

I want that pause button’s effect.

How can this be done via scripting?

So my problem is after I save my game via serialization solution in asset store,

Unity gui’s label text being broken.

This well fixed by pause game, and then unpause game via unity editor’s button.

Not sure if there is a way to replicate the pause function exactly.

This thread here might help you.

I myself use the key press P and then set speeds to 0 on objects or something to that effect.

Just write:

EditorApplication.isPaused = true;

Obviously you can only do this in an editor script - it makes no sense to try to do this from a non-editor script because it’s an editor-only feature. You can control the play button state in the same way, and also execute single frame steps - read the EditorApplication documentation for details on these.

10 Likes

from runtime scripts you can use Debug.Break() to pause execution

10 Likes

I know you want the editor.Pause but is the Timescale of the game any good to you? I used it in the following example:

I had a problem where I was debugging which AI would get caught on obstacles. I made a script that samples their position numerous times and provides an average distance between all the distance elements. This meant that if the average distance between the Vector3 values in this array was too low; the AI is stuck.

So I made this condition play a sound effect to get my attention so I could do other stuff instead of watch them run around for ages. Even with the alarm, sometimes by the time I found the AI, it had become unstuck due to the Timescale being so fast. This is because I had increased the timescale to speed up the time until they got stuck because I was trying to improve their code.

So I wanted the editor to pause. Seeing that it required extra effort I realized I am already using the Timescale function and so why not just use this so that the game is slowed down to an almost standstill. So I did. It’s not a pause but it’s kind of better than a pause because if the AI was not really stuck but just triggered an alarm, I have the logic still running so that it can get back to work.

If the AI manages to unstuck itself then I have the code check that average distance between positions and if it’s greater than the alarm value it will restore the timescale back to maximum so that it speeds up the time until the AI get stuck again.

1 Like

The easiest way is to add Debug.Break() and call it from a keypress. You can pause after the frame start and before the frame ends. Somewhere in-between. ( @danielwilkins , is that correct? )

this guy has a tutorial on that

I ran into an issue where I wanted to be able to step though what was happening frame by frame, but I didn’t want a debugger break, because that changed the behavior of the code. So I put this code into where I thought the problem was beginning

#if UNITY_EDITOR
UnityEditor.EditorApplication.isPaused = true;
#endif

That clicked the pause button on the editor as soon as my code was triggered, letting me step through frame by frame and look at values in the editor. Naturally this wouldn’t work in a build, but if you need to pause the editor and don’t have catlike reflexes to hit the pause button manually, this is one way to do it.

Incorrect. The pause happens at the end of the frame.

1 Like

Since this seems to high in google search for pausing/unpausing the editor.

you can use Ctrl+Shift+P during play to pause/unpause. Incase that’s all you want.

1 Like

Ok, It’s really useful ;
and I think it works as well

#if UNITY_EDITOR
UnityEditor.EditorApplication.isPaused = true;
#endif
2 Likes
Debug.Break();
1 Like

This does not work anymore with Unity 2022
At least not on Mac (CMD + SHIFT + P)
It used to work in Unity 2021