Skip to content

Instantly share code, notes, and snippets.

@lasagnaphil
Created June 9, 2016 10:31
Show Gist options
  • Save lasagnaphil/4a14fa68d62c27faa2d7e121a745416a to your computer and use it in GitHub Desktop.
Save lasagnaphil/4a14fa68d62c27faa2d7e121a745416a to your computer and use it in GitHub Desktop.
SoundManager class from Unity (basic sound management)
using UnityEngine;
using System.Collections.Generic;
[System.Serializable]
public class SoundData
{
public SoundManager.Sounds soundEnum;
public AudioClip audioClip;
[Range(0.0f, 1.0f)]
public float volume = 1.0f;
}
public class AudioSourceData
{
public SoundManager.Sounds soundEnum;
public AudioSource source;
public AudioSourceData(SoundManager.Sounds soundEnum, AudioSource source)
{
this.soundEnum = soundEnum;
this.source = source;
}
}
public class SoundManager : Singleton<SoundManager>
{
public enum Sounds
{
Main,
Search,
DoorBell,
DoorClose,
DoorOpen,
Eating,
Flush,
FrontDoorLock,
FrontDoorUnlock,
MouseClick,
PageFlip,
Rolling,
Shower,
Snoring,
TextPrint,
PlayerWalking,
HouseownerWalking,
WalkingUpstairs,
Writing,
TV,
secretdoor
}
public List<AudioSourceData> audioSources = new List<AudioSourceData>();
public List<SoundData> soundDataList;
public void Play(Sounds soundEnum, bool loop = false)
{
SoundData soundData = soundDataList.Find(x => x.soundEnum == soundEnum);
var foundSource = audioSources.Find(x => !x.source.isPlaying);
if (foundSource != null)
{
foundSource.source.loop = loop;
foundSource.source.clip = soundData.audioClip;
foundSource.source.volume = soundData.volume;
foundSource.source.Play();
}
else
{
audioSources.Add(new AudioSourceData(soundEnum, gameObject.AddComponent<AudioSource>()));
audioSources[audioSources.Count - 1].source.loop = loop;
audioSources[audioSources.Count - 1].source.clip = soundData.audioClip;
audioSources[audioSources.Count - 1].source.volume = soundData.volume;
audioSources[audioSources.Count - 1].source.Play();
}
}
void Update()
{
var sourcesToDelete = audioSources.FindAll(x => !x.source.isPlaying);
sourcesToDelete.ForEach(x => Destroy(x.source));
sourcesToDelete.ForEach(x => audioSources.Remove(x));
}
public void Stop(Sounds soundEnum)
{
audioSources
.FindAll(x => x.soundEnum == soundEnum)
.ForEach(x => x.source.Stop());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment