WEBVTT

00:00.080 --> 00:00.530
Hello guys.

00:00.530 --> 00:01.550
Welcome to the video.

00:01.550 --> 00:05.360
In this one we're going to set up background music for the game.

00:05.750 --> 00:07.190
I'm going to be not that difficult.

00:07.190 --> 00:10.070
We just need to get background music itself.

00:10.070 --> 00:12.620
Let's make a folder for sound effects.

00:14.880 --> 00:16.380
Can I call it a suffix?

00:16.410 --> 00:22.110
Can I take these drag them into folder and we're going to need to open this link.

00:22.110 --> 00:27.450
So this the music I'm going to use again I'm going to show you how to use different music if you like

00:27.450 --> 00:31.710
to uh, but as of now I'm just going to use this one because I need some example.

00:31.710 --> 00:33.210
And this music is great.

00:33.420 --> 00:35.520
Uh, you can just click Open in unity.

00:35.520 --> 00:38.340
It will open in your unity for some reason.

00:38.340 --> 00:43.680
Today the asset store is very laggy for me and I cannot open the link.

00:43.680 --> 00:47.040
Oh I can okay, so I did open the link and now I'm going to click.

00:47.940 --> 00:49.020
Uh, download.

00:51.410 --> 00:55.340
Also, I'm not going to use all ten tracks, I'm just going to use two tracks.

00:55.340 --> 00:58.430
So maybe three because I want to show you how to play random music.

00:58.430 --> 01:04.670
And when I'm transferring the project in the end of the section, I'm going to remove this sound from

01:04.670 --> 01:05.630
the project.

01:05.780 --> 01:12.620
And because I want project to be not very big in size, like if I put lots of music there, it's going

01:12.620 --> 01:15.860
to get the size is going to be bigger than I want it to be.

01:15.860 --> 01:19.400
So yeah, I want you to have easy way of downloading the project.

01:19.400 --> 01:21.680
That's why I'm not going to keep sounds in there.

01:21.680 --> 01:26.030
I mean, for the music, you'll have to download them yourself, but it's not that difficult.

01:26.030 --> 01:29.900
As you can see, I'm just doing it right now while I'm talking to you.

01:29.900 --> 01:30.830
What a magic.

01:31.800 --> 01:33.390
Now it did download itself.

01:33.390 --> 01:35.370
So I'm going to try to import.

01:36.640 --> 01:41.680
And as I said, I'm not going to use all of them because I don't want to make it heavy.

01:41.680 --> 01:44.650
So I'm going to just maybe take two.

01:45.840 --> 01:46.140
Okay.

01:46.140 --> 01:46.650
Nice.

01:46.650 --> 01:48.750
Now let me click import.

01:53.310 --> 01:54.840
And let's go back.

01:54.840 --> 01:56.310
Let's go to assets.

01:56.310 --> 02:01.170
Let's change this to background music.

02:03.500 --> 02:07.910
And we're going to take this background music and drag it to audio like that.

02:08.300 --> 02:09.170
Very nice.

02:09.200 --> 02:11.900
Now let's go to the main menu.

02:12.260 --> 02:14.600
We're going to set up things here.

02:14.630 --> 02:17.090
I'm going to go to audio background music.

02:17.120 --> 02:19.520
Going to take these two drag them here.

02:19.550 --> 02:22.850
We're going to uncheck play on awake same as we did before.

02:22.910 --> 02:24.920
And then we're going to give them parent.

02:26.110 --> 02:28.420
Background music.

02:29.170 --> 02:31.930
And we're going to make it as a child of audio manager.

02:31.960 --> 02:32.920
Very nice.

02:32.950 --> 02:34.630
Now let's go to audio manager.

02:34.930 --> 02:39.430
Somewhere here we're going to make serialized field private.

02:40.090 --> 02:44.500
Uh, audio source array BGM.

02:44.980 --> 02:47.740
And then we're going to need methods for this.

02:48.070 --> 02:51.040
And then we need a method that would play background music.

02:51.040 --> 02:56.770
So I'm going to do public void play BGM int BGM to play.

02:56.950 --> 03:03.940
And before we play the music we supposed to stop any music that is playing now or played before.

03:03.940 --> 03:13.030
So I'm going to do uh for loop for the BGM Linux, not sound effects, but BGM links, BGM links and

03:13.030 --> 03:17.410
then over here we need to do BGM with the I dot stop.

03:18.140 --> 03:20.030
So this will stop all of the music.

03:20.030 --> 03:23.990
And after that we can do BGM with the BGM to play.

03:28.640 --> 03:30.920
And let's fix it over here.

03:30.920 --> 03:34.310
So this will play the music if you just call the method.

03:34.310 --> 03:40.250
And now my goal is to play the music all the time while you're playing the game.

03:40.820 --> 03:43.010
And also I want to play random music.

03:43.010 --> 03:44.690
I don't want to play just some music.

03:44.690 --> 03:47.660
So what I'm going to do here is something simple.

03:48.820 --> 03:55.510
We're going to go ahead and make a private int, current BGM and maybe index.

03:57.530 --> 03:59.150
Let's do just BGM index.

03:59.180 --> 04:00.440
BGM index.

04:01.410 --> 04:02.010
Okay.

04:02.010 --> 04:02.700
Good.

04:03.270 --> 04:05.040
Then we're going to make.

04:06.410 --> 04:08.660
A method that would play random BGM.

04:08.660 --> 04:18.050
So I'm going to do public void play random BGM in which we're going to take a BGM index and make it

04:18.050 --> 04:19.790
equal to random range.

04:20.750 --> 04:27.170
Between zero and BGM length, so it will choose one of the elements that is in the array.

04:27.350 --> 04:31.310
And then we're going to do play BGM with a BGM index.

04:32.810 --> 04:33.380
Perfect.

04:33.380 --> 04:38.000
And when music plays and then stops, nothing is going to happen.

04:38.000 --> 04:41.300
Music will just stop playing and BGM index will not be changed.

04:41.300 --> 04:47.390
And thanks to that, we can check if music is playing right now and if no, we can just restart the

04:47.390 --> 04:48.110
music.

04:48.350 --> 04:51.080
So I'm going to make a method public void.

04:51.080 --> 04:54.290
Play music if needed.

04:54.320 --> 04:55.040
All right.

04:55.040 --> 05:02.960
And over here we're going to do if BGM with a BGM index is playing equals to false means we do not play

05:02.960 --> 05:07.460
any music, then we're going to do play random BGM just like that.

05:07.850 --> 05:14.030
And then I want to call this method every now and then, maybe every second or every two seconds.

05:14.030 --> 05:18.860
So we can just check if music is playing and if not we're going to play it again.

05:19.640 --> 05:20.150
Um.

05:21.760 --> 05:24.700
Let's go ahead and do invoke repeating.

05:27.770 --> 05:31.700
Name of play music if needed.

05:32.300 --> 05:39.410
And then we're going to give it, uh, zero delay and repeat rate can be as often as you like.

05:39.410 --> 05:41.480
I suppose that music can stop.

05:41.480 --> 05:44.060
Then it can wait for a couple of seconds and then play again.

05:44.060 --> 05:51.920
So I'm going to do check every two seconds and something else here, just in case.

05:51.920 --> 05:58.040
When we play the music let's do BGM index is equals to BGM to play.

05:58.100 --> 06:02.870
So we do not, you know, interrupt the music with this check over here.

06:02.870 --> 06:07.700
Because this music I mean this check checks BGM index that we have at the moment.

06:07.700 --> 06:09.980
And this will allow us to update the value.

06:09.980 --> 06:12.830
So this one will never be met.

06:12.830 --> 06:16.580
Otherwise you can just interrupt the music you play a certain point.

06:16.580 --> 06:23.000
Like if you want to play a boss fight in music, you can just call play BGM and choose boss fight music.

06:24.210 --> 06:27.570
But then if you do not update this value, then it will be interrupted.

06:28.020 --> 06:29.970
Okay, let's save this all.

06:29.970 --> 06:31.650
Let's go back to unity.

06:32.070 --> 06:37.290
I'm going to just assign this to VMs over here.

06:38.350 --> 06:43.420
And I'm going to go to play mode, and I'm sure I'm going to hear the music.

06:43.420 --> 06:46.960
I cannot check if it starts a new song until I see it.

06:46.960 --> 06:50.950
So I'm sorry, let's make it a serialized field.

06:50.950 --> 06:52.750
And again, I will not.

06:52.750 --> 06:54.100
Oh, what is happening?

06:54.280 --> 06:57.640
Yeah, I will not check this on the video because that would take too long.

06:57.640 --> 07:02.620
But I'll stop recording and then I'll see if New Song began to play.

07:02.620 --> 07:04.090
Okay, that's what I'm going to do.

07:04.690 --> 07:06.040
Um, yeah.

07:06.400 --> 07:10.960
Because otherwise you would have to look how I'm listening to the song until the end.

07:10.960 --> 07:11.860
Not cool.

07:11.980 --> 07:13.060
Let's go.

07:15.500 --> 07:18.830
Oh my God, I'm so sorry I jumped.

07:19.400 --> 07:20.420
So scary.

07:20.450 --> 07:21.890
Let me make volume.

07:23.330 --> 07:24.080
Point one.

07:27.100 --> 07:27.640
Anyway.

07:27.640 --> 07:28.510
It's working.

07:28.600 --> 07:29.080
Yeah.

07:29.080 --> 07:30.130
It began different song.

07:30.130 --> 07:31.510
So random is working.

07:31.510 --> 07:32.470
It's fine.

07:32.620 --> 07:36.250
And let me try to make it lower.

07:36.460 --> 07:37.510
Like even lower.

07:39.430 --> 07:40.780
I can hear it's playing.

07:40.780 --> 07:45.370
I'm going to go to continue and I can hear the music.

07:45.400 --> 07:48.370
Very cool, I like it.

07:49.540 --> 07:52.300
Um, yeah.

07:52.870 --> 07:55.030
Other than that, I guess there is nothing to do.

07:55.030 --> 08:00.160
I'm going to check if music is playing again when it's off, like off the screen.

08:00.160 --> 08:03.730
And other than that, if something is wrong, I'm going to let you know.

08:03.730 --> 08:05.080
Now let's go to the next video.

08:05.080 --> 08:07.390
We're going to set up the volume settings.
