A
A
Alexander2021-05-05 17:33:19
C++ / C#
Alexander, 2021-05-05 17:33:19

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

1 answer(s)
A
Alexander, 2021-05-05
@GreenProg

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 question

Ask a Question

731 491 924 answers to any question