- Mar 29, 2022
- 30
- 3
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Audio;
using UnityEngine.UI;
public class Volumen : MonoBehaviour
{
[SerializeField] private AudioMixer myMixer;
[SerializeField] private Slider sliderSFX;
[SerializeField] private Slider sliderBGM;
[SerializeField] private Image imagenMuteBGM;
[SerializeField] private Image imagenMuteSFX;
[SerializeField] private Image imagenLowVolumeSFX;
[SerializeField] private Image imagenNormalVolumeSFX;
[SerializeField] private Image imagenLowVolumeBGM;
[SerializeField] private Image imagenNormalVolumeBGM;
private void Awake()
{
if (PlayerPrefs.HasKey("SFX"))
{
LoadSFXVolumen();
}
else
{
SetSFXVolume();
}
if (PlayerPrefs.HasKey("BGM"))
{
LoadBGMVolume();
}
else
{
SetBGMVolume();
}
EstoyMuted();
}
public void SetSFXVolume()
{
float volumeSFX = sliderSFX.value;
myMixer.SetFloat("SFX", Mathf.Log10(volumeSFX) * 20);
PlayerPrefs.SetFloat("SFX", sliderSFX.value);
EstoyMuted();
PlayerPrefs.Save();
}
public void SetBGMVolume()
{
float volumeBGM = sliderBGM.value;
myMixer.SetFloat("BGM", Mathf.Log10(volumeBGM) * 20);
PlayerPrefs.SetFloat("BGM", sliderBGM.value);
PlayerPrefs.Save();
EstoyMuted();
}
private void LoadSFXVolumen()
{
sliderSFX.value = PlayerPrefs.GetFloat("SFX");
SetSFXVolume();
}
private void LoadBGMVolume()
{
sliderBGM.value = PlayerPrefs.GetFloat("BGM");
SetBGMVolume();
}
public void EstoyMuted()
{
myMixer.GetFloat("SFX", out float volumeSFX);
myMixer.GetFloat("BGM", out float volumeBGM);
imagenMuteSFX.enabled = false;
imagenLowVolumeSFX.enabled = false;
imagenNormalVolumeSFX.enabled = false;
if (volumeSFX <= -80f)
{
imagenMuteSFX.enabled = true;
}
else if (volumeSFX <= -6f)
{
imagenLowVolumeSFX.enabled = true;
}
else
{
imagenNormalVolumeSFX.enabled = true;
}
imagenMuteBGM.enabled = false;
imagenLowVolumeBGM.enabled = false;
imagenNormalVolumeBGM.enabled = false;
if (volumeBGM <= -80f)
{
imagenMuteBGM.enabled = true;
}
else if (volumeBGM <= -6f)
{
imagenLowVolumeBGM.enabled = true;
}
else
{
imagenNormalVolumeBGM.enabled = true;
}
}
}
I've been trying for a while, but I can't find the error. I managed to make the graphics options work, etc. But sound is the only one that doesn't save the values for me.
The thing is, when I enter the game (main menu), the soundtrack plays super loud, and if I go to the options (after previously entering them), the sliders are saved and the soundtrack volume decreases. But until I enter the options, the sound doesn't adjust.
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Audio;
using UnityEngine.UI;
public class Volumen : MonoBehaviour
{
[SerializeField] private AudioMixer myMixer;
[SerializeField] private Slider sliderSFX;
[SerializeField] private Slider sliderBGM;
[SerializeField] private Image imagenMuteBGM;
[SerializeField] private Image imagenMuteSFX;
[SerializeField] private Image imagenLowVolumeSFX;
[SerializeField] private Image imagenNormalVolumeSFX;
[SerializeField] private Image imagenLowVolumeBGM;
[SerializeField] private Image imagenNormalVolumeBGM;
private void Awake()
{
if (PlayerPrefs.HasKey("SFX"))
{
LoadSFXVolumen();
}
else
{
SetSFXVolume();
}
if (PlayerPrefs.HasKey("BGM"))
{
LoadBGMVolume();
}
else
{
SetBGMVolume();
}
EstoyMuted();
}
public void SetSFXVolume()
{
float volumeSFX = sliderSFX.value;
myMixer.SetFloat("SFX", Mathf.Log10(volumeSFX) * 20);
PlayerPrefs.SetFloat("SFX", sliderSFX.value);
EstoyMuted();
PlayerPrefs.Save();
}
public void SetBGMVolume()
{
float volumeBGM = sliderBGM.value;
myMixer.SetFloat("BGM", Mathf.Log10(volumeBGM) * 20);
PlayerPrefs.SetFloat("BGM", sliderBGM.value);
PlayerPrefs.Save();
EstoyMuted();
}
private void LoadSFXVolumen()
{
sliderSFX.value = PlayerPrefs.GetFloat("SFX");
SetSFXVolume();
}
private void LoadBGMVolume()
{
sliderBGM.value = PlayerPrefs.GetFloat("BGM");
SetBGMVolume();
}
public void EstoyMuted()
{
myMixer.GetFloat("SFX", out float volumeSFX);
myMixer.GetFloat("BGM", out float volumeBGM);
imagenMuteSFX.enabled = false;
imagenLowVolumeSFX.enabled = false;
imagenNormalVolumeSFX.enabled = false;
if (volumeSFX <= -80f)
{
imagenMuteSFX.enabled = true;
}
else if (volumeSFX <= -6f)
{
imagenLowVolumeSFX.enabled = true;
}
else
{
imagenNormalVolumeSFX.enabled = true;
}
imagenMuteBGM.enabled = false;
imagenLowVolumeBGM.enabled = false;
imagenNormalVolumeBGM.enabled = false;
if (volumeBGM <= -80f)
{
imagenMuteBGM.enabled = true;
}
else if (volumeBGM <= -6f)
{
imagenLowVolumeBGM.enabled = true;
}
else
{
imagenNormalVolumeBGM.enabled = true;
}
}
}
I've been trying for a while, but I can't find the error. I managed to make the graphics options work, etc. But sound is the only one that doesn't save the values for me.
The thing is, when I enter the game (main menu), the soundtrack plays super loud, and if I go to the options (after previously entering them), the sliders are saved and the soundtrack volume decreases. But until I enter the options, the sound doesn't adjust.