Тёмный

Optimize Game Sounds: Pooling Audio Sources in Unity 

git-amend
Подписаться 16 тыс.
Просмотров 6 тыс.
50% 1

Опубликовано:

 

15 сен 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 111   
@git-amend
@git-amend 3 месяца назад
Happy Sunday! There's some extra code and extension methods in the repository that I didn't have time to include in the video, please check it out! And please add and comments and questions related to a follow up for Playlists and Music! 👍
@chigo9679
@chigo9679 3 месяца назад
Hi dear, was looking for channel that give tips and advanced techniques for Unity cuz must of them are for amateurs (with full respect) and do not respect clean code/architecture. But am having trouble while my projects grow over time and become more complex to manage and coupling problems. Those are topics I hope one day you will talk about them and show us the best practices: - Dialogue system. more as binary tree approach if we want player's choices driven dialogue. and how to set choices once the player load the game ? - UI. Saw you talked about Inventory, but in this case more about Setting UI for audio, video, language, ... and how to merge it with other UI like inventory and showing player states as an example. - Quest system. how to keep tracking of the player progress when scene changes ? and how to unlock events (spawn certain npcs/enemies/objects) associated to the specific quest once the current is completed (Side and main quest). - Tracking system. how npc can catch object that the player have to make specific actions/dialogue. - CutScene system. more focused in 2D games, how to implement robust cut scene old Pokemon games like (player following the npc to a position for example) and how to include dialogue into it (the cut scene)? Best regards and thank you for your effort.
@git-amend
@git-amend 3 месяца назад
@@chigo9679 Haha, well that’s a lot of requests. Too bad I can only make one video per week. In time, we’ll probably cover all those things you’ve requested. There is also a channel on the Discord server for requests if you think of anything else.
@chigo9679
@chigo9679 3 месяца назад
@@git-amend no rush at all. I subscribed and notifications is on if any video is uploaded cuz your channel is to valuable for an indie game dev. Keep Hoping you gona make those requests. Big thanks.
@skylarmuffin8145
@skylarmuffin8145 3 месяца назад
Ive been learning unity for about a year now and its so cool to finally actually understand your videos XD
@git-amend
@git-amend 3 месяца назад
Awesome!
@lakemonstergames32
@lakemonstergames32 3 месяца назад
This, along with your other tutorials, are goldmines for learning more advanced topics, thank you for filling in this much needed space!
@git-amend
@git-amend 3 месяца назад
Great to hear! Glad you like them!
@antijulius
@antijulius 3 месяца назад
Been putting off refactoring my audio into something more sophisticated for ages so I'm really excited for this. Thanks for all the great content!
@git-amend
@git-amend 3 месяца назад
Awesome, I hope this gives you some ideas. I put some code for a MusicManager in the repository and some extension methods too.
@richardrothkugel8131
@richardrothkugel8131 3 месяца назад
An additional optimisation for large projects: Create a ScriptableObject called SoundLibraryObject which holds references to all sounds in the game (private field with a public accessor). Each of these sounds are AudioSoundData. When applying a sound to an object, don't use SoundData, but instead use SoundLibraryObject and call the the SoundData from the library directly. What this means is that if you want to change a sound down the line, you don't need to hunt down the object it's attached to, instead just drag the sound into the sound library object, which will update everything automatically throughout your project. Excellent video. Learned a lot and those verbosity simplifications were very nice. I'm glad you showed the verbose version, then shortened it. I did have one question though, how do I change the parent object to which the sounds are spawned to. Currently it spawns to the SoundManager, but I have a child container in sound manager I want to spawn the sounds to and doing Instantiate(soundEmitterPrefab, container) doesn't seem to work. How would I set a custom container for the pool?
@git-amend
@git-amend 3 месяца назад
That's a great tip! Thanks for sharing! In regard to your question about setting a Parent, I would add an additional method to the Builder for that which defaults to the SoundManager, but can optionally be set.
@richardrothkugel8131
@richardrothkugel8131 3 месяца назад
@@git-amend Thanks, I figured it out. Your videos are great and I've subscribed to your channel. Look forward to future content.
@bromanguy
@bromanguy 2 месяца назад
Love this tip, thank you!
@drewalkemade3715
@drewalkemade3715 3 месяца назад
You're just knocking these out of the park git! Another super useful tutorial with great implementation.
@git-amend
@git-amend 3 месяца назад
Glad you like them! Thank you!
@forbiddenbox
@forbiddenbox 3 месяца назад
u are really that advanced channel to watch once you want to create big games
@git-amend
@git-amend 3 месяца назад
Thanks for the kind words!
@metaling278
@metaling278 Месяц назад
Looking forward to the music and playlist video :D. Thanks for the tutorial!
@git-amend
@git-amend Месяц назад
No problem!
@bromanguy
@bromanguy 2 месяца назад
Literally just got to the audio part of my little project and you make a video on the system. Thank you very much for your videos and explanations, as always!
@git-amend
@git-amend 2 месяца назад
Glad I could help!
@bromanguy
@bromanguy 2 месяца назад
@@git-amend Quick question if you don't mind. How would I go about stopping a specific sound. If for example I have a looping sound of a torch playing. I can activate and deactivate the torch and I would need to Play and Stop the emitter, what is the best way to handle it? Should I return a SoundEmitter in Play() method and then store reference to it in the torch script so I can Stop it whenever I need? or is there a better way to handle it? Thanks a ton for the video again.
@git-amend
@git-amend 2 месяца назад
@@bromanguy If you are playing all your sounds using SoundEmitters, then keeping a reference to it while active might be your best solution. However, also consider playing some audio without the pooling system if they are long running sounds like a torch. In this case, you could limit the size of your pool for the one shot sounds, but ensure that you still have enough voices configured in your settings to account for 10-20 long running audio clips in your scene as well. These looping or long running sounds could just be managed by the game objects that own them.
@bromanguy
@bromanguy 2 месяца назад
@@git-amend It makes sense, thank you very much for your help. You deserve way more subscribers and views!
@ItsMidNightDev
@ItsMidNightDev 3 месяца назад
Another awesome video! I particularly appreciate how you show the design process at the beginning using the diagrams to show how you initially plan what each class will do/what data it will be responsible for, then showing how each class communicates with each other.
@git-amend
@git-amend 3 месяца назад
Thank you very much!
@kutanarcanakgul5533
@kutanarcanakgul5533 3 месяца назад
In programming i think the most important thing is Memory. I really want to see your perspective on this matter. - How can we use memory efficiently in Unity? - What is the gotchas or what should we avoid? - How can find our mistakes and profile it?
@git-amend
@git-amend 3 месяца назад
That's a great topic, and much requested. There will be a video on memory and profiling coming at some point this year.
@franciscooteiza
@franciscooteiza 2 месяца назад
Thanks Adam, the video is incredible as always and of course It would be nice to have a follow up video for the playlists and music.
@git-amend
@git-amend 2 месяца назад
Thank you!
@muhammadfadilhasan691
@muhammadfadilhasan691 Месяц назад
this like an exclusive video, which gives very valuable optimization tips, thank you for the tutorial!
@git-amend
@git-amend Месяц назад
Glad it was helpful!
@semiterrestrial
@semiterrestrial 28 дней назад
Tried this out on my shmup, works really well! Thanks!
@git-amend
@git-amend 28 дней назад
Great to hear!
@4rcant
@4rcant 3 месяца назад
Absolutely gorgeous tutorial, I didn't knew about Unity's built-in ObjectPool, so thanks a lot!! About the code, i think it would be more SOLID to raise an event "onFire" and handle the SFX and VFX in its own class, like SFXCaller and VFX Caller. It's not big deal, I know, but since no one is saying it...
@git-amend
@git-amend 3 месяца назад
Thanks for the comment! You might like another video on this channel about building a Flyweight Factory which uses the Unity Object Pool in a slightly different way.
@Anvelir
@Anvelir 3 месяца назад
AAAHHHHH! That's exactly what I was thinking about a lot in recent time. Thank you very much!
@git-amend
@git-amend 3 месяца назад
Awesome! I know it's not discussed much, so I hope it helps you out!
@나만없어고양이-f9j
@나만없어고양이-f9j 10 дней назад
Using SoundData as a Scriptable Object seems like it would be more convenient! 😊
@Frenuellcrackser33
@Frenuellcrackser33 3 месяца назад
I never bothered with unitys ObjectPool class and always rewrote a new pool class for everything. I now see how easy it is to use. Great tutorial thanks !
@git-amend
@git-amend 3 месяца назад
Same actually until not too long ago. I was impressed. Thanks!
@therealvincentle
@therealvincentle 3 месяца назад
Super cool tutorial about optimizing sounds! For your music and playlists follow up, I'd love to see how you go about triggering changes in music to deal with things like horizontal sequencing/vertical layering or stingers! Thanks for the amazing videos!
@git-amend
@git-amend 3 месяца назад
Great ideas, I'm taking notes! I hadn't thought of including stingers!
@benky5478
@benky5478 3 месяца назад
Hi, I think your videos are excellent, fantastic, best in class. Please make sure you look after yourself so you can keep uploading great content into the future. Don't feel pressured to keep pumping out videos at a high pace, please nurture your passion for your channel because it's great!
@git-amend
@git-amend 3 месяца назад
Thank you, I will!
@Fitz0fury
@Fitz0fury 3 месяца назад
Hmm, this actually looks like a pretty simple refactor from my current audio managment. Little snip here, a little wrap there and poof! I dont think its really necessary for my current project but pooling audio seems like a cool optimization. Its nice to have a quick little audio solition to drop into a new project, and thanks to another recent video i can do that too!
@git-amend
@git-amend 3 месяца назад
Glad to hear that!
@mstergtr
@mstergtr 3 месяца назад
Great video! I think also having a minimum time between instances could be a useful addition. That way sounds can't play at the exact same time and have their amplitudes stack (resulting in clipping). Also, randomization of volume can be a subtle but useful parameter.
@git-amend
@git-amend 3 месяца назад
Those are some good suggestions! I'll see about implementing those in the next update!
@absencee_
@absencee_ 3 месяца назад
Well that's what I call a great video, mate!
@git-amend
@git-amend 3 месяца назад
Many thanks!
@rofu8096
@rofu8096 3 месяца назад
LETS GOOOOO I HAVE BEEN WAITING FOR THIS FOR A LONG TIME
@git-amend
@git-amend 3 месяца назад
Right on! Make sure to check the repository, there a bit more code and one optimization!
@karole5646
@karole5646 3 месяца назад
Thank you so much for this video. I hope there will be a part 2. I really want to know how you handle things like mute, loop BGM, etc...
@git-amend
@git-amend 3 месяца назад
We'll see. This video did not do quite as well as I had hoped. In the meantime, I did add some code to the repository for a Music Manager, check it out!
@Icewind007
@Icewind007 3 месяца назад
I finally have sound! And cool design patterns too!
@git-amend
@git-amend 3 месяца назад
Yay, thank you! I just made a performance commit on the repository, be sure to check it out!
@rudolfsilkens604
@rudolfsilkens604 3 месяца назад
Recent Subscriber here. Really appreciate Your content and Your delivery of said content. As an Audio Designer, I have some notes: - You could define a WaitForSecondsRealtime = new(clip.length) for the Coroutine, execute playback AND pre-set "NextPitch" in it. It might seem trivial but I go by "Shoot, Reload" order where A sound is ready to just "Play", and after each play the cylinder is rolled. Might be my DAW head seeing a line of code as plugin latency. - My personal issue with pools: I really don't like creating / destroying AudioSources, so I instead go for a "Manual Voice Limit approach" where I have a SoundEmitter[] and a "SoundEmitter GetNext()" method to cycle through the collection. This would brute force a voice count. In Your example - at the firing rate of fire of 3 turrets - we wouldn't notice when one Emitter is stopped and retriggered.
@git-amend
@git-amend 3 месяца назад
Thanks for the comment. I like the idea of playing sounds in a sequence. I'll add something like that to the repository when I have a moment!
@rudolfsilkens604
@rudolfsilkens604 3 месяца назад
@@git-amend for sequences, consider AudioSource.PlayScheduled(). In a for loop you could assign a "batch" of emitters to play a sequence. Dynamic UI notifications based on some params, music, You name it
@shenshaw5345
@shenshaw5345 2 месяца назад
cant believe this is free, thank you.
@git-amend
@git-amend 2 месяца назад
No worries!
@kantagara
@kantagara 3 месяца назад
Amazing video ! A definitive go-to guide for Game Sounds :) BTW , a very very small nitpick. Wouldn't pitch always be moving? Shouldn't it just be .pitch = rather than .pitch += ?
@git-amend
@git-amend 3 месяца назад
Thanks! The SoundBuilder will set the audioSource.pitch to the base value first, and then the random pitch adjustment is applied after that.
@techdave99
@techdave99 3 месяца назад
Glad you only covered what you did. Adding playlists and music would have been too much. I understand that these videos take a lot to make. I will support you more when my wallet lets me. Thanks.
@git-amend
@git-amend 3 месяца назад
No worries, views, comments and likes are what really drive the channel! I appreciate your support!
@rechnight
@rechnight 2 месяца назад
Amazing video as always! Tweaked your implementation to allow for random and sequential sound playing, as well as syncronized multiple audio clips. One thing that I'm wondering: - Wouldn't a ScriptableObject be better than a Serializable for the SoundData? Many times the same SFX is applied to multiple game objects, so using a ScriptableObject would be generally better in terms of performance and memory efficiency. right? Or am I missing something here? Thanks!
@git-amend
@git-amend 2 месяца назад
Nice, glad you ran with it. In regard to SOs, I think you need to evaluate that based on your project. Either way is good, but if you are reusing the same ones often then SOs are a great alternative to what's done in the video.
@aaatthh
@aaatthh 3 месяца назад
Great work, thank you 💯
@git-amend
@git-amend 3 месяца назад
Thanks for watching!
@HaustierYui
@HaustierYui Месяц назад
Hi @git-amend ! Thank you for the great tutorials. It would be awesome to get some kind of license on your code. Like could you add that to the github repos?
@git-amend
@git-amend Месяц назад
I've been adding licenses as I have time or when I feel it's necessary. Since the code is primarily what is shown in the RU-vid videos, you can assume it is free to use without restriction. The goal is to help you improve as a programmer, and I hope you take what you learn here and make it your own.
@IrakliKokrashvili
@IrakliKokrashvili 3 месяца назад
I love your videos and learning many interesting details with each one. I noted you've creating new SoundBuilder with each call. Will not it lead to memory leaks or am I missing something ?
@git-amend
@git-amend 3 месяца назад
Thanks! Actually that’s a good optimization. Caching the builder would save some unnecessary garbage collection. I’ll update the repo when I get home!
@SDB_Dev
@SDB_Dev 3 месяца назад
Great video! I am curious, I wanted to use Scriptable Objects instead of serializing the SoundData class the way you did in the video. Afaik it seems fine to literally just make that class into a scriptable object class without really changing much. Is there a problem if I do that? The only thing I wasnt sure about is the soundcount dictionary, but it seems to work fine.
@git-amend
@git-amend 3 месяца назад
You should be able to do the same thing with a ScriptableObject no problem.
@tst2648
@tst2648 3 месяца назад
Thanks!
@git-amend
@git-amend 3 месяца назад
Thanks for the super!!
@ragozh
@ragozh 3 месяца назад
With frequent sound, you enqueue every time sound played. But dequeue only happened when the queue count greater than max sound instances. I think we should also dequeue when sound emitter stop/return to pool right?
@git-amend
@git-amend 3 месяца назад
That's a good point. To implement something like that we could use a different data structure, like a Linked List. I'll see about adding an optimization to the repository. Cheers!
@damonfedorick
@damonfedorick 3 месяца назад
nice!
@git-amend
@git-amend 3 месяца назад
Thanks!
@Random_Variableee
@Random_Variableee 3 месяца назад
Hey pal! very great implementation tho but dont u think this system's complexity might be overkill for simple audio needs? like for games with straightforward audio requirements, this system might introduce unnecessary overhead??
@git-amend
@git-amend 3 месяца назад
Of course. This tutorial is geared towards people trying to solve complex audio problems in their game. This is overkill for beginner projects.
@OskGame
@OskGame 3 месяца назад
Wow, the video tutorial is great and I have 2 questions 1. In a two-sided battle with 50 enemies on each side and holding guns, if each soldier made a sound it would be very annoying and unnecessary (so what should I do when handling this situation) 2. I see in your script that using IEnumerator and creating Audio Sources will reduce performance compared to using Audio sources and calling PlayOneShot? Thank !
@git-amend
@git-amend 3 месяца назад
Thanks for the comment. In regard to #1, I would implement a throttle on that particular sound similar to what I did with 'FrequentSounds' but limit that particular sound clip to only play 2-3 at any given time. You could do that with something like Dictionary - then you would have a queue for specific sounds, and limit it's capacity to a specific amount, just like in the video. That way you can take total control of those particular sounds that are being played by too many units. For #2, I'm not totally sure what you mean, but PlayOneShot can be useful too because you can play more than one clip from one AudioSource. However, if the GameObject containing that AudioSource is destroyed, you'll lose any audio it was playing. So, you could still use pooled AudioSources and combine with PlayOneShot if your game required it.
@OskGame
@OskGame 3 месяца назад
@@git-amend oh, i will try your audio pooling application into a battle game, thank you very much for your answer.
@git-amend
@git-amend 3 месяца назад
Tonight I implemented a Linked List as a small improvement over using a Queue, have a look at the repository and see if it gives you any ideas. In this version a SoundEmitter will keep a reference to it's own Node in the list so it can remove itself when it Stops playing in O(1) time. You could still use a Dictionary to keep track of how many counts of each SFX are playing at a given time as well.
@OskGame
@OskGame 3 месяца назад
@@git-amend Thank you very much for your dedication. Sincerely thank you !
@abhijitleihaorambam3763
@abhijitleihaorambam3763 3 месяца назад
this channel is such a great resource even tho i use godot
@git-amend
@git-amend 3 месяца назад
Haha awesome glad to hear that!
@JustFor-dq5wc
@JustFor-dq5wc 3 месяца назад
I would just add laser mini gun sound in place of 1000 single bullets sound.
@git-amend
@git-amend 3 месяца назад
Even though the example in the video is just one SFX to keep it easy to understand, try to picture in your minds eye how handing 100 different SFX might require a more complex solution.
@JustFor-dq5wc
@JustFor-dq5wc 2 месяца назад
2 week later I'm rewatching your video because I need to play hundreds of same sound in short time. Don't get me wrong, you do advanced things, but sometimes I've got feelings you overcomplicate things. Complicated or not - it's useful.
@나만없어고양이-f9j
@나만없어고양이-f9j 18 дней назад
1:10 What Program is this?
@git-amend
@git-amend 18 дней назад
Excalidraw for Obsidian ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-o0exK-xFP3k.html
@RafaelAzriaiev-kv9qm
@RafaelAzriaiev-kv9qm 3 месяца назад
Noice!!!!
@git-amend
@git-amend 3 месяца назад
Thank you!
@kadircalloglu2848
@kadircalloglu2848 3 месяца назад
I think Unity's sound system sucks, fmod is much better for both sound calibration and optimization.
@git-amend
@git-amend 3 месяца назад
I agree, fmod is far superior. I think most feel it’s overkill or too hard to use. I wonder if that would make a good video?
@kadircalloglu2848
@kadircalloglu2848 3 месяца назад
@@git-amend I think it's worth it
@IrakliKokrashvili
@IrakliKokrashvili 3 месяца назад
Agreed, I am migrating to wwise. Actually ATM AudioManager handles both types of audios until I migrate all to WW
@folkenberger
@folkenberger 3 месяца назад
@@git-amend it would 100% be a good video, both studios i'm wokring right now want to implement it, it became pretty popular
2 месяца назад
Man! Thank you soo much for this!!! This is a pure gold! ❤ New sub!
@git-amend
@git-amend 2 месяца назад
Welcome!
2 месяца назад
@@git-amend do you plan to make follow up for Playlists and Music?
@git-amend
@git-amend 2 месяца назад
Maybe, but this video has not really resonated with my audience, as I suspected. Just like UI, things like audio and memory profiling are an afterthought for most people. However, I might still make one - but in case I don't, I did put some helpful code in the repository so that you can kick start your own Music management system.
2 месяца назад
I'll follow, in case you change your mind. Anyway, keep up amazing work man!
@antijulius
@antijulius 2 месяца назад
@@git-amend I really hope you build on this video at some point. Really appreciated this one. I've dabbled in FMOD but it seems like overkill for my purposes but resources for handling audio in a clean and scalable way in Unity seem to be few and far in between. It's all just "here's how to play a sound, go nuts". In any case, thanks for including the MusicManager in the repo!
Далее
6 Code Smells REFACTORED to Patterns!
15:41
Просмотров 8 тыс.
БЕЛКА РОЖАЕТ? #cat
00:21
Просмотров 602 тыс.
The #1 Way To Get Better At Programming
22:43
Просмотров 10 тыс.
Unity Code Optimization - Do you know them all?
15:49
Просмотров 191 тыс.
The Unity HACK that the PROS know
21:27
Просмотров 14 тыс.
PLEASE use a Unity SOUND MANAGER! - Full Tutorial
15:58