0
\$\begingroup\$
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Audio;
using UnityEngine.UI;
using TMPro;
using System;
using UnityEngine.Events;
using System.Linq;

public class Settings : MonoBehaviour
{
    [SerializeField] private AudioSource[] audioSources;
    public AudioMixer audioMixer;
    public TMP_Dropdown resolutionDropdown;
    public TMP_Dropdown qualityDropdown;
    public Text musicText;
    public Text sfxText;
    public Slider[] audioSliders;
    public Toggle fullScreenToggle;

    private Resolution[] resolutions;

    private void Awake()
    {
        audioSources = GetComponents<AudioSource>();

        resolutionDropdown.onValueChanged.AddListener(new UnityAction<int>(index =>
        {
            PlayerPrefs.SetInt("resolutionvalue", resolutionDropdown.value);
            PlayerPrefs.Save();

        }));

        qualityDropdown.onValueChanged.AddListener(new UnityAction<int>(index =>
        {
            PlayerPrefs.SetInt("qualityvalue", qualityDropdown.value);
            PlayerPrefs.Save();

        }));

        fullScreenToggle.onValueChanged.AddListener(new UnityAction<bool>(index =>
        {
            PlayerPrefs.SetInt("fullscreen", boolToInt(fullScreenToggle.isOn));
            PlayerPrefs.Save();

        }));
    }

    private void Start()
    {
        qualityDropdown.value = PlayerPrefs.GetInt("qualityvalue");

        var resolutions = Screen.resolutions.Where(resolution => resolution.refreshRate == 60).ToArray();
        resolutionDropdown.ClearOptions();

        List<string> options = new List<string>();

        int currentResolutionIndex = 0;
        for(int i = 0; i < resolutions.Length; i++)
        {
            string option = resolutions[i].width + " x " + resolutions[i].height;
            options.Add(option);

            if(resolutions[i].width == Screen.currentResolution.width &&
                resolutions[i].height == Screen.currentResolution.height)
            {
                currentResolutionIndex = i;
            }
        }

        resolutionDropdown.AddOptions(options);
        resolutionDropdown.value = PlayerPrefs.GetInt("resolutionvalue", currentResolutionIndex);
        resolutionDropdown.RefreshShownValue();

        float musicvolume = PlayerPrefs.GetFloat("mainmenumusicvolume");
        float sfxvolume = PlayerPrefs.GetFloat("mainmenusfxvolume");

        musicText.text = musicvolume.ToString();
        sfxText.text = sfxvolume.ToString();
        audioSliders[0].value = musicvolume / 100f;
        audioSliders[1].value = sfxvolume / 100f;

        fullScreenToggle.isOn = intToBool(PlayerPrefs.GetInt("fullscreen", 0));
        
    }

    public void SetResolution(int resolutionIndex)
    {
        if (resolutions != null)
        {
            Resolution resolution = resolutions[resolutionIndex];
            Screen.SetResolution(resolution.width, resolution.height, Screen.fullScreen);
        }
    }

    public void SetMusicVolume(float volume)
    {
        audioMixer.SetFloat("mainmenumusicvolume", Mathf.Log10(volume) * 20);
        musicText.text = Math.Round(volume * 100, MidpointRounding.AwayFromZero).ToString();

        PlayerPrefs.SetFloat("mainmenumusicvolume", (float)Math.Round(volume * 100, MidpointRounding.AwayFromZero));

        //audioMixer.SetFloat("gamemusicvolume", Mathf.Log10(volume) * 20);
        //PlayerPrefs.SetFloat("gamemusicvolume", (float)Math.Round(volume * 100, MidpointRounding.AwayFromZero));
    }

    public void SetSfxVolume(float volume)
    {
        audioMixer.SetFloat("mainmenusfxvolume", Mathf.Log10(volume) * 20);
        sfxText.text = Math.Round(volume * 100, MidpointRounding.AwayFromZero).ToString();

        PlayerPrefs.SetFloat("mainmenusfxvolume", (float)Math.Round(volume * 100, MidpointRounding.AwayFromZero));

        if (!audioSources[1].isPlaying)
            audioSources[1].Play();
    }

    public void SetQuality(int qualityIndex)
    {
        QualitySettings.SetQualityLevel(qualityIndex);
    }

    public void SetFullscreen(bool isFullscreen)
    {
        Screen.fullScreen = isFullscreen;        
    }

    int boolToInt(bool val)
    {
        if (val)
            return 1;
        else
            return 0;
    }

    bool intToBool(int val)
    {
        if (val != 0)
            return true;
        else
            return false;
    }
}

I'm calling this method from ui slider from On Value Changed (single) event in the editor :

public void SetSfxVolume(float volume)
    {
        audioMixer.SetFloat("mainmenusfxvolume", Mathf.Log10(volume) * 20);
        sfxText.text = Math.Round(volume * 100, MidpointRounding.AwayFromZero).ToString();

        PlayerPrefs.SetFloat("mainmenusfxvolume", (float)Math.Round(volume * 100, MidpointRounding.AwayFromZero));

        if (!audioSources[1].isPlaying)
            audioSources[1].Play();
    }

Screenshot :

sfx volume

The problem is that when I'm running the game first time and it's starting with the main menu it's playing once the sfx effect audio because this two lines :

if (!audioSources[1].isPlaying)
    audioSources[1].Play();

The audio source play on awake in the editor is disabled unchecked. but in the script it's playing it once when the game is running.

What can I do and where to make that it will not play the sfx audio when running the game only when changing the sfx slider volume value ?

\$\endgroup\$
0
\$\begingroup\$

In your start function, you change the value of the sliders:

audioSliders[0].value = musicvolume / 100f;
audioSliders[1].value = sfxvolume / 100f;

Because the value was changed, the sliders fire the OnValueChanged event and call the function you told that event to call.

If you don't want to fire the event, you should use SetValueWithoutNotify.

\$\endgroup\$
1
  • \$\begingroup\$ In the end I did something else much simple. I add a flag in the top : private bool hasChanged = false; then in the Start after changing the sliders value I change the flag to true and then in the function I added to the IF if (!audioSources[1].isPlaying && hasChanged) that's all. The problem is that it's first making the code in the function then the code in the Start I also checked and it's doing the code in the function before the Awake. Using this flag did the trick. \$\endgroup\$ – Daniel Jacksonville Feb 2 at 23:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.