I have the following script which I use within my settings menu : -
using UnityEngine;
using UnityEngine.Audio;
using UnityEngine.UI;
public class settingsScreen : MonoBehaviour {
[Header("AudioMixer Reference")]
public AudioMixer mixer;
[Header("Slider References")]
public Slider masterSlider;
public Slider musicSlider;
public Slider soundFxSlider;
public void masterVolumeChangeValue(float value)
{
mixer.SetFloat("volMaster", value);
PlayerPrefs.SetFloat("volMaster", value);
PlayerPrefs.Save();
}
public void musicVolumeChangeValue(float value)
{
mixer.SetFloat("volMusic", value);
}
public void soundFxVolumeChangeValue(float value)
{
mixer.SetFloat("volSoundFx", value);
}
public void closeSettings ()
{
gameObject.SetActive(false);
}
}
Right now, Iām trying to save this information to PlayerPrefs, Iām currently testing with the MasterVolume :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class musicPlayer : MonoBehaviour {
static protected musicPlayer s_Instance;
static public musicPlayer instance { get { return s_Instance; } }
void Awake() {
if (s_Instance !=null)
{
Destroy(gameObject);
return;
}
s_Instance = this;
AudioListener.pause = false;
PlayerPrefs.GetFloat("volMaster");
GetComponent<AudioSource>().Play();
DontDestroyOnLoad(gameObject);
}
}
where I have used
PlayerPrefs.GetFloat("volMaster");
to try and load my āsavedā mastervolume, Iām aware that this sort of stuff isnāt supposed to work within the Unity Editor, but subsequent external builds basically prove it doesnāt work at all, any pointers would be greatly appreciated ?
The problem here is that youāre retrieving the āvolMasterā value, but then not doing anything with it. You need to assign it to something, or pass it as a parameter. For example,
in the Start function I also added a new check to make sure PlayerPrefs has the key volMaster, and if not, apply it
it seems no matter which way, other than removing it all together, I get any volume at all !? For some reason, the save seems to 0 out the volume all together ?
I looked that up to check, but it does in fact have an out parameter when used with an AudioMixer. I didnāt know that before. (reference: https://docs.unity3d.com/ScriptReference/Audio.AudioMixer.GetFloat.html)
Anyhow, because of the prior fix suggested here, playerprefs should be working.
So, Iāve rewritten my settings script, and this works much better now :
using UnityEngine;
using UnityEngine.Audio;
using UnityEngine.UI;
public class settingsScreen : MonoBehaviour {
[Header("AudioMixer Reference")]
public AudioMixer mixer;
[Header("Slider References")]
public Slider masterSlider;
public Slider musicSlider;
public Slider soundFxSlider;
public void masterVolumeChangeValue(float value)
{
mixer.SetFloat("volMaster", value);
mixer.GetFloat("volMaster", out value);
Debug.Log("Current volMaster Volume : "+value);
PlayerPrefs.SetFloat("volMaster", value);
PlayerPrefs.Save();
}
public void musicVolumeChangeValue(float value)
{
mixer.SetFloat("volMusic", value);
mixer.GetFloat("volMusic", out value);
Debug.Log("Current volMusic Volume : "+value);
PlayerPrefs.SetFloat("volMusic", value);
PlayerPrefs.Save();
}
public void soundFxVolumeChangeValue(float value)
{
mixer.SetFloat("volSoundFx", value);
mixer.GetFloat("volSoundFx", out value);
Debug.Log("Current volSoundFx Volume : "+value);
PlayerPrefs.SetFloat("volSoundFx", value);
PlayerPrefs.Save();
}
public void closeSettings ()
{
gameObject.SetActive(false);
}
void Start () {
if (PlayerPrefs.HasKey("volMaster")) {
masterSlider.GetComponent<Slider>().value = PlayerPrefs.GetFloat("volMaster");
}
if (PlayerPrefs.HasKey("volMusic")) {
musicSlider.GetComponent<Slider>().value = PlayerPrefs.GetFloat("volMusic");
}
if (PlayerPrefs.HasKey("volSoundFx")) {
soundFxSlider.GetComponent<Slider>().value = PlayerPrefs.GetFloat("volSoundFx");
}
}
}
However, I think my main problem lies in my musicPlayer.cs, every single time it runs, the volume is set to 0 ?
Now, my thinking, my sliders have a audiomixer range of between -80db and 0, so when the volume is loudest, the slider has a value of 0, could it be, that when saving my slider value, the volume is being read back in from the playerprefs as literally a value of 0, i.e. no volume ?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class musicPlayer : MonoBehaviour {
static protected musicPlayer s_Instance;
static public musicPlayer instance { get { return s_Instance; } }
void Awake() {
if (s_Instance !=null)
{
Destroy(gameObject);
return;
}
s_Instance = this;
AudioListener.pause = false;
GetComponent<AudioSource>().volume = PlayerPrefs.GetFloat("volMaster", 1);
GetComponent<AudioSource>().Play();
DontDestroyOnLoad(gameObject);
}
}
Thatās the trouble, Iām unable to think at all, itās completely befuddling.
Clearly Iām out of my depth here, I think I may just have be having a problem all together with āVOLUMEā, maybe I shouldnāt be working with it all and instead somehow apply the mixer settings instead, Iāve noticed I can change the settings within my slide range, and the volume āAUDIBLYā changes, but the volume slider on my AUDIOSOURCE does not, which suggests I need to reference something else all together ( i.e. tell it to use the mixer directly somehow ), call me perplexedā¦
No, it does change if I manually move it, however, the audioMixer controls the volume independently of this, if I change my scripted sliders, the volume changes audibly, but Iāve noticed the audiosource volume of my persistent theme music stays at 1. A picture tells a 1000 words, so :
Youā'll notice the audiosource volume is 1 in the top part of the image, then once Iām within the settings screens, even though I have changed the sliders and audibly changed the volume, the volume is still at 1, so maybe volume is the wrong value to target, I somehow need to tell my music audiosource to find the mixer value ( volMaster ) instead and apply that ( but not to volume ? ).
Sorry, not sure. Iād check first by ensuring the variable for volume is between 0 and 1 and afaik the volume is adjustable. Iāve never used a mixer before, though, so I guess I canāt say for certain in that respect.
I hope you figure it out
ack, Iām sure I will, I usually just keep stabbing in the dark until something bleedsā¦
removing the GetComponent().volume all together seems like Iām getting somewhere, which kind of works, but doesnāt respect the playerprefs volume setting, itās always highest volume, , stab, stab, stabā¦
This āalmostā seems like itās working now, but it appears to have hi lighted another problem, when I go back into the settings menu, I get a half reduction in overall volume, oh, well, onwardsā¦
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Audio;
public class musicPlayer : MonoBehaviour {
static protected musicPlayer s_Instance;
static public musicPlayer instance { get { return s_Instance; } }
void Awake() {
if (s_Instance !=null)
{
Destroy(gameObject);
return;
}
s_Instance = this;
AudioListener.pause = false;
if (PlayerPrefs.HasKey("volMaster")) {
float currentVolMaster = PlayerPrefs.GetFloat("volMaster",1);
// Decibel to Linear calculation
currentVolMaster = Mathf.Pow(10.0f, currentVolMaster / 20.0f);
Debug.Log("currentVol :"+currentVolMaster);
GetComponent<AudioSource>().volume = currentVolMaster;
}
GetComponent<AudioSource>().Play();
DontDestroyOnLoad(gameObject);
}
}
So, it seems I pass my volume value to the AudioSource().volume component, but the AudioMixer volume remains at 0, so it seems like I need to pass that value to the audiomixer at the start instead of the volume, yeah ? Any ideas how I go about that ?
Awesome. Glad to see you solved it. It kinda looks very similar to me But Iām glad ya got it.
I think you could even shorten the second script, because if the key is not found, you get ā0ā (zero) for āfreeā
But that makes no real difference.