From 3d3813d164cca88294e348f7b13f65b408011cda Mon Sep 17 00:00:00 2001
From: miepzerino <o.skotnik@gmail.com>
Date: Sun, 17 Dec 2023 20:36:01 +0000
Subject: [PATCH] Added volume settings in settings menu

---
 Assets/Scripts/SoundManager.cs |   65 +++++++++++++++++++-------------
 1 files changed, 38 insertions(+), 27 deletions(-)

diff --git a/Assets/Scripts/SoundManager.cs b/Assets/Scripts/SoundManager.cs
index 84a6a6a..a3b8107 100644
--- a/Assets/Scripts/SoundManager.cs
+++ b/Assets/Scripts/SoundManager.cs
@@ -10,7 +10,7 @@
 [System.Serializable]
 public class Sound
 {
-    public string name;
+    public SoundName name;
     public AudioClip clip;
     public AudioMixerGroupEnum mixerGroup;
 
@@ -88,11 +88,18 @@
             sounds[i].SetSource(_go.AddComponent<AudioSource>(), audioMixer);
         }
         DontDestroyOnLoad(this);
-        PlaySound("MusicHappy");
+        PlaySound(SoundName.MusicHappy);
+    }
+    private void Start()
+    {
+        LoadAudioSettings();
     }
 
-    public void PlaySound(string _name)
+    public void PlaySound(SoundName _name)
     {
+        if (_name == SoundName.None)
+            return;
+
         for (int i = 0; i < sounds.Length; i++)
         {
             if (sounds[i].name == _name)
@@ -103,11 +110,14 @@
         }
 
         //no Sound with name
-        Debug.LogWarning("SoundManager: Sounds not found in list: " + _name);
+        Debug.LogWarning("AudioManager: Sounds not found in list, add sound to audiomanager: " + _name);
     }
 
-    public void PlaySoundAtPoint(GameObject gObject, string _name)
+    public void PlaySoundAtPoint(GameObject gObject, SoundName _name)
     {
+        if (_name == SoundName.None)
+            return;
+
         gObject.AddComponent<AudioSource>();
         for (int i = 0; i < sounds.Length; i++)
         {
@@ -123,13 +133,11 @@
         }
 
         //no Sound with name
-        Debug.LogWarning("SoundManager: Sounds not found in list: " + _name);
+        Debug.LogWarning("AudioManager: Sounds not found in list, add sound to audiomanager: " + _name);
     }
 
     private static AudioSource PlayClipAtPointCustom(AudioSource audioSource, Vector3 pos, Sound sound)
     {
-        audioSource.volume = sound.volume * (1 + Random.Range(-sound.randomVolume / 2f, sound.randomVolume / 2f));
-        audioSource.pitch = sound.pitch * (1 + Random.Range(-sound.randomPitch / 2f, sound.randomPitch / 2f));
         GameObject tempGO = new GameObject("TempAudio"); // create the temp object
         tempGO.transform.position = pos; // set its position
         AudioSource tempASource = tempGO.AddComponent<AudioSource>(); // add an audio source
@@ -142,8 +150,8 @@
         tempASource.playOnAwake = audioSource.playOnAwake;
         tempASource.loop = audioSource.loop;
         tempASource.priority = audioSource.priority;
-        tempASource.volume = audioSource.volume;
-        tempASource.pitch = audioSource.pitch;
+        tempASource.volume = audioSource.volume * (1 + Random.Range(-sound.randomVolume / 2f, sound.randomVolume / 2f));
+        tempASource.pitch = audioSource.pitch * (1 + Random.Range(-sound.randomPitch / 2f, sound.randomPitch / 2f));
         tempASource.panStereo = audioSource.panStereo;
         tempASource.spatialBlend = audioSource.spatialBlend;
         tempASource.reverbZoneMix = audioSource.reverbZoneMix;
@@ -175,7 +183,7 @@
         audioMixer.SetFloat("sfxVol", 20f * Mathf.Log10(_volume));
     }
 
-    public void ChangeMusic(string _name)
+    public void ChangeMusic(SoundName _name)
     {
         for (int i = 0; i < sounds.Length; i++)
         {
@@ -186,7 +194,6 @@
                 return;
             }
         }
-        Debug.Log("No music was playing!");
         PlaySound(_name);
         return;
     }
@@ -196,46 +203,50 @@
         PlayerPrefs.SetFloat("pVolMaster", volMaster);
         PlayerPrefs.SetFloat("pVolMusic", volMusic);
         PlayerPrefs.SetFloat("pVolSFX", volSFX);
+    }
 
-        Debug.Log("Saved Options");
+    public void DiscardAudioSettings()
+    {
+        volMaster = PlayerPrefs.GetFloat("pVolMaster");
+        volMusic = PlayerPrefs.GetFloat("pVolMusic");
+        volSFX = PlayerPrefs.GetFloat("pVolSFX");
+        ChangeMasterVolume(volMaster);
+        ChangeSfxVolume(volSFX);
+        ChangeMusicVolume(volMusic);
     }
 
     public void LoadAudioSettings()
     {
-        GameObject tempGO = new GameObject("TempSlider");
-
         if (PlayerPrefs.HasKey("pVolMaster") == false)
         {
-            PlayerPrefs.SetFloat("pVolMaster", 0.5f);
+            PlayerPrefs.SetFloat("pVolMaster", 1f);
         }
 
         if (PlayerPrefs.HasKey("pVolMusic") == false)
         {
-            PlayerPrefs.SetFloat("pVolMusic", 0.5f);
+            PlayerPrefs.SetFloat("pVolMusic", 1f);
         }
 
         if (PlayerPrefs.HasKey("pVolSFX") == false)
         {
-            PlayerPrefs.SetFloat("pVolSFX", 0.5f);
+            PlayerPrefs.SetFloat("pVolSFX", 1f);
         }
 
         volMaster = PlayerPrefs.GetFloat("pVolMaster");
         volMusic = PlayerPrefs.GetFloat("pVolMusic");
         volSFX = PlayerPrefs.GetFloat("pVolSFX");
 
-        if (GameObject.Find("SliderVolume") == false)
+        if (GameObject.Find("SliderVolume") == true && GameObject.Find("SliderSFX") == true && GameObject.Find("SliderMusic") == true)
         {
-            ChangeMasterVolume(volMaster);
-            ChangeSfxVolume(volSFX);
-            ChangeMusicVolume(volMusic);
-            return;
+            GameObject.Find("SliderVolume").GetComponent<Slider>().value = volMaster;
+            //ChangeMasterVolume(volMaster);
+            GameObject.Find("SliderSFX").GetComponent<Slider>().value = volSFX;
+            //ChangeSfxVolume(volSFX);
+            GameObject.Find("SliderMusic").GetComponent<Slider>().value = volMusic;
+            //ChangeMusicVolume(volMusic);
         }
-
-        GameObject.Find("SliderVolume").GetComponent<Slider>().value = volMaster;
         ChangeMasterVolume(volMaster);
-        GameObject.Find("SliderSFX").GetComponent<Slider>().value = volSFX;
         ChangeSfxVolume(volSFX);
-        GameObject.Find("SliderMusic").GetComponent<Slider>().value = volMusic;
         ChangeMusicVolume(volMusic);
 
     }

--
Gitblit v1.9.3