Unity 2D Platformer Tutorial 21 - Audio Manager Part 1 (Sound Manager)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's up everyone in this episode we're going to learn how to create a sound system for our game let's go so the sound system is going to be in charge of playing all of our sounds in the game whether it's going to be a sound effect or simply music so we're going to go with these two different differentiations as we explained back in the introduction of the audio in the previous episode so whether it's going to be a jumping sound or a landing sound or just you know interaction with something sound or it could be something else that's gonna be looping in the background like the ambient or the music of the game we're going to do both of them but first before we start i want to explain that but first before we start i want to mention that i've got a free assets from this from the unity asset store and uh it's basically a sound clips pack let me yeah it's called free casual game sfx pack i'm gonna i'm gonna put the link in the comments in uh sorry description below you can get it it's a basically a bunch of uh sound effects that can be uh used and whatever you want i mean i'm not gonna use all of them just use them for for the tutorial and then they're different stuff you see like anyway so i'm going to put the link in the description so you can also use it if you want to do it for your game or testing reasons first of all i want to explain how can we play a sound in the simplest manner first of all let's create a game object let's call it audio test and inside of it we're going to use as we described the og source which is the main container for the audio by default it comes with an empty audio clip which you have to have to add and it plays by default and does not loop with full sound and a 2d so these are the main stuff we're gonna keep everything as it is but we're gonna i'm gonna take off the plate on start or awake because i'm gonna initiate the playing manually but for the sake of this just this small test here let me keep it on because i want to call it without the script right now so let's have this on like this i'm going to take the fourth sound i think it's it sounds like maybe oh it's actually like a tick tock so if i put this in here and then i put it play on a wake and when i start the game it should play the sound yep that's it so it plays a sound in one iteration but if we keep it loop it's going to keep on playing endlessly so one thing for our game is i want to keep the sound effects played once but the music looping and we're gonna have a script let me actually um no actually it's better if i draw it in my amazing drawing skills so we have we'll have an entity called let's call it sound system okay inside of it it's gonna be a prefab inside of it there can be a big list of you know a lot of audio clips different stuff like footsteps door opening you know of landing shotgun all these things and it would be separate like we'll have one for sfx and one for let's say music okay you also have its own list since music will be less it depends on your game and what's gonna happen is we're gonna call we're gonna call this script from outside from whatever you want let's say we wanna play a sound when we jump right so we go to the script of the character and then at the place that we do the jumping we're going to call this sound system and we're going to call a method inside of it and this method it's going to do is it's going to it's going to pick up the sound that comes after referencing from this call like for example i'm going to call this method inside of here with a reference named jump okay so it's going to translate this into fetching the proper audio making a game object put the og inside like i don't know how to draw draw audio you know something like this anyway so it plays the audio and when the audio finishes this game object gets deleted and so on and uh purpose we're deleting the same game object because we don't want to keep a lot of clutter in the game and it's gonna end up you know lagging if we play a lot of sound so this is just to save for just optimization if you don't do it you're gonna have a lot of problems so this is simply how we're gonna do it let's go start with creating the script so first of all let's create a script call it sound let's call it sound or audio manager i'm going to call it loading manager audio manager that sounds more something okay so let's minimize this some reason whenever i click anything it opens up so let's delete this stuff first of all i'm gonna have to create an audio source mean how you want to group this up the audio clips it's all up to you you can make a public list all your clips of course it's going to be other clips let's call it sfx right library uh you can do it like oh this should be like this it's a mistake you can keep it like this you can go and actually make them one by one like like you can create one you can create one that says jump uh hit point etcetera you know and you can add as much as you want some of some of some other people they can do other things like instead of referencing from the script they put it in a folder called resources and inside resources you can call it by accessing this one resources that okay it has to be inside the script so let's do this resources dot load so this one is a different approach and i would i wouldn't recommend this at the start until you get a hang of the your audio system sorry audio manager and then you can improve it to more stuff because this one is used to run assets on runtime for us we're i'm gonna go with this method and you can also make enums for example you have an enum but this is the simplest right now so right now i'm gonna i'm gonna make multiple sounds let's have point and click all right actually let me go through the sounds to find out a proper sound okay this is click this is like a something okay tick tock okay so we've got i think i found two things [Music] yeah so i'm gonna use these two this one for landing and this one when we pick up the cherry all right so i'm gonna call this landing i think it's this one wait no wait was it this one no yeah i'll use this one here landing yeah i'll call it cherry this thing's fine let's look where because of the naming it actually went over here oh that's the wrong one oh crap dick duck landing was actually number five if i'm not mistaken yeah yeah not number five landing so we got ourselves two of those stuff let's do this test one and open the script again let's have landing and sharing again this is just for testing reasons and i want to look for a slightly longer audio so i can play it as a background i don't think i have they have music in here like long sounds let's see i think all of them are just sound effects which is how it should be because i got sound effects okay i think i'm gonna use a tick tock yeah tick tock sounds better so i'm gonna also create public audio clip tick dock again naming convention should be also important for you you have to make sure that all your sound effects you have to make sure that you can call the proper stuff so you can confuse sound effects like in here i can make sfx underscore its effects underscore and then i can make music underscore i think that would make much more sense music so we we got these two referenced in here and the next thing we need to do is we need to create a game object as we just described there let's call it we'll just call it sound that's it it's elaborate it makes sense in audio source this object is gonna be basically null empty and we're gonna create it during runtime and assign the sound to it and do whatever we want to do so i'm going to keep it as it is actually this doesn't matter but let's do it anyway let's make a folder called prefab so prefab if you've never used it before it's basically a prefabricated game object that can be saved and used on multiple you know scenes or multiple events which can hold the same attribute so this is useful for let's say using bullets or you want to use it let's say pick up objects like health bars sorry head health potions or anything like this that can be used multiple times across your game and the cool thing is you can add this in here and another scene and then if you modify the stuff in here let's say i made this two this one also gets the same effect so this is really useful so let's put this like here and let's go back and let's make one sound object so go public game object sound object so this one is going to be used to be instantiated and i guess this is it for all what we need in here the other thing we need to do is we need to make this one accessible by uh external scripts and then i mean it can it can be accessed by using this find okay let's just do a test script here it can be accessed by using find game object and then audio manager but i want to make a different way i'm going to use a singleton singleton is uh basically making a static reference of the script that can be accessed from with from outside this script in a more of a direct way so what we're going to do for this one we need to go make a reference public static and then use the game object type audio manager you can call it whatever you want but i'm by practice i used i used to like keep on using this thing instance and that's it so what we've done here we made this a public static game object of the script okay so we have this entity called instance but we need to reference it to this script in here so we need to do is the most practice you use for this is assign it during the awake method cycle because it's stiff it's earlier than start so go avoid awake and what we need to do is instance equals this we'll talk about other stuff about avoiding duplications and single tones later if if needed but this is used for um like a one scene purpose but in other games i want to create this thing in a different scene like in the main menu and then i want it to come to me like i wanted to keep persistent along the whole game so this is neat this needs a lot of other stuff which we'll uh talk in a different episode but i'm going to implement it in here also to make sure that this one this game object actually stays in this scene and also goes with us another scene but if even if we come back to this scene it's gonna make sure that there will be only one element of it that's it so we're going to use this thing in here and make the void awake and this what this does just make sure that this instance or this game object this object is linked to this script in the game object so we can access it from outside like i can show you like right now let's go to the different script this is the item in here i can go audio manager dot let me make it something in here let's just go public void test okay so i can access i can go instance and then test see now i can we can access it from anywhere that we want instead of going fine so that's the first thing we can do here okay so as as we described so let's go and make something called here play sfx okay so for us to be playing sfx from outside of the script we need to send a message to this method here like uh if we go here in the fox and look for the landing part and we say play sfx and we have to reference what kind of sfx so we're going to use string you can use anything you want you can use string you can use the integers it all depends how you want to build it so i'm going to use string sfx name okay so this is done here so what we can do in here we need to actually differentiate like um a mechanism that can choose what sfx do we need to play uh easy simplest way is to go with switch so we need to apply switch to the sfx name and in it you can do whatever you want right like let's go with case first let's say we decided on having landing cherry and tiktok okay so let's go and check uh land okay landing let's make it landing another case would be cherry you know and let's just make a default one because this is important that's it so for sfx we have only two i mean the more you get the more you can add it and the bigger it becomes you can use another approach you can use the uh the index which means like a an id number and with id number you have to make a lot of cases you can have a list with 5 sounds or 10 sounds or 20 sounds and then according to the number that you send you can just play that exact sound all right so we have this in here we can now what we can do is that we can call this method with the name of the sound and it will do at the moment nothing right so what we're going to do in here we're going to create a method that call that does the whole instantiation assigning the clip and playing it all right so i'm going to call it sound object creation okay it's a bit big but it's fine so in here what we need to do is we need to create create sound object game object okay second thing assign audio clip to it to its audio source that makes much more sense play it play the audio i think that's it that's everything that we need so to create it we have a reference of the sound object and then we have to assign the audio clip source from here we need to have the reference of which sound we need to do so one thing we can do here we can go audio clip clip okay and then we will call this thing from here along with the clip that we need to use and that's it so first thing we need to do is we need to go in the landing go sound of the creation since we're doing the landing i'm going to actually reference this sfx signing so that's that's how simple it makes it does so we take the sfx string so if if we have the landing we send the landing audio clip to here to create and then play and the small hand if you can you can have the same output for multiple you know uh conditions like let's say i want to play i i send the text of landing so it's fine but what i can do is like i can say you know what i wanted also to play um land sfx okay and i can also want to play land sound so what this means like any of the if i input this or this or this it will do this thing so you can use it for multiple things let's keep it as it is first of all i'm going to go game object actually a game object let me actually explain it better so we have to do instantiation so we go instantiate and in instantiate we create a game object in a scene from an existing prefab so by default takes only one parameter which is the prefab itself but next one it takes the prefab and the parent i want to make the parent the same game object that this script is included so let's make this game object sound object and then let's go and hear transform transform means this transform of the game object which is the sound manager but uh we need to do one thing we need to access this game object so we have to make a reference of it so we go game object and make it new object okay so what we've done here we create we created this sound object empty one and then we assign it to new game object so simply we go with new object that big component audio source we're accessing the audio source of it and to change the clip audio clip we're going to have to change this parameter called clip and we add this thing that's getting passed along from the previous method so right now we've assigned the clip to it to it the last thing we need to do is play it okay new object dot component audio source dot play so this will do the simplest more straight most straightforward way of playing sounds okay so we can test it right now and i'm gonna make it um in this in a way that i'm playing that sound from when that actually he lands on the floor so let's make audio manager okay and let's drag and drop actually we don't need so we've got audio manager this one so let's make the references here properly actually we can if we click here we go landing we find it in assets we go here cherry because i've already named them and tick tock is basically tick tock and this game object is this one here prefab so we can do the same thing we go with that sound and we can find it here i guess this does it the next thing we need to do we need to actually test it out so let's go to the fox game object and if i'm not mistaken we've we have a place where we actually detect where we land when we land on the floor okay so let's uh land it um let's look at where we do this hmm because i forgot ground check move should be here in the ground check so i'm gonna look for it and get back to you quickly all right i found it it's it's in here so from from this place exactly it's getting called once whenever we land so what we're going to do here we're going to call audio manager that instance that play sfx that's called we're going to call it landing okay so let's keep everything as it is and then we look here on this object so as soon as we land on the floor it's gonna play actually let's get let's try again so let's jump see it's playing the sound without any problems but we have multiple issues in here one of them is whenever we play the sound we need to remove it because we don't want to have a lot of sound in here and then the more the game plays the more it becomes more laggy because of the sounds that we have and the next thing is we need to we're going to do audio for the background like ambient music that's looping but we'll do this in the next episode in this episode and i hope you enjoyed this so much and uh if you do hit the like and subscribe so i can keep giving you more tutorials and more content otherwise if you have more questions or inquiries hit the comment below also visit our discord channel for to stay updated other than this i'll see you next one bye [Music]
Info
Channel: Antarsoft
Views: 3,856
Rating: undefined out of 5
Keywords: antarsoft, gamedev, tutorial, unity, madewithunity, indiegame, youtuber, youtubers, unity 2d game, unity 2d course, unity 2d tutorial, unity tutorial, unity 2d درس, unity 2d, unity tutorial 2d, unity toturials, unity tutorial 2020, unity 2d tutorial for beginners, unity tutorial c#, how to create 2d game in unity, 2d game unity, game development, how to, unity sound manager, unity audio source, unity audio manager tutorial, unity audio manager, unity audio, unity game tutorial
Id: sCaaRKhsSqM
Channel Id: undefined
Length: 24min 5sec (1445 seconds)
Published: Fri Aug 28 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.