In this lecture, we're going to have a little bit of a play around with prefabs.
Now, prefabs are a really important concept.
We haven't got to them yet because we haven't really needed them, but now we're going to start creating
content for our game.
It's going to be very important that we know how prefabs work.
So what are prefabs?
Prefabs are game objects which we've turned into reusable templates and the original template is called
the prefab.
And the copies we add into our scene are called instances, and we'll play around with both of those
in a moment.
And turning a game object into a prefab means a lot of things.
But one important thing it means is we can easily load that prefab into any scene in our game and in
this particular section of the course we're going to be loading from one scene to the next.
So it's important that we are able to persist certain things so we can have a player that goes from
scene one to scene two to scene three, etc., and we need a prefab to be able to do that.
The challenge throughout this lecture is very simple.
We won't be saving our work in this lecture, so be sure to have fun experimenting with prefabs as I'm
explaining them.
That's the challenge.
Play around with them.
If you've already seen prefabs before, it's cool.
This should be a pretty easy process, but if you haven't seen them before, make sure you're comfortable
that you understand.
Oh, that's how prefabs work as I'm experimenting and showing you what's going on.
So just a reminder, we're not going to be saving our work here.
It's all just to get a concept.
In the next lecture, we'll start actually prefab ing things in our game.
So over in the hierarchy, let's right click and create ourselves something.
It doesn't really matter what will make a 2D object sprites and we'll do a circle behind all of our
content.
So I'm going to drag it over to the left over here.
Like I said, we're not saving anything, so it's fine for us just to play around with things.
We've got ourselves a white circle now.
Up until now, we haven't needed a lot of white circles or things like white circles.
So all we've been doing is control D to duplicate.
Now we've got a second white circle.
Maybe one more.
Okay, then we've got three circles.
That's all I wanted to do.
Easy.
But what if.
Here we go.
What if I clicked on the first one and said, Actually, I want my circles to be bigger and green so
I go and change the colour to green.
That looks nice and a little bit bigger.
So I'm going to scale it up, click on the R tool, scale it up.
Great.
Oh, but now I need my other one.
So what can I do?
Well, I could delete these.
Delete, delete and then click on this again.
Duplicate W move it over.
Duplicate again.
There we go.
Okay, it's all good, but oh, that wasn't quite in the right position.
It was before.
Oh, that's a pain.
Okay, well, if I made a change, then I could go and put that information back onto here.
So if I change this one to say red, then I could go and click on this one and make it red or orange.
I guess that turned out to be as well, and so on.
You get the point.
It's a real pain, but that's cool.
It's what we've needed to do up until now.
So let's talk about prefabs.
We'll take one of these circles.
I'll take the original one, I think.
There we go.
Circle.
Now, to turn this into a prefab is really easy.
All I need to do is grab the circle from my hierarchy, drag it down into my project folder anywhere
we like.
And there we go.
This is now officially a prefab.
That process of dragging it in there made it prefab.
You can see now it's blue and it's changed.
The icon that now is clearly a prefab or it's a prefab game object.
This is the prefab down here and you can see it's circle dot prefab.
This has created a brand new thing that is saved on our hard drive.
So it's just a right click show, an explorer and see in here.
We've got a whole bunch of things.
We've got to have folders, animation, scene, sprite, etc. and we've got this circled prefab.
So that is now something that lives on our hard drive.
If I was to delete it from here, it would delete from there everything else we put into our scene.
So if I go to scenes and look at that, you can see we've got our sample scene.
If I was to go and change something in here.
So if I was just to add another circle or maybe a, let's see a square and just save it without making
it a prefab, then the place that is being saved is within our scene, our sample scene that we've got
at the moment.
So all of this information is saved in there, which is okay, but not as good.
We want to be saving our important objects in a separate place so we can use it in many places.
We can use it from scene to scene.
I guess that's just the fundamental structure.
I'm going to get rid of the square fundamental structure of saving it as a prefab.
Now what can I do?
I go, go, drag, drag, drag, boom.
I've got a whole bunch of circles in our scene and you can see it drops them into the hierarchy.
They're all blue.
These are all instances.
So as I mentioned before, these are instances of the prefab.
The original prefab is not that one there.
That is now an instance itself.
It's the original instance, if you will.
It's the game object that we turn into a prefab.
But the actual prefab is just here sitting down in the folder, saved as a prefab on our hard drive.
This is just an instance of that prefab.
Now say we want to make a change.
So I get there, I say, Oh, orange.
What was I thinking?
What are you thinking, Ricky?
Crazy.
No one wants orange nowadays in the video games, they want yellow.
I changed that one to yellow.
It doesn't matter which of these instances we change.
I change circle to group these down here so they're together or else it's going to freak me out a little
bit.
There we go.
They're all nice and together.
And put that down there just so my OCD is all fine.
I'm going to have four circles and one of them.
Let's see this one here.
Click on it.
Circle two is yellow.
What I can do to have all of my circles now become yellow is up in the top right hand corner on the
inspector you see overrides.
That's part of the prefab.
It says prefab open, prefab select and then overrides overrides.
You can see that there's one change we've made which is part of the Sprite renderer.
The Sprite renderer is where we change the colour.
If I click apply all block now all of my instances are yellow.
It applied back to the prefab and if it's on the prefab then the change propagates out into the instances.
Now what if I clicked on this and change the scale a little bit?
So hit ah and scale it up, okay.
And then go to overrides and apply all they all change on the scale as well.
There's another way I could do this is by working directly on the prefab.
So if I double click on the prefab in my folder, that opens up the fresh prefab setting all on its
own, nothing else to distract us.
And if I was to make a change within here, say, changed it to green.
Now you can see the prefab has been changed to green up in the top left.
A little bit hard to find this one, but in the hierarchy there's a little Chevron Arrow thing next
to where it says Circle.
If you click on that, it'll take us back to the scene.
View.
We're no longer editing within inside the prefab and you can see it's made the change.
I don't have to go into that view.
I could click on the prefab circle and then change the colour to a blue colour and you can see that's
going to change it all on the fly.
I like this because then I can see other instances in our seen a change as well.
At the time that I'm changing the prefab one last place that we can click into the prefab, you see
this little arrow thing next to my instance.
If I click on that arrow, I'm now editing the prefab, I'm adding editing this guy down in here.
And any changes I make to this particular instance is actually this particular prefab.
So once again, if I go and change the colour to something else, it propagates.
But this is a nice way of looking at it because I can see them all within context.
If I double click on here onto the prefab, it's not necessarily going to show me all of those in context.
I click back out on here, I'm back into the scene and you can tell if you're in the scene or in the
prefab.
I'll just click back into the prefab by what you see over here in the hierarchy.
I don't see anything else, but what I see up at the top here is circle.
And then if I click back, then you can see now it's showing me I'm on the sample scene.
It doesn't have that circle on there as it did before.
Now, one of the important thing I want to show you here is so I go and change one of my instances.
Circle three, let's go and change circle three to I don't know, we'll make it purple.
I haven't had that yet.
Purple.
That's great.
And I want that to stay purple.
But oh you know what?
I want all of my other instances to be a different colour.
So now if I go and click on the prefab and change the other instances to more of a red, they changed,
but the purple one didn't.
Why is that?
Well, if you've made a change to the instance, that's different to the prefab.
When you go and make a change to the prefab, it doesn't override that change that you've just made.
So I could change the scale, I could change colour.
I don't have a lot of other things on this particular object at the moment, but any of the components,
this same logic applies to that.
By making these changes I've gone and change the scale.
And from this point on, if I go and change anything on the prefab and the X scale, it's not going
to change the X scale.
On my instance that I went and made a modification on that scale.
However, if I was to go and change the Y scale up like that, they all change on the Y scale because
I hadn't gone and made a change to the instance.
Okay, hopefully that makes sense.
If we change something on the instance then making a change on that same particular thing on the prefab
is not going to change the instance or roll out and it's a lot easier to see on the light theme than
it is on the dark theme.
But any time you've changed something on the instance that's different to the prefab, it bolds the
particular element.
So here colour is bolded.
I know it's hard to see and our x value is bolded as well.
And if I look at and say, well, I really do believe that the colour is the best colour, but I don't
actually want to change the scale.
Then I can go up to overrides and choose a particular thing.
What was it?
Yeah, the sprite renderer.
That's the one I want to apply.
That's what the prefab has.
That's what I've got on my instance.
And I can click, apply and apply that and all of them will be purple.
But we keep the scaling that they've all got on the particular objects.
And the other thing to mention in here is that we're dragging these in.
This is called instantiating our object or instantiating our prefab.
So here's a prefab, I'm instantiating it.
That creates an instance we can also instantiate in our code.
And that's something we'll do later on in this section.
We'll, we'll say, write some code that says Instantiate a thing and it appears on our screen.
So that's another good use for prefab.
So just to summarize then, prefab is a great it allows us to have a lot of objects in our scene, but
it's all coming from one object.
So it's an efficient way of creating things.
It allows us to make changes to one game object on a prefab, for example, change the colour and then
that will propagate to all the other things in our scene.
That's an instance of that prefab.
It allows us to load things from scene to scene.
If we're on the next level or the next scene, we can easily add new circles to that next scene without
having to create it from scratch.
We've got this template that we can work from, so prefabs are really.
Fundamental aspect of unity, very important.
And from this point on, we'll be using prefabs a lot more freely.
So good stuff.
In this lecture, I hope you had a good chance to experiment, and I'll see you in the next lecture.