In my game, I create an empty prefab that plays a sound and then destroys its self for every sound effect in my game, example; bullets, hitting the wall, ect…
This seems to make my game run pretty slow. Does anyone have any better techniques for playing sound?
If you are instantiating an empty object to play a sound for each bullet that will slow things down.
Better to attach an AudioSource component to your bullet prefab and set Play On Awake in the inspector - or add
audio.PlayOneShot(yoursoundname)
in the script that controls your bullet behaviour.
Basically, your sound should be a component of the object that emits it.
ok. That sounds good (lol, sounds…pun). Thanks, I’ll give it a try and let you know how it works.
I’ve found the AudioSource.PlayClipAtPoint() class method to be quite useful. Here’s how to use it:
AudioSource.PlayClipAtPoint (theSound, transform.position);
PlayClipAtPoint is still creating and destroying objects; it’s just that Unity is doing it for you instead of you doing it manually.
–Eric
And thus its allocating memory on the fly - which is very expensive/slow
I have written a small Sound Manager that is just simply a game Object with a singleton class that manages all the Sound Effects and Background music.
Sure it does not support stereo or 3D sound but it is suitable for the small games and cost not much performance
How do you do it?
I think audio.play() is less expensive since no object creation / destruction is involved.