How to make an Audio System in Unity | Unity + FMOD Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone my name is Trevor and in this video we're going to create an audio system in unity using something called fmod we'll briefly go over what fmod is how it works with unity and whether or not it makes sense to use for your project then starting from a simple 2D Unity project with no audio we'll integrate fmod into that project and set up a system around it for handling many common types of video game audio including One-Shot sounds like collecting these coins looping sounds like the player's footsteps audio by distance like this sound that's louder and pans around depending on the player's position relative to the coin Ambience like this wind sound in the background and music that also plays in the background we'll also cover how to make the sound adaptive to what's happening in the game for example crossfading one music track into another and making the wind Ambience louder when we enter this blue section over here and finally we'll also hook up an in-game UI to let the player control the volume and just to note you don't need to know anything about fmod before jumping into this tutorial as I'll be doing every everything step by step and giving a high level explanation of the important parts of fmod as we go but with that said I do have another video which acts as an ultimate quick start kind of tutorial for f mod which could be useful to watch either before or even after this tutorial to get a more in-depth understanding of fmod and what it can do and to be clear I'm not sponsored by fmod or anything I've simply been using fmod for my own project and I've really enjoyed using it so far and wanted to share some of my approaches for integrating it with unity since I haven't seen a ton of resources on that topic myself and of course everything we're going to do in this video can be found on the GitHub project which will be linked in the description of this video so the first thing I want to do is go over what fmod is since that's going to be a crucial part of the audio system we'll be creating fmod is an application used for organizing managing and interacting with video game audio and it specializes in creating what's referred to as adaptive audio that just means that the audio needs to change or adapt according to things that are happening within the video game in short any audio related logic including things like randomization of audio audio by distance varying effects transitions between music mixing the audio and much more all happen within fmod from there the unity project just needs to worry about when to play that audio and in some cases how to play it through the use of something called parameters which we'll go over in more detail later in this video having this clear separation of concerns is really nice for a lot of reasons the main one being that it simplifies the code you need to write on the unity side since you can let fmod do the heavy lifting for any audio related logic this can also be a huge plus if you have a dedicated audio team since they'll likely find fmod a lot more familiar to work in rather than working directly in unity as for pricing it's completely free to use for non-commercial use and for Indie developers however there is a fee if your revenue or budget exceed a certain amount so be sure to check fmod's licensing page for the most up-to-date information and of course fmod has direct support for Unity but it also has direct support for Unreal Engine 4 and 5. that said any game engine can integrate with fmob through their API it'll just be more work to do so and it's worth noting that there are some Community made in a operations out there for some of the more popular game engines like Godot if you clicked on this video you're likely using Unity but the key takeaway here is that if you decide to switch game engines in the future you'll be able to take your knowledge of fmod and then apply that elsewhere and the last thing I'll mention is that fmod is somewhat of an industry standard for video game audio with a huge roster of companies and games that have used it so to sum this up should you use fmod to handle your game's audio fmod is a very powerful tool with a lot of depth to it meaning it can do a lot of really fancy things with video game audio in a really smart way however that does come with a somewhat steep learning curve especially if you're not familiar with basic audio engineering terminology since that's used a decent amount when reading the fmod documentation I personally think it's well worth the effort to learn both fmod and a bit of audio engineering if you want to be a well-rounded game developer however it's certainly worth considering especially if you're a beginner and already overwhelmed by all of the other things involved in making a game so in the end you'll have to determine for yourself if the time investment for learning those things is worth it to you or not second a big reason you'd want to use mod is for its adaptive audio features video games are full of use cases for adaptive audio so even if you're not really considering that at the moment it's likely you'll want some of those features at some point however if you're expecting your game to have really simple audio fmod might be Overkill and you could be better off just sticking with unity's built-in way of handling audio instead there are certainly other things to consider but for most I imagine it boils down to a couple of questions that you should ask yourself first is it worth my time to learn fmod and second do I want adaptive audio in my game if you answer yes to both of those questions I can confidently recommend fmod and hence the system we're going to build in this tutorial but if you answer no to either of those questions or just aren't sure I suggest doing a bit of research yourself to see if fmod would be a good fit for your game or not and as mentioned towards the beginning of this video I do have another video that digs deeper into fmod and what you can do with it which might also help you in making that decision so with all of that said let's finally get into how the actual audio system is going to work starting with understanding how fmod is meant to interact with unity you'll typically have a single F mod project that corresponds to your Unity project and all of your audio files will be stored in the fmod project from there you'd use those audio files also referred to as assets within fmod to create what are called events an event can be made up of a single sound clip or multiple sound clips and you can also add internal logic and varying effects to those events directly within fmod for example you might have a really simple event like a coin collected event which just plays a one shot of a single sound clip or you might have a more complex event like an event that makes up all of the ambience for a level which contains different sound clips for thunder and rain where the rain has a Reverb effect on it and the thunder sound happens at a random frequency next taking that Ambience example let's say we enter an extra rainy area of the game and we want the Thunder to happen more frequently and the rain to sound heavier the great thing about fmod is that it's actually really easy for us to adapt that event to what's happening in the game and we do this by parameterizing the event for example giving it two variables one called rain intensity and another called Thunder frequency then our game code can tell fmod at any point to increase or or decrease those variables which in fmod we can make those variables correspond to both the Reverb effect on the rain making it sound more or less intense or how frequently Thunder happens by simply changing its frequency and next after we have some events that we want to use in our game we can assign those events to what's called a bank you can think of a bank as just a collection of events of some similar type although it is important to note that a bank also gives us control over which events are loaded into memory at any given time for the game and once events are added to different banks you can build the fmont project which compiles those Banks down into files which is why Unity will actually interact with the play events change parameters and so on so that's the gist of how fmod works but now let's talk about how we're going to interact with fmod on the unity side we'll likely have a bunch of scripts in our project that need to play audio at any given time and the first step here is for those scripts to be able to reference whatever fmod event they need to play we could reference an fmod event directly from that script which is a completely fine way to go about it but then you'll have a bunch of event references throughout your project which I personally find a bit difficult to manage as the project grows welcome completely optional in this tutorial we're going to create a Singleton class called fmod events which will contain references to all of the fmod events in the project if you end up with a lot of events you might want to break that down into multiple Singleton instances or even use something like scriptable objects to store those event references instead but again for this video we're going to keep things simple and just use one Singleton class called fmod events so with that any script will be able to easily reference an fmod event and the next step is going to be creating a way to play that event to do that we're going to create another Singleton class called the audio manager which is going to be responsible for most things audio related in the game and will also be our main way of interacting with fmod so let's go through each example we're going to implement in this tutorial and see how each one's going to work for one shot sounds like collecting a coin it's pretty straightforward since the fmod runtime manager has a play One Shot method we can use so any script could call the audio manager passing in the event it wants to play and the audio manager would just use that play One Shot method to play the audio next for looping sounds like player footsteps for example we'll need to create what's referred to as an event instance which lets us play stop or control parameters for a specific event there's a bit of setup and cleanup involved when dealing with event instances so we'll create the instance through the audio manager by passing it the event we want to use and then let the audio manager handle that setup and cleanup then the script can play stop or set parameters on the event however it needs to next to accomplish audio by distance like the coin idle sound we'll be implementing later we'll need to use something called an event emitter which also requires some setup and cleanup so we'll initialize those through the audio manager as well and then deal with them much like we do the event instances we'll dig into event emitters later in this video but for now just know they work more or less like event instances where we can play or stop them as needed and next for Ambience we'll have a single event that represents the ambience for the entire scene and then when the audio manager starts up it'll create an event instance and then start playing it automatically then we can control that Ambience event through the use of parameters by adding trigger areas in the scene where when the player enters a trigger area will inform the audio manager to change a parameter for the ambience event then for music it's going to be the exact same as we did for Ambience except we'll use a string parameter to separate out different music tracks and the trigger area will allow us to switch between those tracks by changing that parameter as for how we transition between those tracks like crossfading them for example will handle all of that on the fmod side and last to allow volume control within the game fmod has a mixing section that we're going to set up with different groups for music Ambience and sound effects then we can manage the volume for each of those groups from the audio manager code from there we can easily hook up a user interface to control the volume on the audio manager which will in turn keep those changes in sync with the fmod groups it could make sense to do a separate volume manager for that part but we're just going to do everything in the audio manager for this tutorial and alternatively if you wanted to you could easily switch out any interactions with the audio manager using Unity events instead but for this video we're going to stick with the Singleton approach so I know that was a ton of info but we're going to reiterate a lot of that information throughout the implementation we do so as long as you understand the gist of what's going on you should be be okay to sum up this entire system though on the fmod side we create events using audio files which are organized into Banks which the unity project can then interact with by playing those events stopping those events or even controlling those events through the use of parameters on the unity side we're going to keep references to all of those events in a Singleton class called fmod events and then interact with those events using another Singleton class called the audio manager so that any script in the project can easily trigger audio and that's how it's all going to work so finally let's get into actually implementing this to save some time we're going to start off with a project that I've already created which is this really simple platformer level where the player just runs around and collects coins it's separated out where there's this gray area and then this blue area which later in the video will adapt the background music and Ambience depending on which area the player is in but don't worry too much about that until later in the video and to save some more time I already created the volume control UI menu it's just not hooked up to anything yet so it doesn't do anything and I'll go over the important parts of this when we get to that section of the video but otherwise it's a relatively simple scene and the focus of this tutorial is on the audio system so I'm not really going to cover things in this project outside of that but I will try to give any other appropriate context as needed throughout the video the first thing we're going to do is install fmod and get that set up with unity we'll need to install fmod Studio which can be found on fmod's official website and then we'll open it up and create a new blank project for right now just save this anywhere outside of your Unity project by going to file save as and then call it whatever you want I'm going to call it audio system fmod project next we'll also need to install the unity integration plugin you could do this through fmod's official website if you wanted to but I think it's a lot easier to install through unity's package manager so we're going to do that instead on the unity asset store just search for fmod for Unity and you're looking for the verified one by fmod and then as long as you're logged in Click the add to my assets button here then in your Unity project you can go to window package manager and then make sure my assets is selected from this drop down and then we'll click on FM mod and then click the import button to import fmod into the project and I'm not completely sure if everything here is needed or not but for this video we're just going to leave everything checked and then press import again then we can close out of the package manager window and we'll see another window called fmod Setup Wizard we'll click Start which takes us to the updating section of the Wizard and pretty much if you see green check marks throughout the process that means you're good to go for that section but if for some reason you don't see green check marks you can click on the corresponding button and then use this tool that pops up to try and automatically fix whatever needs fixing updating looks good so we'll press next for linking we have that fmod studio project that we just created so we'll link that using this top option browsing to where we saved the project and then selecting the fmod studio project file then the listener is just something that fmod uses to play certain types of events correctly like those that require positional information this section is basically going to switch out the unity default audio listener for the fmod 1 and in my experience it's best not to mix unity's default audio components with fmod so if you have listeners show up here just click the replace Unity listener with fmod audio listener button to switch those out same thing here where we don't want the mix audio systems so we'll want the disable unity's default audio by clicking this disable built-in audio button and if you previously had audio sources set up from unity's default audio system you could switch them out here but in our case we don't so we can just hit next and last if you're using git Source control it's a good idea to add these to your dot get ignore file for your Unity project so we'll just paste that at the bottom of ours and then save it and at the end we'll see that all of these have green check marks which means we should be good to go then we can click the close button to finish the setup process and just to note you may get some errors or warnings in your console while setting this up but these should be resolved after you finish the setup Wizards so you can clear the console and only worry about warnings or errors if they show up again after this our first goal is going to be playing a really simple sound so we can get a good foundation built out and the first thing we need to do is actually import some sound files into the fmod project that we can use in fmod audio files are referred to as assets so we can click on this assets Tab and drag in any audio files we want to use for our project when we installed fmod Studio it came with an examples project along with a bunch of example assets that can be used for educational purposes all of the files I just Dragged In are ones I picked out from that examples project excluding the two coin sounds which I made myself I'll put a direct link in the description of this video if you want to go grab the exact sound files that I'm using right now the start off simple we're going to create an event for the sound that happens when a coin is collected in the events tab we'll right click and then select a new 2D action since the game we're working in is in 2D this creates an event starting with an action sheet and we'll just call it coin collected an action sheet is great for really simple sounds like One-Shot sounds which is exactly what the coin collected sound is going to be timelines on the other hand make more sense if the event needs to have a concept of time and we'll see examples of that later in this video so don't worry about that for right now next under the assets tab we'll go into the sfx coin folder and then drag in the coin collected audio file to the event and if we play the event it just plays the sound a single time which is exactly what we want to happen when we collect the coin so we'll leave it at that next to be able to actually see this event from our Unity project it needs to be added to a bank we'll click on the banks tab here and then we'll see that there's already a master Bank something to note here is that the master Bank contains data about the entire fmod project and you don't want to delete this unless you're replacing it with another Master bank so leave that as it is we'll right click in this section and create a new bank which we'll call sfx from there back in the events tab we can right click on our event assign the bank and then select the sfx bank we just created you'll notice that the unassigned tag here will disappear and if we check our banks we'll see the event show up under the sfx bank and that's actually everything we need to do on the fmod side for the coin collected event now we just need to build the fmod project by going to file and then build and it'll compile down those Banks into a build folder in the project where we can see that each file corresponds to each of our banks and that's what Unity will interact with that means that anytime we change things in the fmod project we do need to do a build so that those changes show up on the unity sum side there is a live update feature to get around this where changes will directly apply to your Unity Project without having to build every time but I'm not going to cover setting that up in this video back in unity one of the first things we need to do is set the attenuation object f-mod will use for events that have a concept of where the player is at for things like panning volume or audio by distance fmod needs to know which object and the scene to use for those calculations typically this is going to be the player character so that's what we're going to use we can set the attenuation object by clicking on the main camera in the scene where we'll have this fmod studio listener component and then we can just click and drag the player game object into that slot next let's create the audio manager and get that set up to play a One-Shot sound in the scripts directory of the project we'll create an audio folder and then in there we'll create a script called audio manager and then double click it to open it up we can remove these placeholder methods and as mentioned earlier this is going to be a Singleton class meaning only one of them can exist in the scene at a time and all of our scripts will be able to access it so let's set that up by creating a public static Audio Manager instance with a private Setter and then we'll create an awake method where if the instance is null we'll log an error to let ourselves know that somehow more than one audio manager ended up in the scene but otherwise we'll set the instance to this audio manager so that sets up the class as a Singleton where we can access it through the instance variable but now let's add a way to play that coin collected sound as a One-Shot sound or in other words we want the sound to just play once and then stop we'll need to add a using statement for fmod Unity up at the top and then we can create a new public method called play one shot that takes in an event reference called sound which will be the event that gets played and a vector 3 called World position which is where in the game world that event will play from then we can actually play that by calling runtime manager dot play one shot and pass along those two variables to that where this runtime manager is from the fmod unity package back in the unity editor under this manager's game object in the scene will create a new empty game object and call it audio manager and then drag over the audio manager script we just created to add it to the scene next we can finally add some code to actually play the coin collected sound each coin in this project has a coin script attached to it so let's open that up we could play that coin collected sound from a couple of places but in the coin script there's a collect coin method that gets called when the player collects the coin so that's where we're going to play the sound at the top We'll add a statement for using fmod Unity then we'll add a serialized field private event reference variable called coin collected sound and then to play that sound in the collect coin method we'll call Audio Manager dot instant stop play one shot and then pass in the coin collected event reference and the position of this coin now back in the unity editor we'll see this section here for the event reference for the coin collected event and we can select that event by clicking the search button and then selecting the coin collected event from the fmod project and of course since I have a bunch of these coins in the scene I'll apply that to the prefab as well so that way it applies to all of them then let's go into play mode and make sure your editor isn't muted and we'll see that when we collect the coin the sound plays just like we wanted it to now as mentioned earlier in the how it works section of this video referencing events like this directly from the script is completely fine however as the project grows these references end up all around the project which I personally don't care for so this next part is optional but I do think there are some benefits to having all of the event references in a single place so that's what we're going to do in these scripts audio directory will create another new c-sharp script called fmod events and then double click that to open it up then we'll set this one up as a Singleton class as well by creating a static instance and adding an awake method where we'll log an error if the instance has already been initialized but otherwise we'll set the instance to this and then up at the top We'll add a using statement for fmod Unity and last We'll add a public event reference for the coin collected event where we can get the variable publicly but the setter is private and if you haven't seen this field syntax here before when you have a public getter and a private Setter like we're doing right now it doesn't automatically show up in the unity editor even with the serialized field portion but if you add this field keyword it does so that's why we're using the syntax now back in unity we'll create a new empty game object under the audio manager called fmod events and then drag in the fmod event script to add it to the scene and of course we'll also select the coin collected event in this section here now back in the coin script we can clean this up a bit we can remove this using statement for fmod Unity and the event reference variable then when we call the audio manager to play the event we'll reference the fmod events instance to get the event reference we want to play then we can go back into play mode to see this working just like it was before except with this setup we'll be able to view all of the fmod event references in a single spot of the project and it also cleans up the code in our other scripts a bit and that pretty much does it for playing One-Shot sounds with this system next let's do something a bit more complicated and add footstep sounds for the player back in F mod and the events tab will create a new event choose the new 2D timeline option and then call this player footsteps rather than using a single audio clip for this event we're going to use multiple audio clips along with some logic to shuffle them each time the event plays we can do this by creating what's called a multi-instrument by right-clicking in this track section here and then selecting the multi-instrument option then on the assets tab under the sfx player folder we can drag in all four of these footstep sounds to add them to the multi-instrument by default this is already set up to shuffle sounds which is exactly what we want so we can leave this as is and also the timeline is a bit small so just to note you can zoom in or out on the timeline using the open and close bracket key shortcuts so we'll zoom in a bit and make the instrument Zone a bit smaller as well just to be closer to the sound clip link and we'll see if we play the event it shuffles between the sound Clips each time the event is played so right now this acts a lot like the action sheet where we play it once and then it stops but for the player footsteps event we actually want to Loop it since we're using a timeline sheet for this event we can do that by right clicking on this black bar at the top and then selecting add Loop region and then dragging this out for the section that we want the loop on the timeline and you'll notice as well depending on the size of this Loop region The Sounds play more or less frequently so we can play around with that to get an appropriate frequency for our player's run animation alternatively we could call the event over and over again from our code without looping at all which might work better for some games but we're going to stick with this Loop technique for this tutorial then remember to assign this event to the sfx bank and of course build the project so that the newly added event shows up in unity back in unity let's first add the new event to the fmod events script We'll add a header for the player sfx and then a field for the player footsteps event reference just like we did for the coin collected event and then back in unity we can select the event we just created in fmod for the player footsteps next how we want to go about this since we have the sound looping is to start the player footsteps when the player is running and then stop it in any other situation in this audio system we'll do this by creating what's called an event instance in which we can play and stop the event as needed so let's set that up in the audio manager script We'll add another using statement for fmod.studio then we'll create a public method that returns an event instance called create instance and we'll have that taken an event reference for the event to create that instance with then in there we can declare an event instance and set it equal to runtime manager.create instance passing in the event reference We'll add more to this method in a bit but for right now we'll just return the event instance then back in unity we're going to open up the player's character controller script which is where all of the movement is handled and also where we're going to play and stop the footsteps event from you don't really need to know much about this script and of course the logic will probably differ a bit for your own game but the gist of this is that all of the movement is being handled in this fixed update method where I'm just changing the characters rigid body velocity depending on the player input so first at the top of the script let's add a using statement for fmod.studio then we'll create a private event instance variable for the player footsteps instance and then we'll create a start method and initialize the footsteps instance from there by calling the audio manager.instance.create instance method and passing in the player footsteps event reference then at the very bottom of the script we'll create a new method called update sound which is where we're going to put any code that relates to updating sound for the player character and like mentioned before since we're using the player's rigid body velocity the move we can check against that to see if the player is moving or not and this script also has a Boolean setup to determine if the player is on the ground or not so we can use that as well to determine if they're on the ground and of course if they're moving and on the ground that's when we want to play the footsteps event but we only want to start it if it's not already playing so to check for that we can declare a playback State here and then call playerfoodsteps.getplayback state where the output of this method will go straight into that playback State variable then we'll add a conditional statement here to only start the footsteps event if the playback state is currently stopped and last we can play the event by calling the start method on that event instance and then below here in an else statement that corresponds to the velocity and is grounded conditional will stop the event instance by calling it stop method when we stop an event instance we have a couple of options here which are fairly self-explanatory and for this event we're just going to use the allow Fade Out mode so that'll play and stop the footsteps event instance when we want it to and now we just have to hook that up in the fixed update method since that's where we're handling the player's movement so for this character controller in particular at the bottom of the fixed update method we'll call the update sound method we just created and we'll also call it here when the player is disabled since we want the sound to update even when the player is in a disabled state so now let's enter play mode and see what this sounds like so that's pretty good we could adjust the frequency a bit but I think that's close enough for this tutorial next we need to consider some cleanup for the event instance if the scene is ever destroyed for example if we ever transitioned to another scene right now the event instance would be hanging around through the scene transition which is something you probably don't want to happen so let's set this up in a smart way so that the audio manager takes care of this cleanup automatically back in the audio manager script We'll add a private list of event instance objects then at the end of the awake method we'll initialize that to a new list and next in the create instance method every time we create a new instance We'll add it to that list to keep track of it then we'll add a private method called cleanup where we'll Loop through those event instances in a for each Loop and then call stop using the immediate stop mode followed by release and finally We'll add an on Destroy method which will get called when this game object gets destroyed and in there we'll call that cleanup method and that does it for how we'd handle looping audio like player Footsteps in this system next we're going to cover how to accomplish audio by distance effects or in other words audio that gets louder and pans around your speakers depending on the player's position position relative to the object playing the audio we're going to do this for the coin by adding a coin idle event that we can hear only when the player gets close to a coin so like the other sounds let's first create the event in F mod and the events tab will create a new event and choose the new 2D timeline option and call it coin Idol and now since we have a few events it might be worth organizing them a bit better so we're also going to create an sfx folder and within that folder a player folder and a coin folder and then we'll drag in the events to the appropriate folders next we'll click on the coin idle event we'll go to the assets Tab and drag in the coin idle sound to add it to the event positioning it at the start of the event and zooming in a bit then just like we did with the footsteps event we're going to Loop this sound by right clicking on this top black bar adding a loop region and then stretching it out across the entire sound foreign next let's set this up to do volume and panning depending on the player's position which is actually really easy to do in fmod we just need to click on this section over here to select the entire track and then down here at the bottom we can right click here to add an effect and then we're going to choose the fmod spatializer and just to note since I think this is a bit confusing the object spatializer is meant more for specific platforms like VR so typically you just want the regular spatializer unless you have a specific reason to use the object one you can play around with this curve if you want which determines the rate at which volume changes as the player gets closer to the object but we're just going to stick with the default one and there's also this section that got added to the event where we can play around with what the event sounds like as the player moves around it by playing the event and then moving this white arrow in the middle [Music] and just like that the event is set up just like we needed to be all we need to do now is assign the event to a bank and then build the project so that we can see the event on the unity side back in unity because we organized the events into different folders We actually change those event references luckily fmod is pretty good about catching this and to update those references we can click yes in this pop-up and then scan to find any issues and then execute to fix those issues and I like the scan 8 one more time after that just to make sure nothing shows up in which case we can exit the window and if we go to the fmod events instance where we're keeping all of those references we can see those are now updated to include that folder structure anyways let's get back to the coin Idol event instead of using an event instance to play this event when dealing with audio by distance it's better to use what's called an emitter which has some other features for dealing with that type of audio so we'll click on one of the coin game objects in the scene and for this we're going to add a built-in and fmod unity component called the studio event emitter play event is just how it sounds that's when we want the event to automatically play and for now we'll just do that when the object's start method is called in the unity lifecycle stop event is when we want the event to stop and for now we're just going to leave this as none and then we can browse for and select the coin Idol event for the event section here for the attenuation we'll override it with a Min of 1 and a Max of 3 which you can see changes this white circle in our editor and is essentially allowing us to fine tune the range at which we'll hear the event this depends on if the location of the attenuation object is within that Circle or not which if you remember we set that to the player character and now we can enter play mode and we'll hear that as we get closer to the coin the audio is panning the speakers and gets louder as we get closer to the coin just like we wanted foreign [Music] if we collect the coin the audio is still playing and that's because we left the stop event as none what we want to do is stop the event when the coin is collected and none of the built-in stop events really cover that in a clean way we could leave the play event as objects start since that's working just fine but personally I like managing this through the code so we're going to change that to none as well and last we reference the event here but ideally we'd want to reference that event from the fmon events instance to keep all of those references together we'll set that up as well but unfortunately we need this to be filled out for the override attenuation settings so we're going to leave the reference here and we'll override it with the one that we put in in the fmod events instance so first let's apply this studio event emitter to the coin prefab so that it applies to all of the coins in the scene then let's open up the fmod event script to add the reference to the coin Idol event and of course back in unity we'll fill in the reference we just added with the coin Idol event next in the audio manager script we're going to add a method kind of like how we did with the create instance method so that we can keep track of the event emitters in our scene to stop them as part of the cleanup method so to do that we'll declare a private list of Studio event emitters called event emitters then in the awake method we'll initialize that to a new list and then we'll create a public method that returns a studio event emitter called initialize event emitter where we'll pass in the event reference we want to override it with and the game object the emitter is attached to then we'll get that emitter off of the game object using the git component method and then we'll override the event reference to the one that's passed into this method and we'll also add the emitter to the list for cleanup and finally we'll return the emitter last when this cleanup method is called we'll Loop through each emitter in that list and then stop it if we don't do this here any emitters that are playing when the scene is destroyed we'll continue playing which we most likely don't want to happen next in the coin script We'll add a using statement for fmod Unity then just for clarity we'll also add a require component annotation here of type Studio event emitter so that way anytime we have a coin script on a game object Unity will complain at us if we don't also have an event emitter on that game object then we can declare a private Studio video event emitter called emitter and in the start method of this script we'll assign that emitter to audiomanager.instance.initialize event emitter passing in the coin idle reference from the fmod events instance and this stock game object so that it uses the emitter that's attached to this coin and after that we can start the event using emitter.play and of course when we collect the coin we'll want to stop the event so we can do that here in the collect coin method by saying emitter dot stop and now when we go back into play mode we can see that this is working just like we want it to where when we collect the coin the audio for that coin idle event stops and that covers how you'd go about doing audio by distance in this system so between the three examples we've done so far that should give a pretty decent starting point for most sound effects that you might want so next we're going to add some wind Ambience to the background of the game and we're going to change the wind to be more intense when we enter this blue section over here starting in fmod under the events tab will create a new folder for Ambience and then create a new event under that choosing new 2D timeline and calling it wind then we'll drag in the wind sound from the assets tab under Ambience position it at the beginning and then Loop the entire thing by adding a loop region just like we did with the coin Idol event foreign next since this isn't a sound effect we'll add a new bank and we're actually going to call this the level one bank because we're going to pretend that there's wind sound corresponds to the first level of the game then we'll assign the wind event to the bank and then build the project so that those changes show up on the unity side then just like with the other event references We'll add a reference for the ambience event to the fmod events script and then back in unity we'll select the wind event for that spot now for the ambience we can start this however we want to but for this tutorial we're going to assume that all of the ambience for a single level is contained in a single event and so we'll just have the audio manager create and kick that off itself so in the audio manager script we'll create a private event instance for the ambience and then we'll create a new method called initialize Ambience that takes in an event reference for the ambience event and in here we'll initialize the ambience event by calling create instance and then we'll simply start it and as for when we call this we'll just create a start method and then call it from there passing in the ambience event reference from fmod events and now if we go in the play mode we'll see that the wind sound plays in the background just like we'd expect although it is a bit loud and we'll change that in just a minute so next let's make the wind sound more or less intense depending on where we are in the game in fmod we can do this by parameterizing the event to add a parameter we can go to window preset browser and then click new parameter and we'll call this one wind intensity which is just going to be a value between 0 and 1 and we'll leave the rest of these fields as defaults then we can hit OK and then close out of the preset browser window then with the event clicked on like I just recently mentioned it was a bit loud to begin with so let's bring the volume down by using this knob here next we're going to add an effect to the track so we'll click on the track and then down here we can right click add effect and then choose gain and of course this is just a simple example for this tutorial but all we're going to do here is Alter the value of this gain to make the wind sound more or less intense or in other words sound louder or quieter so to parameterize this we just need to right click on the knob here and then select add automation then click add curve and we'll select the wind intensity parameter that we just created and we'll see that parameter show up towards the top here and as we change the value between 1 and 0 it hits a point on this red line here this red line determines the value of our gain depending on the parameters value so we can click on it to add a couple of points and we're just going to make it a linear increase from negative 10 to 10 and now as the parameter increases so does the gain effect thank you don't forget to build the project again so those changes show up on the unity side and we'll jump straight into the audio manager code to add a method to change parameters on the ambience event instance so we'll create a method called set Ambience parameter that takes in a string for the parameter name and a float for the parameter value and inside of that we'll call Ambience event instance.set parameter by name passing along both of those values next we just need a way to change those parameters based on where the player is in the game will create a new empty game object call it area change trigger add a box collider 2D to it and make it tall enough to where the player can't jump over it then we'll create a new c-sharp script called Ambience change trigger and then double-click it to open it up we can get rid of these placeholder methods and then we'll add two variables one for the parameter name and another for the value of that parameter then we'll use the on trigger enter 2D method to detect when something enters the collider that this script is on I have things set up where the player character is tagged with the player tag so we can check against that to see if it's the player that entered the collider and if it is we'll call the audio manager Ambience parameter method that we created passing in the parameter name and parameter value and I actually made a small mistake here so back in the audio manager the set Ambience parameter method should be public not private so that we can access it back in unity we'll drag that script onto the area change trigger game object and we'll also check this is trigger Boolean on the box collider I'm actually also going to make this a prefab by dragging it into the prefabs folder of the project and then zeroing out its positions in the prefab then we'll drag out another one of these and position them side by side in the scene for the left one we'll rename it to gray area change trigger and the right one will rename it to Blue Area change trigger and for the blue one we'll set the wind intensity parameter to 1 and the gray one will set it to zero and to make testing this a bit quicker we can drag the player over here as well now in play mode we'll see that when we enter the Blue Area the wind sound gets louder and when we enter the gray area the wind sound gets quieter but it's pretty abrupt sounding and it would be a lot nicer if the sound faded into the new intensity value rather than cutting directly to it luckily this is really easy to do in this system back in F mod under the wind event we can click on the Wind intensity parameter over on the far right side and then all we need to do is change the seek speed here to something other than instant and we'll see that the parameter now moves to the new value over time rather than moving directly to it and that's all we have to do be sure to build the project again and then we'll jump back into play mode to try this out fair enough it now Fades to the new value just like we wanted it to hopefully this illustrates how to adapt this simple event using a simple parameter but keep in mind that the concepts here can be applied to any event and pretty much any value in fmod so think of this Ambience example as more of a foundation to build off of but otherwise that does it for ambience in this tutorial next we're going to get things set up to play a background music track we'll start things off by doing pretty much exactly what we did for Ambience so an F mod will create a new folder for music and then create a new event choosing the 2D timeline option and then we'll call it level 1 music then from the assets tab we'll drag out this music clip here and then add a loop region to Loop the entire thing and then we'll assign this event to the level 1 bank and then build the project next back in the fmod events script We'll add a reference for the music event and after that in the unity inspector we'll choose the music event we just created in fmod and then in the audio manager script We'll add a private event instance for the music just like we did for the ambience and we'll also add an initialize music method where we're going to create the instance and then start it right away and just like we did for Ambience we'll call this from the audio manager's start method with all of that in place we can enter play mode and we'll hear that the music is playing [Music] so that would do it if you just have one music track but if you have more than one music track chances are you'll need a good way to transition between them so let's set that up next by transitioning to a new music track when we enter the Blue Area back in fmod we're going to add a new parameter by going to window preset browser and then clicking new parameter this time though the parameter is going to be a string so in this drop down select the user labeled option we're just going to call it area then for the options we're going to do gray area and blue area and make note of the values here because that's actually what we need to pass fmod to transition to each label the rest of these we can leave as defaults and then hit OK and then we can close out of the preset browser now in the level 1 music event up here we're going to click this button to add a new sheet and we're going to choose the parameter sheet and choose the area parameter that we just created and then we can get rid of the timeline sheet that we had previously by right-clicking it and selecting remove sheet so now we have a sheet with two different sections and a parameter that will put us in either of those sections how this is going to work is we're actually going to create what's referred to as a nested event for each section and then set those nested events up just like we had that previous timeline sheet for the music before to do so we'll first right click in the gray area and select add event instrument which creates a nested vent over here that we can rename to gray area music and then we'll do the same thing for the Blue Area renaming the nested event to Blue Area Music it's important to note that these nested events that is the events that are under another event in the project like this won't be seen by the unity project instead the idea here is that the unity project can see the level 1 music event which we'll reference and play those nested events so now all we have to do is set up each of those nested events for the gray area music event we'll drag in the asset we were using before and then we'll Loop the whole thing and for the Blue Area music event we'll drag in this other music asset and then Loop that one as well and now when we play the level 1 music event we can switch between those nested events by changing that parameter like so foreign example you'll notice that the transition is instant and it might be preferable to Crossfade the tracks when we do the transition and luckily this is really easy to do in F mod all we have to do is select one of the event instruments and then right click on the volume down at the bottom select add modulation and then choose ahdsr and then we'll want to do the same for the other event instrument and with that in place now when we transition between the tracks They Crossfade thank you and without getting into too much detail if you wanted to play around with how it's crossfading you could adjust the curves on each of the ahdsr sections that we added but in this situation I think they sound pretty good just with their default values so we'll leave it like that and then remember to build the fmod project and then next let's set things up on the unity side to handle this to deal with the mappings between the fmod label parameter and unity it's usually a good idea to make an enum that matches those labels so let's do that first in the scripts audio directory will create a new c-sharp script called music area and then double click it to open it up we can get rid of these placeholder methods and mono behavior and then we'll also change this from a class to an enum then for the values we want this to match the fmod area parameter labels exactly so if you recall from doing that the gray area had a value of 0 and the Blue Area had a value of 1 and next in the audio manager let's create a method to set the music area parameter we'll create a new method called set music area that takes in a music area enum value called area and then we'll pass it along using the music event instance on set parameter by name method where the parameter is going to be the string area and for the value since it needs to be a float we'll type cast the enum value as a float and to tie all this together we just need to create a trigger script to trigger that change so we'll create another new c-sharp script called music change trigger and then double click it to open it up this is going to be really similar to the ambience change trigger script we created earlier except the variable is going to be an option from the music area enum and then when the player enters the collider we'll call the audio manager set music area method that we just created and back in unity we'll drag this script onto one of the area change game objects and then apply that to the prefab so that it appears on both of them and of course for the Blue Area we'll select the blue area in this drop down and for the gray area we'll select the gray area now we can go in the play mode to test this out and we'll see that the music changes depending on what area we're in and it even crossfades nicely into the new track that we're transitioning to thank you [Music] and that does it for dealing with background music in this system the last thing we're going to cover in this tutorial is how to go about volume control from the game in fmod let's go to window and then mixer which will open up the mixer for this project this is where you'd go about mixing the entire project as an audio engineer but we're not going to focus on that exactly instead the goal here is going to be setting this up in a way that it's easy for the player to control different types of volume by default we already start off with a master bus and as you can see all of our events are under that basically we can access and change the volume of any bus in this mixing window from our Unity code and let's say we want to give the player volume control for music Ambience and sfx all separately in that case we'll create What fmod refers to as a group bus by right clicking on the master bus and selecting new group then we'll create group buses for music Ambience and sfx next we need the tel fmod which events correspond to which group which we can do so by just dragging the events under the corresponding group like so and that's actually all we have to do on the F mod side so make sure you build the project and then we're going to jump straight into the audio manager script towards the top of this script we're going to create public float variables for the master volume Music Volume Ambience volume and the sound effects volume each with a range from 0 to 1 and initialize to 1. then we'll also create private bus variables for each of the buses we need to connect to being Master music Ambience and sound effects and then at the end of the awake method we can get references to those buses using the runtime manager.getbus method where we pass in a string that looks exactly like this starting with the word bus and then a colon and Slash and then the name of the group bus or nothing for the master bus and finally We'll add an update method wherefore each of those buses we'll set the volume according to the variable we have declared at the top now if we go in the play mode we can play around with those Sliders in the inspector to control the game's volume for each one of those volume types but of course the last step is going to be giving the player the option to control that sound to their own preference so let's hook up this system to a user interface to save us some time I've already created a really simple UI for volume control it's just made up of a backing panel some text mesh Pro labels and some sliders which you can create in Unity by right-clicking going to UI and then selecting slider I also have a volume menu script set up which just hides or shows the UI when the player presses the pause button which in this case is the escape button so all we need to do from here is create a script that puts in sync the value of the group bus with the value of the corresponding slider we'll create a new c-sharp script called volume slider and then double-click it to open it up we can remove these placeholder methods and then we'll add a using statement for Unity engine.ui and then we're going to create a private enum within this class called volume type where the type will correspond to the group bus that we want to access then we'll create a serialize field private volume type variable called volume type which is what's going to tell us what type of slider this is and next we'll create a private slider variable and initialize that in the awake method using the git component in children method since the slider component is going to be on a children game object where we're going to attach this script then we'll create an update method and then we'll set up a switch statement for the volume type leaving each case empty for right now and in the default case we'll log a warning if for some reason we get an unexpected volume type through the switch and then we'll also create a public method called onslider value changed and we'll copy and paste that entire switch statement into there as well and then when the slider value changes we'll want to update the variable in the audio manager according to the slider so we'll do that for each volume type using the corresponding variable in the audio manager for that volume type and in update we want to do the opposite setting the volume slider to whatever the audio manager's current values are for each volume type then back in unity under the volume menu we can attach that script to one of the sliders and then hook up that on slider change method through the inspector by clicking on the slider game object adding an on value changed listener by clicking this plus sign dragging in the game object that has the volume slider script and then selecting the on Slide better value changed function in this drop down and since I have these set up as prefabs we can apply those changes to all of them and then we just need to go through and select the correct volume type for each slider and with all of that in place we can play the game press the pause button to open up the volume control menu and then now we can use these sliders to control the volume from the game and that's it for this video I know this was a really long tutorial with a ton of information packed in it so Props to you if you made it to the end I really hope this can act as a good starting point for your own audio system so let me know in the comments if it does or not and if you thought the video was helpful please give it a thumbs up so more people see it and if you'd like to see more from me be sure to subscribe as well doing so helps out this Channel and I really appreciate it you're also welcome to come by my Discord server which is a great place to hang out whether you're just learning about game development working on a passion project or just want to share what you're working on which I'd love to see you can also follow me on Twitter Instagram or tiktok where I mostly post about the game that I'm currently working on anyways thanks again for watching and I hope this was helpful [Music] foreign [Music] [Music]
Info
Channel: Shaped by Rain Studios
Views: 48,383
Rating: undefined out of 5
Keywords: trevermock, trevormock, trevor, trever, mock, unity, unity2d, unity3d, fmod, fmod unity, fmod unity tutorial, fmod tutorial, unity tutorial, fmod unity integration, how to make an audio system, how to make an audio system in unity, how to create an audio system, how to create an audio system in unity, audio system unity fmod, fmod unity audio system, how to make a sound system in unity, fmod unity sound system, sound system, sound system unity, unity audio, unity sound
Id: rcBHIOjZDpk
Channel Id: undefined
Length: 49min 36sec (2976 seconds)
Published: Mon Dec 05 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.