WEBVTT

00:00.050 --> 00:00.650
Hello guys.

00:00.650 --> 00:02.030
Welcome to the next video.

00:02.030 --> 00:05.660
So in this one I'm going to delete this game object.

00:05.660 --> 00:08.630
And also I want to delete this Playerprefs example.

00:08.810 --> 00:11.330
Also let's clean this up a bit.

00:11.330 --> 00:12.830
Let's take these two.

00:13.420 --> 00:15.430
Drag them into scripts folder.

00:21.650 --> 00:22.940
In the scripts folder.

00:22.940 --> 00:25.490
I think it's time to make a UI folder.

00:26.030 --> 00:26.750
UI.

00:29.670 --> 00:32.490
And we can take it all here.

00:32.910 --> 00:33.360
Cool.

00:33.360 --> 00:37.650
Now we have a goal of saving information about progression of the levels.

00:37.980 --> 00:43.350
Every time we finish the level, we want to unlock next level.

00:43.350 --> 00:46.380
So let's go to the game manager.

00:47.340 --> 00:52.530
And first of all, I want to make a variable because we're going to use it a couple of times in different

00:52.530 --> 00:53.160
methods.

00:53.160 --> 00:56.460
Private int next level index.

00:58.160 --> 01:04.940
Now let's go here and say that next level index is equals to current level index plus one.

01:05.390 --> 01:06.200
Nice.

01:06.230 --> 01:11.510
Now let's go down to the methods we created in the last video over here.

01:11.630 --> 01:14.150
First of all we do need a local variable anymore.

01:14.150 --> 01:15.860
We have it saved globally.

01:15.860 --> 01:21.080
And also I think we need to arrange this a bit because level finished method will do a couple of things.

01:21.080 --> 01:24.680
Not only load the next scene, but also it's going to save information and so on.

01:24.680 --> 01:30.590
So better we make something like private void load next scene.

01:31.790 --> 01:32.600
All right.

01:32.600 --> 01:38.600
And we can take this code from here, paste it in here just like that.

01:38.600 --> 01:43.220
And then in the level finished we're going to call load next scene.

01:43.220 --> 01:46.190
Actually I could just do extract method anyway.

01:46.190 --> 01:53.060
Also I want to move this above over here and maybe lock this because we know how they work.

01:53.060 --> 01:54.530
We don't need to do anything here.

01:54.530 --> 02:00.620
Now when we finish in the level we load in the next level, and at the same time we could save information

02:00.620 --> 02:02.600
about next level being unlocked.

02:02.600 --> 02:07.460
So when you go to level selector, you see that this level is unlocked because you played this before.

02:07.460 --> 02:08.720
This is how we're going to do that.

02:08.720 --> 02:10.670
We're going to say Playerprefs.

02:11.760 --> 02:13.350
Dot set integer.

02:14.010 --> 02:15.360
We're going to type level.

02:15.360 --> 02:20.100
Then plus next level index plus unlocked.

02:20.100 --> 02:22.050
And we're going to give it a value of one.

02:22.050 --> 02:26.940
So this will save the number one which will use as a boolean just like I showed you in the last video.

02:26.940 --> 02:31.050
Now let's go ahead and complete all of the levels so we can see the progression.

02:31.320 --> 02:34.050
And we can use it to create the buttons.

02:34.050 --> 02:35.550
I'm going to go to New Game.

02:38.240 --> 02:40.880
And I'm going to quickly complete the levels.

02:42.550 --> 02:43.540
Two.

02:46.580 --> 02:47.990
No no no.

02:49.510 --> 02:50.950
And one more.

02:54.220 --> 02:54.790
Good.

02:54.790 --> 03:01.420
So now, if I'm not mistaken, I did call this method in every level and I unlocked next level every

03:01.420 --> 03:02.020
time.

03:02.020 --> 03:05.710
Now let's go to Level Selection script.

03:07.250 --> 03:08.030
And over here.

03:08.030 --> 03:13.430
It might be a little bit of overkill, but I think it's better this way because we can, um, we can

03:13.430 --> 03:15.110
see information in the inspector.

03:15.110 --> 03:23.000
So I'm going to make serialized field private, uh, bool array, uh, levels unlocked.

03:23.270 --> 03:32.510
And we need to go and do something like private void load levels, load levels info.

03:33.020 --> 03:33.800
All right.

03:34.100 --> 03:35.540
Now we want to.

03:36.490 --> 03:36.970
Yet.

03:36.970 --> 03:38.200
Levels amount.

03:38.200 --> 03:39.610
I'm going to copy this.

03:40.150 --> 03:43.270
Go here and then I'm going to do.

03:44.020 --> 03:45.520
Uh levels unlocked.

03:45.910 --> 03:48.760
Array equals to new boolean of levels amount.

03:48.760 --> 03:50.140
So we're going to make an array.

03:50.350 --> 03:54.010
And it will be equals to the size of the levels amount we have.

03:54.070 --> 03:57.700
Then I'm going to do for loop for the levels amount.

03:57.700 --> 04:01.810
Actually before we do any check let's make it equals to one over here.

04:01.810 --> 04:02.020
Right.

04:02.020 --> 04:04.090
So we start from the first one and zero one.

04:04.090 --> 04:05.170
We're just going to ignore.

04:05.170 --> 04:12.490
Now let's go and make a boolean a local variable level unlocked equals to Playerprefs.

04:12.490 --> 04:22.960
Get int of level plus I plus unlocked and default value will be zero.

04:23.500 --> 04:27.970
And if this one is equals to one, it means level is unlocked.

04:27.970 --> 04:28.960
And then we're going to do.

04:28.990 --> 04:35.620
If level unlocked, then levels unlocked with the I equals to true.

04:36.040 --> 04:41.800
And also, no matter what the progress was before, we need to be sure we unlocked the first level.

04:41.800 --> 04:47.770
So I'm going to go ahead here and I'm going to do levels unlocked one equals to true.

04:48.100 --> 04:48.730
All right.

04:48.730 --> 04:50.590
Now let's go to start method.

04:50.590 --> 04:57.280
And before we create level buttons let's call this load levels info I'm going to save it.

04:57.280 --> 04:59.980
Let's go back to unity and see the result.

05:00.010 --> 05:05.530
Now back in unity let me select New Game and let me assign level selection over here so I can switch

05:05.530 --> 05:06.790
to level selection.

05:06.790 --> 05:08.140
Let's go to play mode.

05:09.540 --> 05:11.340
I'm going to click New Game.

05:11.340 --> 05:14.160
And on the level selection we can see that.

05:15.310 --> 05:18.130
We can see that all three booleans are unlocked.

05:18.490 --> 05:19.300
Nice.

05:19.330 --> 05:21.490
Now let's go to.

05:21.490 --> 05:23.740
Now let's go to UI level selection.

05:24.040 --> 05:29.590
And over here we want to check if level is unlocked because if it was not unlocked there is no point

05:29.590 --> 05:30.790
of creating a button.

05:30.790 --> 05:32.500
So this is how are we going to do that?

05:32.500 --> 05:35.590
I think it's better to make a private.

05:36.950 --> 05:43.790
Private bool method is level unlocked, just so it's a bit easier to read everything, and this one

05:43.790 --> 05:48.740
going to return me levels unlocked with the I boolean.

05:49.250 --> 05:53.930
Just that sorry, not I um, this should be.

05:54.690 --> 05:56.430
Int level index.

05:56.430 --> 05:58.320
And here we pass level index.

05:58.350 --> 05:59.340
Okay nice.

05:59.340 --> 06:00.750
So now we have this boolean.

06:00.750 --> 06:08.550
And we can go here and do if is level unlocked with a I equals to false then return.

06:08.550 --> 06:10.020
And we're going to interrupt the for loop.

06:10.020 --> 06:11.970
We're not going to create any more buttons.

06:11.970 --> 06:16.050
Now let me go back to unity.

06:21.390 --> 06:25.770
I'm going to go to edit and I'm going to click clear all Playerprefs.

06:25.770 --> 06:29.010
So it's going to clear all of the saved information.

06:29.040 --> 06:30.750
Now we're going to go to play mode.

06:34.040 --> 06:36.560
A new game and we have only level one.

06:36.860 --> 06:38.420
Now let's go further.

06:38.420 --> 06:40.370
Let's complete one of the levels.

06:41.290 --> 06:43.690
So now we have two levels unlocked.

06:43.690 --> 06:45.220
We're going to go to play mode.

06:47.580 --> 06:52.560
New game, and we can see that only two buttons were created, even though we have three levels in the

06:52.560 --> 06:53.100
game.

06:53.100 --> 06:54.180
Okay, perfect.

06:54.180 --> 06:55.680
Now we have a simple goal.

06:55.680 --> 07:01.260
We need to check if player has any progression in the game and if he has a progression.

07:01.260 --> 07:07.470
We need to know what the level he played last and then we need to load that level if he presses the

07:07.470 --> 07:11.910
continue button, and also if there is no progression, we need to hide the continue button itself.

07:11.910 --> 07:19.470
So how it's going to be done, first of all, uh, let's go to uh, Game Manager.

07:20.610 --> 07:29.610
And I'm going to make a new player pref here player pref set int last completed level number.

07:29.610 --> 07:34.260
And here we're going to pass next level index I guess.

07:34.380 --> 07:38.790
Or maybe let's just save it as next level number because we press continue and we're supposed to play

07:38.790 --> 07:39.570
the next level.

07:39.570 --> 07:43.830
So I guess let's type continue level number.

07:44.250 --> 07:47.040
Yeah I guess it serves the purpose very well.

07:47.040 --> 07:51.060
Now let's save this and let's go to main menu.

07:53.130 --> 07:55.020
And we're gonna make.

07:55.820 --> 07:57.950
We're going to make a reference to the button.

07:57.950 --> 08:03.920
Let's do serialized field, private game object, and a new button.

08:04.340 --> 08:05.120
Nice.

08:05.270 --> 08:07.850
Now we're going to go ahead and, um.

08:10.320 --> 08:11.400
Let me think a bit.

08:11.430 --> 08:13.110
We need some sort of a method.

08:13.140 --> 08:13.320
Right.

08:13.470 --> 08:14.040
Pull.

08:14.750 --> 08:17.540
As level progression.

08:18.440 --> 08:24.620
And this will return me a boolean of bool as level progression equals two.

08:24.650 --> 08:28.940
We need to take that line of code from a layer manager.

08:28.940 --> 08:32.000
I'm going to copy this so I don't make a mistake with the name.

08:32.000 --> 08:33.860
And over here I'm going to paste this in.

08:33.860 --> 08:36.980
But I'm going to change it from set to get int.

08:36.980 --> 08:38.930
And default value will be zero.

08:38.930 --> 08:43.940
And this will be true if it is bigger than zero.

08:44.360 --> 08:45.440
Simple as that.

08:46.760 --> 08:50.300
And then over here we're going to just return his level progression.

08:50.750 --> 08:51.440
Nice.

08:51.470 --> 08:58.580
Now we need to may now we want to use this reference to enable button in the start.

08:59.150 --> 09:02.630
Now we're going to use this boolean to enable continue button.

09:03.140 --> 09:04.730
In the start we're going to do this.

09:04.730 --> 09:10.580
If has level progression then continue button set active.

09:10.610 --> 09:11.270
True.

09:11.720 --> 09:12.620
Simple as that.

09:12.620 --> 09:14.420
Now let's go ahead and make this button.

09:14.420 --> 09:21.530
We need to go back to unity I'm going to take new game I'm going to duplicate this and move it up.

09:23.570 --> 09:27.470
Let's change it to continue.

09:28.510 --> 09:31.570
And here we're going to do continue.

09:31.600 --> 09:34.450
Probably we should do a different layout for buttons.

09:34.840 --> 09:37.390
But we're also going to have settings button later on.

09:37.570 --> 09:38.890
For now I'll place it here.

09:38.890 --> 09:39.580
It's okay.

09:41.160 --> 09:47.040
Now for this button we need the method that will load the last level in progression.

09:47.040 --> 09:50.160
So probably it's yeah, we can just make a method here.

09:50.160 --> 09:50.910
Why not.

09:51.150 --> 09:52.890
Let's make a method.

09:54.530 --> 10:06.110
Uh public void continue game and this will just do scene manager load scene with uh all we need the

10:06.110 --> 10:06.620
index.

10:06.620 --> 10:07.610
This will take long.

10:07.610 --> 10:08.270
Okay.

10:08.390 --> 10:10.280
Let's do curly brackets here.

10:10.790 --> 10:13.130
And we're going to do int level two.

10:13.130 --> 10:16.430
Load equals to I'm going to get this one.

10:18.670 --> 10:19.630
Okay.

10:19.750 --> 10:30.370
And over here we're going to do load scene level underscore level to load underscore plus level to load

10:30.370 --> 10:31.390
like that.

10:31.600 --> 10:32.440
Okay.

10:32.800 --> 10:34.480
It's a bit confusing I guess.

10:38.110 --> 10:41.230
I'll try to find a way to clean this up, but as of now, this should work.

10:41.650 --> 10:44.290
And let's drag Continue Field over here.

10:44.290 --> 10:49.030
And then on the continue button we're gonna switch to.

10:49.790 --> 10:50.540
UI.

10:52.350 --> 10:54.600
Continue game and just.

10:54.600 --> 10:55.080
That's it.

10:55.080 --> 10:57.660
Now let's disable this button by default.

10:57.660 --> 10:59.580
And I'm going to do file.

10:59.580 --> 11:02.130
Now edit clear all playerprefs.

11:02.130 --> 11:05.310
So if I'm not mistaken, now we're supposed to go to play mode.

11:05.310 --> 11:07.380
And there are going to be only new game.

11:10.370 --> 11:11.090
Yes.

11:11.090 --> 11:11.510
Okay.

11:11.510 --> 11:14.990
Now I'm going to go to New game and I'm going to complete one of the levels.

11:17.370 --> 11:19.020
So I've completed the level.

11:19.230 --> 11:20.250
This is all nice.

11:20.250 --> 11:24.180
Now let's say I'm on this level, and it's a bit difficult for me and I want to take some rest.

11:24.180 --> 11:26.580
So I decided to stop playing.

11:26.580 --> 11:30.150
I go out of the game, then I'm like, oh, I want to play again.

11:30.150 --> 11:31.710
So let's go to play mode.

11:31.710 --> 11:35.340
And here now we have a continue button.

11:35.340 --> 11:36.090
Cool.

11:36.090 --> 11:37.980
Now we can click this button.

11:37.980 --> 11:40.830
And if I'm not mistaken we're going to go to the second level.

11:40.920 --> 11:41.790
Continue.

11:41.790 --> 11:42.930
Yes okay.

11:42.930 --> 11:44.340
Now I'm going to go further.

11:46.280 --> 11:51.500
Complete another level and then I'm like, oh no, I need to go to eat some food.

11:51.500 --> 11:53.570
So I'm going to exit the play mode.

11:54.020 --> 11:55.130
Enter play mode.

11:56.410 --> 11:58.180
Now let's say I want to start new game.

11:58.180 --> 12:03.910
I can choose a level one, two, three because all of them are unlocked, or I can just click continue

12:03.910 --> 12:07.630
and I'm going to play from the last level I played.

12:07.990 --> 12:08.950
Perfect.

12:08.950 --> 12:09.850
Love it man.

12:09.850 --> 12:10.870
Love it so much.

12:10.870 --> 12:13.360
And also it's appearing there.

12:13.360 --> 12:17.500
We can see it's, uh, being enabled, but with a fade image.

12:17.500 --> 12:20.650
It's not going to be like that if you make it dark here.

12:22.390 --> 12:25.120
And go to play mode, then it's going to be.

12:26.060 --> 12:26.930
Just fine.

12:26.930 --> 12:27.770
Okay.

12:27.800 --> 12:28.730
Hello, guys.

12:28.730 --> 12:31.190
Before we finish this, let me show you a bug we have.

12:31.190 --> 12:36.830
So if you complete all of the levels and then you click continue button, it's going to give you an

12:36.830 --> 12:37.520
error.

12:37.700 --> 12:39.260
It's very easy to fix.

12:39.800 --> 12:41.180
You see I'm clicking continue.

12:41.180 --> 12:47.390
And we see level four could not be loaded because it has like we don't have this scene in the build.

12:47.390 --> 12:55.310
So this happens because we load scene with the get int continue level number and we set this continue

12:55.310 --> 12:57.020
level number integer.

12:57.020 --> 13:01.520
When we finish the game in the Game manager over here, this is what we do.

13:01.520 --> 13:04.400
And we set it to next level index no matter what.

13:04.400 --> 13:09.830
But if we don't have any more levels we still set it to the next level index.

13:09.830 --> 13:17.030
So we need to solve this somehow and we're going to go to this load next scene.

13:17.590 --> 13:19.630
And over here we have this info.

13:22.510 --> 13:24.760
Uh, we could use this.

13:24.760 --> 13:25.630
Really?

13:29.850 --> 13:31.860
But I want to have separate methods.

13:31.860 --> 13:35.790
I don't want to hide one method inside of another method.

13:35.790 --> 13:39.420
That's not what I want to do, but also I don't want to have duplicated functionality.

13:40.050 --> 13:40.710
Okay.

13:40.980 --> 13:42.360
Very simple to solve.

13:42.390 --> 13:44.490
Let's make this private bool.

13:44.490 --> 13:51.390
No more levels and this will just do this check for us.

13:52.430 --> 13:55.100
And it will return no more levels.

13:55.100 --> 13:57.320
And now we can use a method here.

13:57.320 --> 13:58.640
No more levels.

14:00.730 --> 14:01.690
And.

14:03.120 --> 14:04.200
Over here I'm going to do.

14:04.200 --> 14:09.150
If no more levels equals to false, then we can save this info.

14:09.480 --> 14:10.260
Perfect.

14:10.260 --> 14:15.300
And let's take these to like three lines of code.

14:15.300 --> 14:16.740
And let's extract method.

14:16.740 --> 14:20.970
And I'm going to do save level progression.

14:21.670 --> 14:24.610
Yeah, so it's a bit cleaner and organized.

14:24.640 --> 14:26.920
All right, I think I like it.

14:29.280 --> 14:35.070
Uh, let me show you everything so you can check it and compare it with yours.

14:36.790 --> 14:37.900
Like that.

14:40.890 --> 14:42.870
Oh my God, I cannot fit it into the screen.

14:44.110 --> 14:44.950
Like that.

14:45.950 --> 14:46.640
Yeah.

14:46.640 --> 14:48.470
Now I'm going to save everything.

14:48.470 --> 14:55.040
I'm going to clear the savings in the edit, and I'm going to try to run again and see if it works fine.

14:55.040 --> 14:58.370
Clear all Playerprefs go to play mode.

15:00.540 --> 15:03.900
Gonna start new game level one and let's just see it.

15:09.380 --> 15:10.430
Now we have end screen.

15:10.430 --> 15:14.060
I'm going to skip it and I'm going to click continue.

15:14.060 --> 15:16.670
And it loads me to the last level.

15:16.670 --> 15:17.690
All right.

15:18.170 --> 15:20.780
So this should be good I think logically it's okay.

15:20.780 --> 15:22.460
Like you continue into the last level.

15:22.460 --> 15:24.650
This is how it happens in other games I played.

15:24.650 --> 15:26.570
So I'm going to keep it like that I like it.

15:27.550 --> 15:29.560
Well, that's good for now.

15:29.560 --> 15:30.910
Let's go to the next video.
