Pause button support?

Is there built in support that will pause everything in my scene?

Most notably - audio playing and animations that are playing. I’m making an interactive storybook that has music and animation synced up.

Or perhaps a button that detects all playing animation and all playing music and pauses it (I don’t want to “stop” them because I don’t want them to reset when the pause state is lifted.)

Thanks :slight_smile:

Does the pause button work (next to the play button)?

Yes, it does work. Does that somehow prove that it can be done easily in the executable (and I should also inquire - the iOS version?)

Sorry, don’t know, I was only mentioning the pause button if you didn’t know about it.
It could happen. :slight_smile:

No, it’s something that you program. Unity can’t know what you want paused and what you don’t. i.e., if everything was paused, there would be no way to get back out of pause mode. Also, you might want to have some kind of animation, like “Paused” blinking on-screen or something, etc.

–Eric

Unity3D could do it automatically using a stack of states. That said, just because Unity3D doesn’t, doesn’t mean you can’t. In general terms the solution is to create a reusable paused controller that does all of the hard work for you.

Probably the simplest way to pause things is to set your time scale to 0. That will pause animations, particles and anything else that uses delta time. To unpause, you set your time scale back to 1.

Adjusting the time scale won’t affect your sound effects and music, but you can set their pitch to 0 and get the same result. (then set pitch back to 1 to continue where they were) The trickiest part will be keeping track of all the audio sources so you can set their pitch when you pause.

Hope that helps!

Bummer - I thought the pause in editor was proof that it would be simple :slight_smile:

Sycle - I think you may have the most plausible way for me to do it, I’ll give it a shot!!

Thanks :slight_smile:

there is a simple way, question is how you get out of it again.
That simply way is Thread.Sleep() which really does what the editor function does too, pause the whole runtime basically.

There is no “pause XY but not XZ” functionality otherwise.

Question is what you want to do. if its just meant to pause when not in focus, then disable “run in background” in the player settings

It is really just to stop an animated scene that is synchronized to music if the user wants to.

I have made a functioning Pause Button by applying this code to a button:

function OnMouseDown ()
{
Time.timeScale = 0;
audio.pitch = 0;
}

There are a few problems.

  1. I have to put the audio source on the button for the code to work as it is now.
  2. Other audio sources in the scene are unaffected.
  3. I can’t hit the button again to “Unpause”

I want a Pause button that does the following:

Sets the timescale to 0 when hit.
Sets the pitch to all audio sources in the scene to 0 when hit.
Reverses all of the above when hit again.
I’d like the texture/material on the button to swap when the button is tapped (so when the button is untapped it shows a pause icon, and when the scene is paused, it now shows a “Play” button)

Am I close to making this happen?

1/2) Use the observer pattern → make a component that handles the local audio soruces and registers against an “audio manager” so you can easily pause and unpause audio (audio.pause() ;))

  1. if you use Unitys OnGUI you can hit unpause again as ongui does not respect timescale

so yeah you are close but I would do it differently for clean and reliable control over it.