Permalink
Browse files

added sound manager

  • Loading branch information...
1 parent d5c5aa2 commit c1663782a3504e580ef3429bd9eaf0292e34e913 @prime31 committed Aug 21, 2010
Showing with 190 additions and 0 deletions.
  1. +77 −0 Scripts/Audio/SoundManager/So.cs
  2. +66 −0 Scripts/Audio/SoundManager/Sound.cs
  3. +47 −0 Scripts/Audio/SoundManager/SoundTester.cs
@@ -0,0 +1,77 @@
+using UnityEngine;
+using System.Collections;
+using System.Collections.Generic;
+
+
+public class So : MonoBehaviour
+{
+ public static So und;
+ public int initialCapacity = 5;
+ private List<Sound> _soundList;
+ private Sound _bgSound;
+
+ void Awake()
+ {
+ // Create the _soundList to speed up sound playing in game
+ _soundList = new List<Sound>( initialCapacity );
+
+ for( int i = 0; i < initialCapacity; i++ )
+ _soundList.Add( new Sound( this ) );
+
+ und = this;
+ }
+
+
+ public void playBGMusic( AudioClip audioClip, bool loop )
+ {
+ if( _bgSound == null )
+ _bgSound = new Sound( this );
+
+ _bgSound.loop = loop;
+ StartCoroutine( _bgSound.playAudioClip( audioClip, AudioRolloffMode.Linear, 0.5f, Vector3.zero ) );
+ }
+
+
+ public void playSound( AudioClip audioClip )
+ {
+ playSound( audioClip, AudioRolloffMode.Linear, 0.5f, Vector3.zero );
+ }
+
+
+ public void playSound( AudioClip audioClip, AudioRolloffMode rolloff, float volume, Vector3 position )
+ {
+ bool multiPlay = false;
+ if( !multiPlay )
+ {
+ if( GameObject.Find( "/" + audioClip.name ) || GameObject.Find( "/" + audioClip.name + "(clone)" ) )
+ return;
+ }
+
+ // Find the first Sound not being used. If they are all in use, create a new one
+ Sound _sound;
+ _sound = _soundList.Find( findFirstUnusedSound );
+
+ if( _sound == null )
+ {
+ _sound = new Sound( this );
+ _sound.destroyAfterPlay = true;
+ _soundList.Add( _sound );
+ }
+
+ StartCoroutine( _sound.playAudioClip( audioClip, rolloff, volume, position ) );
+ }
+
+
+ public void removeSound( Sound s )
+ {
+ _soundList.Remove( s );
+ }
+
+
+ // Predicate to find the first available Sound in the list
+ private static bool findFirstUnusedSound( Sound s )
+ {
+ return s.available;
+ }
+
+}
@@ -0,0 +1,66 @@
+using UnityEngine;
+using System.Collections;
+
+
+public class Sound
+{
+ private So _manager;
+ private AudioSource _audioSource;
+ private GameObject _gameObject;
+
+ public bool available = true;
+ public bool destroyAfterPlay = false;
+
+ public bool loop
+ {
+ set
+ {
+ _audioSource.loop = value;
+ }
+ }
+
+
+ public Sound( So manager )
+ {
+ _manager = manager;
+
+ // Create a GameObject to hold the audioSource for playing sounds
+ _gameObject = new GameObject();
+ _gameObject.transform.parent = manager.transform;
+
+ _audioSource = _gameObject.AddComponent( "AudioSource" ) as AudioSource;
+ }
+
+
+ public void destroy()
+ {
+ _manager.removeSound( this );
+
+ MonoBehaviour.Destroy( _gameObject );
+ }
+
+
+ public IEnumerator playAudioClip( AudioClip audioClip, AudioRolloffMode rolloff, float volume, Vector3 position )
+ {
+ available = false;
+
+ // Setup the GameObject and AudioSource and start playing
+ _gameObject.name = audioClip.name;
+ _gameObject.transform.position = position;
+
+ _audioSource.clip = audioClip;
+ _audioSource.rolloffMode = rolloff;
+ //_audioSource.pitch = Random.Range( 0.9f, 1.1f );
+ _audioSource.audio.Play();
+
+ // Wait for the clip to finish
+ yield return new WaitForSeconds( _audioSource.clip.length + 0.1f );
+
+ // Should we destory ourself after playing?
+ if( destroyAfterPlay )
+ this.destroy();
+
+ available = true;
+ }
+
+}
@@ -0,0 +1,47 @@
+using UnityEngine;
+using System.Collections;
+
+public class SoundTester : MonoBehaviour
+{
+ public AudioClip explosionSound;
+ public AudioClip rocketSound;
+ public AudioClip bgSound;
+
+
+ // Use this for initialization
+ void Start()
+ {
+ So.und.playSound( explosionSound );
+ So.und.playBGMusic( bgSound, true );
+ }
+
+
+ // Update is called once per frame
+ void Update()
+ {
+ bool hasTouchBegan = false;
+ for( int i = 0; i < Input.touchCount; i++ )
+ {
+ if( Input.GetTouch( i ).phase == TouchPhase.Began )
+ {
+ Debug.Log( "playing sound" );
+ hasTouchBegan = true;
+ break;
+ }
+ }
+
+ if( hasTouchBegan || Input.GetKeyUp( KeyCode.A ) )
+ {
+ if( Random.Range( 0, 100 ) % 2 == 0 )
+ {
+ So.und.playSound( explosionSound );
+ }
+ else
+ {
+ So.und.playSound( rocketSound );
+ }
+ }
+ }
+
+
+}

0 comments on commit c166378

Please sign in to comment.