- Home /
The simple way to put sounds in a game?
Hello,
What is the simple way to play sounds in game? Without any extras. We also need that the same sound would play only a single instance. For example, if there are 10 bombs exploded simultaneously we don't want there to be 10 explosion sounds played at once. Only single. So if the sound is the same then it should interrupt all previous sounds of the same type.
My coder don't know English much and he found some sound mixer and put into the game. I afraid it will eat extra CPU. We need just simply sounds playing without compression, limiter, multiple channels and stuff like that.
Thanks
You're asking two questions:
1- How do I use sounds in the game
2- How do I implement some sort of load balancing for sound effects
Implementing sound effects is a straightforward exercise that I doubt will take you or your engineer any time, so we can skip that. It helps to read the documentation, though.
Load balancing sounds is a little more interesting and there are a few ways to go about it.
While you want multiple sounds to only play once (which smells a little like premature optimization), your players might not expect it. If you see 10 bombs about to explode on screen, then you sort of expect that a louder sound will happen versus just 1 bomb.
Our game can be compared to something like match 3 games(single screen casual tile based game). So it is better to have a single bomb sound even if there are 10 of them exploding at the same time.
The problem is that I am not into coding so I do not know what to look. And coder is bad with English. That's why I asked if there is some light on CPU option.
Answer by ImpOfThePerverse · May 18, 2018 at 03:50 PM
I'd write a singleton that lets your bombs get ahold of a single collection of audio sources, all set to 2D sound. You'd have one gameobject with a number of child audiosources, one for each sound effect. Leave the audio source spatial blend set to 2D since the audiosource location won't match up with the bombs, etc.
One really good reason to output to a mixer is that it lets you have a volume slider in your options menu that controls all audio sources at once.
public class AudioManager : Monobehavior
{
public AudioSource sfxBombExplosion;
public AudioSource sfxGunshot;
// etc., one for each sound in your game
private static AudioManager Instance { get; set; }
void Awake()
{
if (Instance != null && Instance != this)
{
GameObject.Destroy(Instance.gameObject);
}
Instance = this;
}
public static void PlayBombExplosion() { Instance.sfxBombExplosion.Play(); }
public static void PlayGunshot() { Instance.sfxGunshot.Play(); }
// etc., one for each sound effect
}
To play a sound effect:
AudioManager.PlayBombExplosion();
I just worry about CPU that the mixer might use. The game should be crossplatform. It would be on mobile too. So it could be nice to use CPU optimal option. If mixer makes extra usage, that would not be good for mobiles. But if there is no difference or very little, then I would use it.
It should be possible to remove the mixer/disable sound in some way and allow the game to still run with extremely $$anonymous$$or changes, what do you see when you profile it running vs disabled, i would be very interested to know the result.
Your answer
Follow this Question
Related Questions
Sound gets loud in while playing game? 1 Answer
How to activate a sound when a player walks through a collider 1 Answer
3d Sound not working? 0 Answers
how to add a sound when collecting coins? 0 Answers
Can i change tha audio driver? 0 Answers