This site uses cookies! Learn More

This site uses cookies!

armedunity.com uses cookies to improve user experience.

By continuing to use this site, you agree to allow us to store cookies on your computer.

Sign in to follow this  
Followers 0
OneManArmy

Simple AudioManager code

4 posts in this topic

You have the audio components: AudioSource (attached to your sound emitter) and AudioListener (attached to the main camera). Now, the problem is that audio clips are represented by the AudioClip class, and each AudioSource can have a single AudioClip playing at the moment. That is sometimes ok, but other times, you want your game object to be able to play a number of different sounds, maybe at the same time (Think of a character playing footsteps sounds and maybe saying something at the same time).

Since it is very natural to have one manager object in your scene acting as a singleton, a component that can be attached to such manager. The work of this component is to play almost any sound you want to play in your game. The code is very simple and self explationary.

Please login or register to see this code.

As you can see, this class is basically 6 overloads for the Play function. Basically we have two types: the type that takes a transform (emitter) as parameter and the type that takes a Vector3. The only difference is that if you provide a point (Vector3), the sound will be played there, but if you provide an emitter, the sound will be played where the emitter is, but will also follow the emitter as it moves (e.g. the engine of a passing car).

Share this post


Link to post
Share on other sites
Please login or register to see this quote.

 

this will still do sound hiccups on large sounds amount though

Share this post


Link to post
Share on other sites

This is an old post, but I read it just today. It seems to me that there is a minor error which could be easily fixed. You cannot destroy the GO considering length value because it was modified by the pitch value.

If the pitch of the sound is setting 0.5f, the real length is multiplyed * 2. Using simply the length value, you destroy the GO before the end of the sound. This function use a different way - take a look at the last line :

Please login or register to see this code.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0