Hello guys.
Welcome to the video.
In this one we're going to set up background music for the game.
I'm going to be not that difficult.
We just need to get background music itself.
Let's make a folder for sound effects.
Can I call it a suffix?
Can I take these drag them into folder and we're going to need to open this link.
So this the music I'm going to use again I'm going to show you how to use different music if you like
to uh, but as of now I'm just going to use this one because I need some example.
And this music is great.
Uh, you can just click Open in unity.
It will open in your unity for some reason.
Today the asset store is very laggy for me and I cannot open the link.
Oh I can okay, so I did open the link and now I'm going to click.
Uh, download.
Also, I'm not going to use all ten tracks, I'm just going to use two tracks.
So maybe three because I want to show you how to play random music.
And when I'm transferring the project in the end of the section, I'm going to remove this sound from
the project.
And because I want project to be not very big in size, like if I put lots of music there, it's going
to get the size is going to be bigger than I want it to be.
So yeah, I want you to have easy way of downloading the project.
That's why I'm not going to keep sounds in there.
I mean, for the music, you'll have to download them yourself, but it's not that difficult.
As you can see, I'm just doing it right now while I'm talking to you.
What a magic.
Now it did download itself.
So I'm going to try to import.
And as I said, I'm not going to use all of them because I don't want to make it heavy.
So I'm going to just maybe take two.
Okay.
Nice.
Now let me click import.
And let's go back.
Let's go to assets.
Let's change this to background music.
And we're going to take this background music and drag it to audio like that.
Very nice.
Now let's go to the main menu.
We're going to set up things here.
I'm going to go to audio background music.
Going to take these two drag them here.
We're going to uncheck play on awake same as we did before.
And then we're going to give them parent.
Background music.
And we're going to make it as a child of audio manager.
Very nice.
Now let's go to audio manager.
Somewhere here we're going to make serialized field private.
Uh, audio source array BGM.
And then we're going to need methods for this.
And then we need a method that would play background music.
So I'm going to do public void play BGM int BGM to play.
And before we play the music we supposed to stop any music that is playing now or played before.
So I'm going to do uh for loop for the BGM Linux, not sound effects, but BGM links, BGM links and
then over here we need to do BGM with the I dot stop.
So this will stop all of the music.
And after that we can do BGM with the BGM to play.
And let's fix it over here.
So this will play the music if you just call the method.
And now my goal is to play the music all the time while you're playing the game.
And also I want to play random music.
I don't want to play just some music.
So what I'm going to do here is something simple.
We're going to go ahead and make a private int, current BGM and maybe index.
Let's do just BGM index.
BGM index.
Okay.
Good.
Then we're going to make.
A method that would play random BGM.
So I'm going to do public void play random BGM in which we're going to take a BGM index and make it
equal to random range.
Between zero and BGM length, so it will choose one of the elements that is in the array.
And then we're going to do play BGM with a BGM index.
Perfect.
And when music plays and then stops, nothing is going to happen.
Music will just stop playing and BGM index will not be changed.
And thanks to that, we can check if music is playing right now and if no, we can just restart the
music.
So I'm going to make a method public void.
Play music if needed.
All right.
And over here we're going to do if BGM with a BGM index is playing equals to false means we do not play
any music, then we're going to do play random BGM just like that.
And then I want to call this method every now and then, maybe every second or every two seconds.
So we can just check if music is playing and if not we're going to play it again.
Um.
Let's go ahead and do invoke repeating.
Name of play music if needed.
And then we're going to give it, uh, zero delay and repeat rate can be as often as you like.
I suppose that music can stop.
Then it can wait for a couple of seconds and then play again.
So I'm going to do check every two seconds and something else here, just in case.
When we play the music let's do BGM index is equals to BGM to play.
So we do not, you know, interrupt the music with this check over here.
Because this music I mean this check checks BGM index that we have at the moment.
And this will allow us to update the value.
So this one will never be met.
Otherwise you can just interrupt the music you play a certain point.
Like if you want to play a boss fight in music, you can just call play BGM and choose boss fight music.
But then if you do not update this value, then it will be interrupted.
Okay, let's save this all.
Let's go back to unity.
I'm going to just assign this to VMs over here.
And I'm going to go to play mode, and I'm sure I'm going to hear the music.
I cannot check if it starts a new song until I see it.
So I'm sorry, let's make it a serialized field.
And again, I will not.
Oh, what is happening?
Yeah, I will not check this on the video because that would take too long.
But I'll stop recording and then I'll see if New Song began to play.
Okay, that's what I'm going to do.
Um, yeah.
Because otherwise you would have to look how I'm listening to the song until the end.
Not cool.
Let's go.
Oh my God, I'm so sorry I jumped.
So scary.
Let me make volume.
Point one.
Anyway.
It's working.
Yeah.
It began different song.
So random is working.
It's fine.
And let me try to make it lower.
Like even lower.
I can hear it's playing.
I'm going to go to continue and I can hear the music.
Very cool, I like it.
Um, yeah.
Other than that, I guess there is nothing to do.
I'm going to check if music is playing again when it's off, like off the screen.
And other than that, if something is wrong, I'm going to let you know.
Now let's go to the next video.
We're going to set up the volume settings.