Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by rodude123 · Apr 21, 2016 at 09:39 AM · uiaudiogamevolume

changing game volume

I have this script here which changes the volume in the main menu but how would I change the volume for the whole game not just the main menu scene. I am not new to unity but I am new to the new unity 5 UI and audio. So if anyone could help that would be great.

 public void Volume(float newVolume)
     {
         AudioListener.volume = newVolume;
     }


Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by Quertie · May 01, 2016 at 01:39 PM

Perhaps you could use the PlayerPrefs to store the newVolume float, since PlayerPrefs are saved between sessions (and, a fortiori, also between scenes!)

So, when you change the volume:

 using UnityEngine;
 using System.Collections;
 
 public class Volume: MonoBehaviour
 {
     public void changeVolume(float newVolume)
     {
         PlayerPrefs.SetFloat("volume", newVolume);
         AudioListener.volume = PlayerPrefs.GetFloat("volume");
     }
 }
     

On every scene, you can then add:

 using UnityEngine;
 using System.Collections;
 
 public class GameController : MonoBehaviour
 {
     private void Start()
     {
         AudioListener.volume = PlayerPrefs.GetFloat("volume");
     }
 }

Hope this helps :)

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image rodude123 · May 01, 2016 at 05:58 PM 0
Share

Thanks didn't think of this will add this

avatar image Quertie rodude123 · May 01, 2016 at 06:03 PM 1
Share

You're welcome :)

avatar image rodude123 Quertie · May 07, 2016 at 06:19 PM 0
Share

I read the docuentation, didn't find what I needed. Will the player prefs store data even after the user quits the application. I am about to use this is for high score, so will the high score save or is there another way to do this

avatar image
5

Answer by v01pe_ · Aug 23, 2016 at 10:01 AM

You can also use an Audio Mixer for this. It's a bit of work to set it up, but once it's done it's a very powerful tool.

To change anything, you first have to expose the desired parameter to be able to manipulate it - this can be almost every value from the mixer including effects, etc.

After creating you mixer, you have to assign it to all AudioSources that shall be affected as Output

  1. Open the Audio Mixer window and select your Audio Mixer

  2. Select the group you want to manipulate ( e.g.`Master` for main volume)

  3. In the Inspector right click on the label Volume

  4. Click on Expose 'Volume (of Master)' to script

  5. Rename the Exposed Parameter in the dropdown in the top right corner of the Audio Mixer window to e.g. MasterVolume

  6. Read/ Write the volume from the script

Example use in a component

 public class SetAudioParameter : MonoBehaviour
 {
     public AudioMixer mixer;
     public string parameterName = "MasterVolume";
 
     protected float Parameter
     {
         get
         {
             float parameter;
             mixer.GetFloat(parameterName, out parameter);
             return parameter;
         }
         set
         {
             mixer.SetFloat(parameterName, value);
         }
     }
 }

Of course, you first have to drag in your AudioMixer to the Component's field in the editor, to make it work.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
2

Answer by Ebonicus · Dec 13, 2016 at 06:04 AM

The video actually offers a very slick method of doing this, and uses a very simple single script that is public and attached to some canvas or gameobject so the functions are available publicly.

 public class MixLevels : MonoBehaviour {
 
     public AudioMixer masterMixer;
 
 
     public void SetSfxLevel(float sfxLvl) {
     
         masterMixer.SetFloat ("volSfx", sfxLvl);
     
     }
 
     public void SetMusicLevel(float musicLvl) {
 
         masterMixer.SetFloat ("volMusic", musicLvl);
 
     }
 
 
 }


Then you just have two sliders, and they just call the functions directly from the inspector settings of the sliders:

alt text

This allows you to change two game volume options with just one script and two UI sliders.


capture.jpg (18.6 kB)
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Tazling · Feb 01, 2017 at 12:42 AM 0
Share

Ebonicus you refer to a video... I didn't see a link in the thread to a video. I would like to view it, because I tried the method you describe and have not got it to work yet.

avatar image RealPowerPaul Tazling · Jun 22, 2019 at 07:38 PM 1
Share

Even when you asked years ago, i want to share the answer for folks in the future. The video he mentioned is the Unity tutorial here: https://unity3d.com/de/learn/tutorials/topics/audio/exposed-audiomixer-parameters

I found it, just because of googling one line of code he mentioned. A hint for everybody how to find a source of a published code.

Greetings!

avatar image Tazling · Feb 01, 2017 at 09:06 AM 0
Share

Ebonicus you refer to a video... I have read the whole thread a couple of times and didn't see a link to a video. Which video? I tried the method you describe and so far no luck, so I would like to find that video. Unity is funny that way -- sometimes it seems insanely easy to do something that you would think was really complicated and difficult, other times it seems insanely difficult to do something that you would think would be simple and easy (like presenting the user with volume controls!)...

avatar image hacky97 · Oct 28, 2017 at 11:25 PM 1
Share

Don't forget to add: using UnityEngine.Audio; to the monobehaviour.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

76 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Volume Slider - Play a sound after modifying slider value and releasing the mouse 1 Answer

[Daydream] Disable default volume menu? 0 Answers

Why is my backtrack's volume getting lower? 0 Answers

How to mute my volume 2 Answers

Linear volume control of AudioMixer groups 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges