Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Music and sound effects from audio mixer

Discussion in 'Scripting' started by hoshyargames, May 2, 2021.

  1. hoshyargames

    hoshyargames

    Joined:
    May 2, 2021
    Posts:
    24
    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?

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using UnityEngine.Audio;
    4.  
    5. public class SoundManager : MonoBehaviour
    6. {
    7.     public AudioMixer audioMixer;
    8.  
    9.     [SerializeField]
    10.     private Image musicOnIcon;
    11.  
    12.     [SerializeField]
    13.     private Image musicOffIcon;
    14.  
    15.     [SerializeField]
    16.     private Image sfxOnIcon;
    17.  
    18.     [SerializeField]
    19.     private Image sfxOffIcon;
    20.  
    21.     private bool musicIsMuted = false;
    22.     private bool sfxIsMuted = false;
    23.  
    24.     // sprites save and load correctly.
    25.     // music and sfx does not respond correctly base on sprite mode.
    26.     // checking start method for music and sfx initializations.
    27.  
    28.  
    29.     private void Start()
    30.     {
    31.         //if the key musicmuted value is false, then value 0 is set to that key.
    32.         if (!PlayerPrefs.HasKey("musicmuted"))
    33.         {
    34.             PlayerPrefs.SetInt("musicmuted", 0);
    35.             Load();
    36.         }
    37.         else
    38.         {
    39.             Load();
    40.         }
    41.  
    42.         if (!PlayerPrefs.HasKey("sfxmuted"))
    43.         {
    44.             PlayerPrefs.SetInt("sfxmuted", 0);
    45.             Load();
    46.         }
    47.         else
    48.         {
    49.             Load();
    50.         }
    51.  
    52.         UpdateMusicIcon();
    53.         UpdateSfxIcon();
    54.         musicIsMuted = PlayerPrefs.GetInt("musicmuted") == 1;
    55.         sfxIsMuted = PlayerPrefs.GetInt("sfxmuted") == 1;
    56.     }
    57.  
    58.     public void OnMusicButtonPressed()
    59.     {
    60.         if (musicIsMuted == false)
    61.         {
    62.             musicIsMuted = true;
    63.             audioMixer.SetFloat("music", -80f);
    64.         }
    65.         else
    66.         {
    67.             musicIsMuted = false;
    68.             audioMixer.SetFloat("music", -0f);
    69.         }
    70.  
    71.         UpdateMusicIcon();
    72.         Save();
    73.     }
    74.  
    75.     public void OnSFXButtonPressed()
    76.     {
    77.         if (sfxIsMuted == false)
    78.         {
    79.             sfxIsMuted = true;
    80.             audioMixer.SetFloat("sfx", -80f);
    81.         }
    82.         else
    83.         {
    84.             sfxIsMuted = false;
    85.             audioMixer.SetFloat("sfx", 0f);
    86.         }
    87.  
    88.         UpdateSfxIcon();
    89.         Save();
    90.     }
    91.  
    92.     private void UpdateMusicIcon()
    93.     {
    94.         if (musicIsMuted == false)
    95.         {
    96.             musicOnIcon.enabled = false;
    97.             musicOffIcon.enabled = true;
    98.         }
    99.         else
    100.         {
    101.             musicOnIcon.enabled = true;
    102.             musicOffIcon.enabled = false;
    103.         }
    104.     }
    105.  
    106.     private void UpdateSfxIcon()
    107.     {
    108.         if (sfxIsMuted == false)
    109.         {
    110.             sfxOnIcon.enabled = false;
    111.             sfxOffIcon.enabled = true;
    112.         }
    113.         else
    114.         {
    115.             sfxOnIcon.enabled = true;
    116.             sfxOffIcon.enabled = false;
    117.         }
    118.     }
    119.  
    120.     private void Save()
    121.     {
    122.         PlayerPrefs.SetInt("musicmuted", musicIsMuted ? 1 : 0);
    123.         PlayerPrefs.SetInt("sfxmuted", sfxIsMuted ? 1 : 0);
    124.     }
    125.  
    126.     private void Load()
    127.     {
    128.         musicIsMuted = PlayerPrefs.GetInt("musicmuted") == 1;
    129.         sfxIsMuted = PlayerPrefs.GetInt("sfxmuted") == 1;
    130.     }
    131. }
    132.  
     
  2. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,826
    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).
     
    hoshyargames likes this.
  3. hoshyargames

    hoshyargames

    Joined:
    May 2, 2021
    Posts:
    24
    Hi and thanks for your help.

    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.
     
  4. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,826
    Try something like this:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using UnityEngine.Audio;
    4.  
    5. public class SoundManager : MonoBehaviour
    6. {
    7.     [SerializeField]
    8.     private AudioMixer audioMixer;
    9.     [SerializeField]
    10.     private Image musicOnIcon;
    11.     [SerializeField]
    12.     private Image musicOffIcon;
    13.     [SerializeField]
    14.     private Image sfxOnIcon;
    15.     [SerializeField]
    16.     private Image sfxOffIcon;
    17.  
    18.     private bool _musicIsMuted = false;
    19.     private bool _sfxIsMuted = false;
    20.  
    21.     private const string MUSIC_PREFS_KEY = "musicmuted";
    22.     private const string SFX_PREFS_KEY = "sfxmuted";
    23.     private const string AUDIO_MIXER_MUSIC = "music";
    24.     private const string AUDIO_MIXER_SFX = "sfx";
    25.     private const int MUTED = 1;
    26.     private const int NOT_MUTED = 0;
    27.     private const float MUTED_VALUE = -80f;
    28.     private const float NOT_MUTED_VALUE = 0f;
    29.  
    30.     private void Awake()
    31.     {
    32.         if (!PlayerPrefs.HasKey(MUSIC_PREFS_KEY)) PlayerPrefs.SetInt(MUSIC_PREFS_KEY, NOT_MUTED);
    33.         if (!PlayerPrefs.HasKey(SFX_PREFS_KEY)) PlayerPrefs.SetInt(SFX_PREFS_KEY, NOT_MUTED);
    34.         Load();
    35.  
    36.         UpdateMusicIcon();
    37.         UpdateSfxIcon();
    38.     }
    39.  
    40.     public void OnMusicButtonPressed()
    41.     {
    42.         _musicIsMuted = !_musicIsMuted;
    43.         audioMixer.SetFloat(AUDIO_MIXER_MUSIC, _musicIsMuted ? MUTED_VALUE : NOT_MUTED_VALUE);
    44.         UpdateMusicIcon();
    45.         Save();
    46.     }
    47.  
    48.     public void OnSFXButtonPressed()
    49.     {
    50.         _sfxIsMuted = !_sfxIsMuted;
    51.         audioMixer.SetFloat(AUDIO_MIXER_SFX, _sfxIsMuted ? MUTED_VALUE : NOT_MUTED_VALUE);
    52.  
    53.         UpdateSfxIcon();
    54.         Save();
    55.     }
    56.  
    57.     private void UpdateMusicIcon()
    58.     {
    59.         musicOnIcon.enabled = !_musicIsMuted;
    60.         musicOffIcon.enabled = _musicIsMuted;
    61.     }
    62.  
    63.     private void UpdateSfxIcon()
    64.     {
    65.         sfxOnIcon.enabled = !_sfxIsMuted;
    66.         sfxOffIcon.enabled = _sfxIsMuted;
    67.     }
    68.  
    69.     private void Save()
    70.     {
    71.         PlayerPrefs.SetInt(MUSIC_PREFS_KEY, _musicIsMuted ? MUTED : NOT_MUTED);
    72.         PlayerPrefs.SetInt(SFX_PREFS_KEY, _sfxIsMuted ? MUTED : NOT_MUTED);
    73.         PlayerPrefs.Save();
    74.     }
    75.  
    76.     private void Load()
    77.     {
    78.         _musicIsMuted = PlayerPrefs.GetInt(MUSIC_PREFS_KEY) == MUTED;
    79.         _sfxIsMuted = PlayerPrefs.GetInt(SFX_PREFS_KEY) == MUTED;
    80.     }
    81. }
     
    hoshyargames likes this.
  5. hoshyargames

    hoshyargames

    Joined:
    May 2, 2021
    Posts:
    24
    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?
     
  6. hoshyargames

    hoshyargames

    Joined:
    May 2, 2021
    Posts:
    24
    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.
     
  7. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,826
    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.
     
    hoshyargames likes this.
  8. hoshyargames

    hoshyargames

    Joined:
    May 2, 2021
    Posts:
    24
    images are working fine, however audio does not muted at the start. that is the only problem.
    hope you find some solution please.
     
  9. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,826
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using UnityEngine.Audio;
    4.  
    5. public class SoundManager : MonoBehaviour
    6. {
    7.     [SerializeField]
    8.     private AudioMixer audioMixer;
    9.     [SerializeField]
    10.     private Image musicOnIcon;
    11.     [SerializeField]
    12.     private Image musicOffIcon;
    13.     [SerializeField]
    14.     private Image sfxOnIcon;
    15.     [SerializeField]
    16.     private Image sfxOffIcon;
    17.  
    18.     private bool _musicIsMuted = false;
    19.     private bool _sfxIsMuted = false;
    20.  
    21.     private const string MUSIC_PREFS_KEY = "musicmuted";
    22.     private const string SFX_PREFS_KEY = "sfxmuted";
    23.     private const string AUDIO_MIXER_MUSIC = "music";
    24.     private const string AUDIO_MIXER_SFX = "sfx";
    25.     private const int MUTED = 1;
    26.     private const int NOT_MUTED = 0;
    27.     private const float MUTED_VALUE = -80f;
    28.     private const float NOT_MUTED_VALUE = 0f;
    29.  
    30.     private void Awake()
    31.     {
    32.         if (!PlayerPrefs.HasKey(MUSIC_PREFS_KEY)) PlayerPrefs.SetInt(MUSIC_PREFS_KEY, NOT_MUTED);
    33.         if (!PlayerPrefs.HasKey(SFX_PREFS_KEY)) PlayerPrefs.SetInt(SFX_PREFS_KEY, NOT_MUTED);
    34.         Load();
    35.  
    36.         UpdateMusic();
    37.         UpdateSfx();
    38.     }
    39.  
    40.     public void OnMusicButtonPressed()
    41.     {
    42.         _musicIsMuted = !_musicIsMuted;
    43.         UpdateMusic();
    44.         Save();
    45.     }
    46.  
    47.     public void OnSFXButtonPressed()
    48.     {
    49.         _sfxIsMuted = !_sfxIsMuted;
    50.         UpdateSfx();
    51.         Save();
    52.     }
    53.  
    54.     private void UpdateMusic()
    55.     {
    56.         musicOnIcon.enabled = !_musicIsMuted;
    57.         musicOffIcon.enabled = _musicIsMuted;
    58.         audioMixer.SetFloat(AUDIO_MIXER_MUSIC, _musicIsMuted ? MUTED_VALUE : NOT_MUTED_VALUE);
    59.     }
    60.  
    61.     private void UpdateSfx()
    62.     {
    63.         sfxOnIcon.enabled = !_sfxIsMuted;
    64.         sfxOffIcon.enabled = _sfxIsMuted;
    65.         audioMixer.SetFloat(AUDIO_MIXER_SFX, _sfxIsMuted ? MUTED_VALUE : NOT_MUTED_VALUE);
    66.     }
    67.  
    68.     private void Save()
    69.     {
    70.         PlayerPrefs.SetInt(MUSIC_PREFS_KEY, _musicIsMuted ? MUTED : NOT_MUTED);
    71.         PlayerPrefs.SetInt(SFX_PREFS_KEY, _sfxIsMuted ? MUTED : NOT_MUTED);
    72.         PlayerPrefs.Save();
    73.     }
    74.  
    75.     private void Load()
    76.     {
    77.         _musicIsMuted = PlayerPrefs.GetInt(MUSIC_PREFS_KEY) == MUTED;
    78.         _sfxIsMuted = PlayerPrefs.GetInt(SFX_PREFS_KEY) == MUTED;
    79.     }
    80. }
     
    hoshyargames likes this.
  10. hoshyargames

    hoshyargames

    Joined:
    May 2, 2021
    Posts:
    24

    still the same problem exist!
     
  11. hoshyargames

    hoshyargames

    Joined:
    May 2, 2021
    Posts:
    24
    please help.
     
  12. hoshyargames

    hoshyargames

    Joined:
    May 2, 2021
    Posts:
    24
    i hope still for some help. i am here since 2 days. please. why sounds play again when game starts?
     
  13. hoshyargames

    hoshyargames

    Joined:
    May 2, 2021
    Posts:
    24
    music and sound effects dont get saved when quitting the game, why?

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using UnityEngine.Audio;
    4.  
    5. public class AudioManager : MonoBehaviour
    6. {
    7.     private static AudioManager instance = null;
    8.  
    9.     public static AudioManager Instance
    10.     {
    11.         get { return instance; }
    12.     }
    13.  
    14.     [SerializeField]
    15.     private AudioMixer audioMixer;
    16.  
    17.     [SerializeField]
    18.     private Image musicOnIcon;
    19.  
    20.     [SerializeField]
    21.     private Image musicOffIcon;
    22.  
    23.     [SerializeField]
    24.     private Image sfxOnIcon;
    25.  
    26.     [SerializeField]
    27.     private Image sfxOffIcon;
    28.  
    29.     private bool _musicIsMuted = false;
    30.     private bool _sfxIsMuted = false;
    31.  
    32.     private const string MUSIC_PREFS_KEY = "musicmuted";
    33.     private const string SFX_PREFS_KEY = "sfxmuted";
    34.  
    35.     private const string AUDIO_MIXER_MUSIC = "music";
    36.     private const string AUDIO_MIXER_SFX = "sfx";
    37.  
    38.     private const int MUTED = 1;
    39.     private const int NOT_MUTED = 0;
    40.  
    41.     private const float MUTED_VALUE = -80f;
    42.     private const float NOT_MUTED_VALUE = 0f;
    43.  
    44.  
    45.     private void Awake()
    46.     {
    47.         if (instance != null && instance != this)
    48.         {
    49.             Destroy(this.gameObject);
    50.             return;
    51.         }
    52.         else
    53.         {
    54.             instance = this;
    55.         }
    56.  
    57.         DontDestroyOnLoad(this.gameObject);
    58.  
    59.  
    60.  
    61.         if (!PlayerPrefs.HasKey(MUSIC_PREFS_KEY))
    62.         {
    63.             PlayerPrefs.SetInt(MUSIC_PREFS_KEY, NOT_MUTED);
    64.         }
    65.        
    66.         if (!PlayerPrefs.HasKey(SFX_PREFS_KEY))
    67.         {
    68.             PlayerPrefs.SetInt(SFX_PREFS_KEY, NOT_MUTED);
    69.         }
    70.            
    71.  
    72.         Load();
    73.  
    74.         UpdateMusic();
    75.         UpdateSfx();
    76.     }
    77.  
    78.     public void OnMusicButtonPressed()
    79.     {
    80.         _musicIsMuted = !_musicIsMuted;
    81.         UpdateMusic();
    82.         Save();
    83.     }
    84.  
    85.     public void OnSFXButtonPressed()
    86.     {
    87.         _sfxIsMuted = !_sfxIsMuted;
    88.         UpdateSfx();
    89.         Save();
    90.     }
    91.  
    92.     private void UpdateMusic()
    93.     {
    94.         musicOnIcon.enabled = !_musicIsMuted;
    95.         musicOffIcon.enabled = _musicIsMuted;
    96.         audioMixer.SetFloat(AUDIO_MIXER_MUSIC, _musicIsMuted ? MUTED_VALUE : NOT_MUTED_VALUE);
    97.     }
    98.  
    99.     private void UpdateSfx()
    100.     {
    101.         sfxOnIcon.enabled = !_sfxIsMuted;
    102.         sfxOffIcon.enabled = _sfxIsMuted;
    103.         audioMixer.SetFloat(AUDIO_MIXER_SFX, _sfxIsMuted ? MUTED_VALUE : NOT_MUTED_VALUE);
    104.     }
    105.  
    106.     private void Save()
    107.     {
    108.         PlayerPrefs.SetInt(MUSIC_PREFS_KEY, _musicIsMuted ? MUTED : NOT_MUTED);
    109.         PlayerPrefs.SetInt(SFX_PREFS_KEY, _sfxIsMuted ? MUTED : NOT_MUTED);
    110.         PlayerPrefs.Save();
    111.     }
    112.  
    113.     private void Load()
    114.     {
    115.         _musicIsMuted = PlayerPrefs.GetInt(MUSIC_PREFS_KEY) == MUTED;
    116.         _sfxIsMuted = PlayerPrefs.GetInt(SFX_PREFS_KEY) == MUTED;
    117.     }
    118. }
    119.