1
\$\begingroup\$

Please Explain me how to Save or Set Audiolistner or scene Audio to playerprefs to control game all scene sound by a simple Button On and Off ..

Please Give Me C sharp Script as a Example To Mute and UnMute Full Game All Scenes Sound from Scene 1.

Thanks ..Waiting

\$\endgroup\$
0
\$\begingroup\$

First of all you need to use is the pattern below to control all the scene's sounds in one place:

-Create a MusicManager class. It would better be singleton.

-Initialize an object from this class, and use DontDestroyOnLoad() in order to keep musicmanager alive when scene changes;

-Declare a static methods like:

  • public static void PlaySound(AudioClip clip);
  • public static void SetVolume(float volume);
  • public static void Mute(){audioSourceObject.volume = 0f;}

-Then when it comes to play any audio, use these methods for playing sound by giving the sounds as a parameter.

-Also when it comes to mute just call the mute function.

For saving purposes:

PlayerPrefs.SetFloat("soundVolume", currentVolume);  //Note that you need to keep the volume in a variable called currentVolume or whatever you name it.

And also when the game starts, put the code below in MusicManager class' void start() method:

currentVolume = PlayerPrefs.GetFloat("soundVolume");
SetVolume(currentVolume);    //This function has been defined above

By doing these, if the player mutes the game, the musicmanager will set the volume to 0f, then save it as 0f when the game gets closed. And also be re-set as 0f when the game is opened again.

Hope this helps!

\$\endgroup\$
  • \$\begingroup\$ Your'e absolutely welcome! \$\endgroup\$ – Tolga Şen Aug 8 '17 at 8:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.