Hello guys.
Welcome to the next video.
So in this one I'm going to delete this game object.
And also I want to delete this Playerprefs example.
Also let's clean this up a bit.
Let's take these two.
Drag them into scripts folder.
In the scripts folder.
I think it's time to make a UI folder.
UI.
And we can take it all here.
Cool.
Now we have a goal of saving information about progression of the levels.
Every time we finish the level, we want to unlock next level.
So let's go to the game manager.
And first of all, I want to make a variable because we're going to use it a couple of times in different
methods.
Private int next level index.
Now let's go here and say that next level index is equals to current level index plus one.
Nice.
Now let's go down to the methods we created in the last video over here.
First of all we do need a local variable anymore.
We have it saved globally.
And also I think we need to arrange this a bit because level finished method will do a couple of things.
Not only load the next scene, but also it's going to save information and so on.
So better we make something like private void load next scene.
All right.
And we can take this code from here, paste it in here just like that.
And then in the level finished we're going to call load next scene.
Actually I could just do extract method anyway.
Also I want to move this above over here and maybe lock this because we know how they work.
We don't need to do anything here.
Now when we finish in the level we load in the next level, and at the same time we could save information
about next level being unlocked.
So when you go to level selector, you see that this level is unlocked because you played this before.
This is how we're going to do that.
We're going to say Playerprefs.
Dot set integer.
We're going to type level.
Then plus next level index plus unlocked.
And we're going to give it a value of one.
So this will save the number one which will use as a boolean just like I showed you in the last video.
Now let's go ahead and complete all of the levels so we can see the progression.
And we can use it to create the buttons.
I'm going to go to New Game.
And I'm going to quickly complete the levels.
Two.
No no no.
And one more.
Good.
So now, if I'm not mistaken, I did call this method in every level and I unlocked next level every
time.
Now let's go to Level Selection script.
And over here.
It might be a little bit of overkill, but I think it's better this way because we can, um, we can
see information in the inspector.
So I'm going to make serialized field private, uh, bool array, uh, levels unlocked.
And we need to go and do something like private void load levels, load levels info.
All right.
Now we want to.
Yet.
Levels amount.
I'm going to copy this.
Go here and then I'm going to do.
Uh levels unlocked.
Array equals to new boolean of levels amount.
So we're going to make an array.
And it will be equals to the size of the levels amount we have.
Then I'm going to do for loop for the levels amount.
Actually before we do any check let's make it equals to one over here.
Right.
So we start from the first one and zero one.
We're just going to ignore.
Now let's go and make a boolean a local variable level unlocked equals to Playerprefs.
Get int of level plus I plus unlocked and default value will be zero.
And if this one is equals to one, it means level is unlocked.
And then we're going to do.
If level unlocked, then levels unlocked with the I equals to true.
And also, no matter what the progress was before, we need to be sure we unlocked the first level.
So I'm going to go ahead here and I'm going to do levels unlocked one equals to true.
All right.
Now let's go to start method.
And before we create level buttons let's call this load levels info I'm going to save it.
Let's go back to unity and see the result.
Now back in unity let me select New Game and let me assign level selection over here so I can switch
to level selection.
Let's go to play mode.
I'm going to click New Game.
And on the level selection we can see that.
We can see that all three booleans are unlocked.
Nice.
Now let's go to.
Now let's go to UI level selection.
And over here we want to check if level is unlocked because if it was not unlocked there is no point
of creating a button.
So this is how are we going to do that?
I think it's better to make a private.
Private bool method is level unlocked, just so it's a bit easier to read everything, and this one
going to return me levels unlocked with the I boolean.
Just that sorry, not I um, this should be.
Int level index.
And here we pass level index.
Okay nice.
So now we have this boolean.
And we can go here and do if is level unlocked with a I equals to false then return.
And we're going to interrupt the for loop.
We're not going to create any more buttons.
Now let me go back to unity.
I'm going to go to edit and I'm going to click clear all Playerprefs.
So it's going to clear all of the saved information.
Now we're going to go to play mode.
A new game and we have only level one.
Now let's go further.
Let's complete one of the levels.
So now we have two levels unlocked.
We're going to go to play mode.
New game, and we can see that only two buttons were created, even though we have three levels in the
game.
Okay, perfect.
Now we have a simple goal.
We need to check if player has any progression in the game and if he has a progression.
We need to know what the level he played last and then we need to load that level if he presses the
continue button, and also if there is no progression, we need to hide the continue button itself.
So how it's going to be done, first of all, uh, let's go to uh, Game Manager.
And I'm going to make a new player pref here player pref set int last completed level number.
And here we're going to pass next level index I guess.
Or maybe let's just save it as next level number because we press continue and we're supposed to play
the next level.
So I guess let's type continue level number.
Yeah I guess it serves the purpose very well.
Now let's save this and let's go to main menu.
And we're gonna make.
We're going to make a reference to the button.
Let's do serialized field, private game object, and a new button.
Nice.
Now we're going to go ahead and, um.
Let me think a bit.
We need some sort of a method.
Right.
Pull.
As level progression.
And this will return me a boolean of bool as level progression equals two.
We need to take that line of code from a layer manager.
I'm going to copy this so I don't make a mistake with the name.
And over here I'm going to paste this in.
But I'm going to change it from set to get int.
And default value will be zero.
And this will be true if it is bigger than zero.
Simple as that.
And then over here we're going to just return his level progression.
Nice.
Now we need to may now we want to use this reference to enable button in the start.
Now we're going to use this boolean to enable continue button.
In the start we're going to do this.
If has level progression then continue button set active.
True.
Simple as that.
Now let's go ahead and make this button.
We need to go back to unity I'm going to take new game I'm going to duplicate this and move it up.
Let's change it to continue.
And here we're going to do continue.
Probably we should do a different layout for buttons.
But we're also going to have settings button later on.
For now I'll place it here.
It's okay.
Now for this button we need the method that will load the last level in progression.
So probably it's yeah, we can just make a method here.
Why not.
Let's make a method.
Uh public void continue game and this will just do scene manager load scene with uh all we need the
index.
This will take long.
Okay.
Let's do curly brackets here.
And we're going to do int level two.
Load equals to I'm going to get this one.
Okay.
And over here we're going to do load scene level underscore level to load underscore plus level to load
like that.
Okay.
It's a bit confusing I guess.
I'll try to find a way to clean this up, but as of now, this should work.
And let's drag Continue Field over here.
And then on the continue button we're gonna switch to.
UI.
Continue game and just.
That's it.
Now let's disable this button by default.
And I'm going to do file.
Now edit clear all playerprefs.
So if I'm not mistaken, now we're supposed to go to play mode.
And there are going to be only new game.
Yes.
Okay.
Now I'm going to go to New game and I'm going to complete one of the levels.
So I've completed the level.
This is all nice.
Now let's say I'm on this level, and it's a bit difficult for me and I want to take some rest.
So I decided to stop playing.
I go out of the game, then I'm like, oh, I want to play again.
So let's go to play mode.
And here now we have a continue button.
Cool.
Now we can click this button.
And if I'm not mistaken we're going to go to the second level.
Continue.
Yes okay.
Now I'm going to go further.
Complete another level and then I'm like, oh no, I need to go to eat some food.
So I'm going to exit the play mode.
Enter play mode.
Now let's say I want to start new game.
I can choose a level one, two, three because all of them are unlocked, or I can just click continue
and I'm going to play from the last level I played.
Perfect.
Love it man.
Love it so much.
And also it's appearing there.
We can see it's, uh, being enabled, but with a fade image.
It's not going to be like that if you make it dark here.
And go to play mode, then it's going to be.
Just fine.
Okay.
Hello, guys.
Before we finish this, let me show you a bug we have.
So if you complete all of the levels and then you click continue button, it's going to give you an
error.
It's very easy to fix.
You see I'm clicking continue.
And we see level four could not be loaded because it has like we don't have this scene in the build.
So this happens because we load scene with the get int continue level number and we set this continue
level number integer.
When we finish the game in the Game manager over here, this is what we do.
And we set it to next level index no matter what.
But if we don't have any more levels we still set it to the next level index.
So we need to solve this somehow and we're going to go to this load next scene.
And over here we have this info.
Uh, we could use this.
Really?
But I want to have separate methods.
I don't want to hide one method inside of another method.
That's not what I want to do, but also I don't want to have duplicated functionality.
Okay.
Very simple to solve.
Let's make this private bool.
No more levels and this will just do this check for us.
And it will return no more levels.
And now we can use a method here.
No more levels.
And.
Over here I'm going to do.
If no more levels equals to false, then we can save this info.
Perfect.
And let's take these to like three lines of code.
And let's extract method.
And I'm going to do save level progression.
Yeah, so it's a bit cleaner and organized.
All right, I think I like it.
Uh, let me show you everything so you can check it and compare it with yours.
Like that.
Oh my God, I cannot fit it into the screen.
Like that.
Yeah.
Now I'm going to save everything.
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.
Clear all Playerprefs go to play mode.
Gonna start new game level one and let's just see it.
Now we have end screen.
I'm going to skip it and I'm going to click continue.
And it loads me to the last level.
All right.
So this should be good I think logically it's okay.
Like you continue into the last level.
This is how it happens in other games I played.
So I'm going to keep it like that I like it.
Well, that's good for now.
Let's go to the next video.