Answer the question
In order to leave comments, you need to log in
How to turn off the sound in the game when minimizing the browser?
I made a browser game on Unity WebGL, it is necessary that when the window with the game is minimized, the sound is turned off. How to write it in code?
Answer the question
In order to leave comments, you need to log in
I figured it out, maybe it will help someone
public class FocusSoundController : MonoBehaviour
{
void OnApplicationFocus(bool hasFocus)
{
Silence(!hasFocus);
}
void OnApplicationPause(bool isPaused)
{
Silence(isPaused);
}
private void Silence(bool silence)
{
AudioListener.pause = silence;
// Or / And
AudioListener.volume = silence ? 0 : 1;
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question