slimecing

a fighting game featuring slimes and swords
Log | Files | Refs | README

AudioManager.cs (2101B)


      1 using System;
      2 using UnityEngine;
      3 
      4 public class AudioManager : MonoBehaviour
      5 {
      6     //Sounds
      7     public Sound[] sounds;
      8 
      9     //Music
     10     public Sound[] musics;
     11     int num_music = 0;
     12     public Sound music;
     13 
     14 
     15     public static AudioManager instance;
     16 
     17     void Awake()
     18     {
     19 
     20         if (instance == null)
     21             instance = this;
     22         else
     23         {
     24             Destroy(gameObject);
     25             return;
     26         }
     27         DontDestroyOnLoad(gameObject);
     28 
     29 
     30         Screen.SetResolution(1280, 600, false);
     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         }
     41 
     42     }
     43 
     44     //Return the Sound object with the name given. Used by other objets to access the sounds they want to play.
     45     public Sound Find(string name)
     46     {
     47         Sound s = Array.Find(sounds, sound => sound.name == name);
     48         if (s == null)
     49         {
     50             Debug.Log("Sound:" + name + " not found!");
     51         }
     52         return s;
     53     }
     54 
     55     //Mute all SFX sounds
     56     public void MuteAllSounds()
     57     {
     58         foreach (Sound s in sounds)
     59             s.source.mute = true;
     60     }
     61 
     62     public void PauseAll()
     63     {
     64         foreach (Sound s in sounds)
     65             s.source.Pause();
     66         music.source.Pause();
     67     }
     68     //Resume all sounds.
     69     public void ResumeAll()
     70     {
     71         foreach (Sound s in sounds)
     72             s.source.UnPause();
     73         music.source.UnPause();
     74     }
     75 
     76     //Change the music for the next one in the array musics[]
     77     public void nextMusic()
     78     {
     79         if (musics.Length - 1 > num_music)
     80             num_music++;
     81         else
     82             num_music = 0;
     83 
     84         music.source.Stop(); 
     85         
     86         music.source.clip = musics[num_music].clip;
     87         music.source.volume = musics[num_music].volume;
     88         music.source.pitch = musics[num_music].pitch;
     89         music.source.Play();
     90     }
     91 
     92     public static AudioManager getInstance()
     93     {
     94         return instance;
     95     }
     96 }