Guest User

audio manager

a guest
Oct 16th, 2019
189
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using UnityEngine.Audio;
  2. using System;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5.  
  6. public class scr_AudioManager : MonoBehaviour
  7. {
  8.     [Tooltip("Manages sound/volume")]
  9.     public AudioMixer gameMixer;
  10.  
  11.     public Sound[] sounds;
  12.  
  13.     public static scr_AudioManager instance;
  14.     public Slider musicSlider;
  15.  
  16.     // Start is called before the first frame update
  17.     void Awake()
  18.     {
  19.  
  20.         if (instance == null)
  21.         {
  22.             instance = this;
  23.         }
  24.         else
  25.         {
  26.             Destroy(gameObject);
  27.             return;
  28.         }
  29.  
  30.         DontDestroyOnLoad(gameObject);
  31.  
  32.         foreach (Sound s in sounds)
  33.         {
  34.             s.source = gameObject.AddComponent<AudioSource>();
  35.             s.source.clip = s.clip;
  36.  
  37.             s.source.volume = s.volume;
  38.             s.source.pitch = s.pitch;
  39.             s.source.loop = s.loop;
  40.             s.source.outputAudioMixerGroup = s.group;
  41.         }
  42.     }
  43.  
  44.     void Start()
  45.     {
  46.  
  47.         musicSlider.value = PlayerPrefs.GetFloat("musicVol", 1.0f);
  48.        
  49.  
  50.         PlaySound("MainTheme_01");
  51.     }
  52.  
  53.     public void PlaySound(string name)
  54.     {
  55.         Sound s = Array.Find(sounds, sound => sound.name == name);
  56.         if (s == null)
  57.         {
  58.             Debug.LogWarning("Sound: " + name + " not found!");
  59.             return;
  60.         }
  61.  
  62.         s.source.Play();
  63.     }
  64.  
  65.     public void SetMusicVol (float musicVol)
  66.     {
  67.         gameMixer.SetFloat("musicVolume", Mathf.Log10(musicVol) * 20);
  68.         PlayerPrefs.SetFloat("musicVol", musicVol);
  69.         Debug.Log(musicVol);
  70.     }
  71. }
RAW Paste Data