hi, i have the following class responsible for music and sound effects ui button sprite swap and also their functionality.
however, now sprite changes correctly , but music and sound effects are not stopped and resumed correctly.
each time game starts they mess. any idea to fix that?
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Audio;
public class SoundManager : MonoBehaviour
{
public AudioMixer audioMixer;
[SerializeField]
private Image musicOnIcon;
[SerializeField]
private Image musicOffIcon;
[SerializeField]
private Image sfxOnIcon;
[SerializeField]
private Image sfxOffIcon;
private bool musicIsMuted = false;
private bool sfxIsMuted = false;
// sprites save and load correctly.
// music and sfx does not respond correctly base on sprite mode.
// checking start method for music and sfx initializations.
private void Start()
{
//if the key musicmuted value is false, then value 0 is set to that key.
if (!PlayerPrefs.HasKey("musicmuted"))
{
PlayerPrefs.SetInt("musicmuted", 0);
Load();
}
else
{
Load();
}
if (!PlayerPrefs.HasKey("sfxmuted"))
{
PlayerPrefs.SetInt("sfxmuted", 0);
Load();
}
else
{
Load();
}
UpdateMusicIcon();
UpdateSfxIcon();
musicIsMuted = PlayerPrefs.GetInt("musicmuted") == 1;
sfxIsMuted = PlayerPrefs.GetInt("sfxmuted") == 1;
}
public void OnMusicButtonPressed()
{
if (musicIsMuted == false)
{
musicIsMuted = true;
audioMixer.SetFloat("music", -80f);
}
else
{
musicIsMuted = false;
audioMixer.SetFloat("music", -0f);
}
UpdateMusicIcon();
Save();
}
public void OnSFXButtonPressed()
{
if (sfxIsMuted == false)
{
sfxIsMuted = true;
audioMixer.SetFloat("sfx", -80f);
}
else
{
sfxIsMuted = false;
audioMixer.SetFloat("sfx", 0f);
}
UpdateSfxIcon();
Save();
}
private void UpdateMusicIcon()
{
if (musicIsMuted == false)
{
musicOnIcon.enabled = false;
musicOffIcon.enabled = true;
}
else
{
musicOnIcon.enabled = true;
musicOffIcon.enabled = false;
}
}
private void UpdateSfxIcon()
{
if (sfxIsMuted == false)
{
sfxOnIcon.enabled = false;
sfxOffIcon.enabled = true;
}
else
{
sfxOnIcon.enabled = true;
sfxOffIcon.enabled = false;
}
}
private void Save()
{
PlayerPrefs.SetInt("musicmuted", musicIsMuted ? 1 : 0);
PlayerPrefs.SetInt("sfxmuted", sfxIsMuted ? 1 : 0);
}
private void Load()
{
musicIsMuted = PlayerPrefs.GetInt("musicmuted") == 1;
sfxIsMuted = PlayerPrefs.GetInt("sfxmuted") == 1;
}
}
What does ‘mess’ and ‘not stopped correctly’ mean exactly? It is muted but doesn’t pause or is it not muted? What are those floats you’re setting on your AudioMixer? You may want to call Pause on your audio source if you want to stop the music (or Stop, depends).
what i mean is imagine you have clicked on both music and sound effect button. so now you have muted the music and ui sound effect. ja?
however for me i need to click 2 times on each button to have music and sound effects muted
the other problem is when game is stopped and replayed again, music and sound effects mute is broken and music plays at the start.
how may i fix ui button click duplicate and cancellation of mute at the start please?
so What are those floats you’re setting on your AudioMixer?
in order to have control on each signal line , i.e music and sound effects , in the audio mixer there are two channel.and those floats are from -80 to 0 for mute and unmute the audio signal. each pair for music and sound effects.
hope it is clear to you what i need to be done with music settings.
i have now used your class instead, however for some reason, as soon as game is ended and new session is started music and sound effects keep playing, but i did muted them the last time before leaving the game.
something could need to be fixed in awake method. or?
in some way i need to tell to the game the last time game is ended , music and sound effects were muted. and dont make them work now until user decides about them.
how to add this fix please?
otherwise, when game is ended when music and sound effects are not muted, in new session they keep playing as man expects.
IDK what is happening in your project, I tried this code now without audio, it is working. I added four images and all of them turn on/off as they should.