1
00:00:03,130 --> 00:00:06,190
In this lecture, we're going to have a little bit of a play around with prefabs.

2
00:00:06,190 --> 00:00:08,860
Now, prefabs are a really important concept.

3
00:00:08,860 --> 00:00:13,450
We haven't got to them yet because we haven't really needed them, but now we're going to start creating

4
00:00:13,450 --> 00:00:14,800
content for our game.

5
00:00:14,800 --> 00:00:17,620
It's going to be very important that we know how prefabs work.

6
00:00:17,620 --> 00:00:19,180
So what are prefabs?

7
00:00:19,180 --> 00:00:25,420
Prefabs are game objects which we've turned into reusable templates and the original template is called

8
00:00:25,420 --> 00:00:26,260
the prefab.

9
00:00:26,260 --> 00:00:31,240
And the copies we add into our scene are called instances, and we'll play around with both of those

10
00:00:31,240 --> 00:00:31,870
in a moment.

11
00:00:31,870 --> 00:00:35,200
And turning a game object into a prefab means a lot of things.

12
00:00:35,200 --> 00:00:42,100
But one important thing it means is we can easily load that prefab into any scene in our game and in

13
00:00:42,100 --> 00:00:46,360
this particular section of the course we're going to be loading from one scene to the next.

14
00:00:46,360 --> 00:00:52,480
So it's important that we are able to persist certain things so we can have a player that goes from

15
00:00:52,480 --> 00:00:56,560
scene one to scene two to scene three, etc., and we need a prefab to be able to do that.

16
00:00:56,560 --> 00:00:59,200
The challenge throughout this lecture is very simple.

17
00:00:59,200 --> 00:01:04,900
We won't be saving our work in this lecture, so be sure to have fun experimenting with prefabs as I'm

18
00:01:04,900 --> 00:01:05,710
explaining them.

19
00:01:05,710 --> 00:01:06,520
That's the challenge.

20
00:01:06,520 --> 00:01:07,300
Play around with them.

21
00:01:07,300 --> 00:01:09,520
If you've already seen prefabs before, it's cool.

22
00:01:09,520 --> 00:01:13,300
This should be a pretty easy process, but if you haven't seen them before, make sure you're comfortable

23
00:01:13,300 --> 00:01:14,110
that you understand.

24
00:01:14,110 --> 00:01:18,340
Oh, that's how prefabs work as I'm experimenting and showing you what's going on.

25
00:01:18,340 --> 00:01:20,680
So just a reminder, we're not going to be saving our work here.

26
00:01:20,680 --> 00:01:22,270
It's all just to get a concept.

27
00:01:22,270 --> 00:01:25,540
In the next lecture, we'll start actually prefab ing things in our game.

28
00:01:25,540 --> 00:01:29,020
So over in the hierarchy, let's right click and create ourselves something.

29
00:01:29,020 --> 00:01:35,350
It doesn't really matter what will make a 2D object sprites and we'll do a circle behind all of our

30
00:01:35,350 --> 00:01:35,740
content.

31
00:01:35,740 --> 00:01:37,570
So I'm going to drag it over to the left over here.

32
00:01:37,570 --> 00:01:41,290
Like I said, we're not saving anything, so it's fine for us just to play around with things.

33
00:01:41,290 --> 00:01:43,000
We've got ourselves a white circle now.

34
00:01:43,000 --> 00:01:47,650
Up until now, we haven't needed a lot of white circles or things like white circles.

35
00:01:47,650 --> 00:01:50,440
So all we've been doing is control D to duplicate.

36
00:01:50,440 --> 00:01:51,700
Now we've got a second white circle.

37
00:01:51,700 --> 00:01:52,510
Maybe one more.

38
00:01:52,540 --> 00:01:53,800
Okay, then we've got three circles.

39
00:01:53,800 --> 00:01:54,820
That's all I wanted to do.

40
00:01:54,820 --> 00:01:55,390
Easy.

41
00:01:55,390 --> 00:01:56,500
But what if.

42
00:01:56,500 --> 00:01:57,040
Here we go.

43
00:01:57,040 --> 00:02:02,590
What if I clicked on the first one and said, Actually, I want my circles to be bigger and green so

44
00:02:02,590 --> 00:02:04,120
I go and change the colour to green.

45
00:02:04,120 --> 00:02:05,440
That looks nice and a little bit bigger.

46
00:02:05,440 --> 00:02:09,460
So I'm going to scale it up, click on the R tool, scale it up.

47
00:02:09,460 --> 00:02:09,910
Great.

48
00:02:09,910 --> 00:02:11,260
Oh, but now I need my other one.

49
00:02:11,260 --> 00:02:12,190
So what can I do?

50
00:02:12,190 --> 00:02:14,650
Well, I could delete these.

51
00:02:14,650 --> 00:02:17,290
Delete, delete and then click on this again.

52
00:02:17,290 --> 00:02:19,180
Duplicate W move it over.

53
00:02:19,210 --> 00:02:20,170
Duplicate again.

54
00:02:20,170 --> 00:02:20,620
There we go.

55
00:02:20,620 --> 00:02:23,080
Okay, it's all good, but oh, that wasn't quite in the right position.

56
00:02:23,080 --> 00:02:23,800
It was before.

57
00:02:23,800 --> 00:02:24,550
Oh, that's a pain.

58
00:02:24,550 --> 00:02:28,870
Okay, well, if I made a change, then I could go and put that information back onto here.

59
00:02:28,870 --> 00:02:34,570
So if I change this one to say red, then I could go and click on this one and make it red or orange.

60
00:02:34,570 --> 00:02:36,910
I guess that turned out to be as well, and so on.

61
00:02:36,910 --> 00:02:37,630
You get the point.

62
00:02:37,630 --> 00:02:38,950
It's a real pain, but that's cool.

63
00:02:38,950 --> 00:02:40,750
It's what we've needed to do up until now.

64
00:02:40,750 --> 00:02:42,460
So let's talk about prefabs.

65
00:02:42,460 --> 00:02:43,720
We'll take one of these circles.

66
00:02:43,720 --> 00:02:45,580
I'll take the original one, I think.

67
00:02:45,580 --> 00:02:46,030
There we go.

68
00:02:46,030 --> 00:02:46,600
Circle.

69
00:02:46,600 --> 00:02:49,300
Now, to turn this into a prefab is really easy.

70
00:02:49,300 --> 00:02:55,390
All I need to do is grab the circle from my hierarchy, drag it down into my project folder anywhere

71
00:02:55,390 --> 00:02:56,020
we like.

72
00:02:56,020 --> 00:02:56,800
And there we go.

73
00:02:56,800 --> 00:02:59,140
This is now officially a prefab.

74
00:02:59,140 --> 00:03:01,420
That process of dragging it in there made it prefab.

75
00:03:01,420 --> 00:03:03,640
You can see now it's blue and it's changed.

76
00:03:03,640 --> 00:03:09,160
The icon that now is clearly a prefab or it's a prefab game object.

77
00:03:09,160 --> 00:03:13,480
This is the prefab down here and you can see it's circle dot prefab.

78
00:03:13,480 --> 00:03:16,600
This has created a brand new thing that is saved on our hard drive.

79
00:03:16,600 --> 00:03:19,870
So it's just a right click show, an explorer and see in here.

80
00:03:19,870 --> 00:03:20,640
We've got a whole bunch of things.

81
00:03:20,650 --> 00:03:24,460
We've got to have folders, animation, scene, sprite, etc. and we've got this circled prefab.

82
00:03:24,460 --> 00:03:27,820
So that is now something that lives on our hard drive.

83
00:03:27,820 --> 00:03:32,380
If I was to delete it from here, it would delete from there everything else we put into our scene.

84
00:03:32,380 --> 00:03:35,440
So if I go to scenes and look at that, you can see we've got our sample scene.

85
00:03:35,440 --> 00:03:37,330
If I was to go and change something in here.

86
00:03:37,330 --> 00:03:44,410
So if I was just to add another circle or maybe a, let's see a square and just save it without making

87
00:03:44,410 --> 00:03:49,510
it a prefab, then the place that is being saved is within our scene, our sample scene that we've got

88
00:03:49,510 --> 00:03:49,930
at the moment.

89
00:03:49,930 --> 00:03:54,040
So all of this information is saved in there, which is okay, but not as good.

90
00:03:54,040 --> 00:03:59,170
We want to be saving our important objects in a separate place so we can use it in many places.

91
00:03:59,170 --> 00:04:00,940
We can use it from scene to scene.

92
00:04:00,940 --> 00:04:02,650
I guess that's just the fundamental structure.

93
00:04:02,650 --> 00:04:06,340
I'm going to get rid of the square fundamental structure of saving it as a prefab.

94
00:04:06,340 --> 00:04:07,210
Now what can I do?

95
00:04:07,210 --> 00:04:09,780
I go, go, drag, drag, drag, boom.

96
00:04:09,820 --> 00:04:14,560
I've got a whole bunch of circles in our scene and you can see it drops them into the hierarchy.

97
00:04:14,560 --> 00:04:15,550
They're all blue.

98
00:04:15,640 --> 00:04:17,680
These are all instances.

99
00:04:17,680 --> 00:04:20,230
So as I mentioned before, these are instances of the prefab.

100
00:04:20,260 --> 00:04:22,690
The original prefab is not that one there.

101
00:04:22,690 --> 00:04:25,030
That is now an instance itself.

102
00:04:25,030 --> 00:04:26,980
It's the original instance, if you will.

103
00:04:26,980 --> 00:04:29,290
It's the game object that we turn into a prefab.

104
00:04:29,290 --> 00:04:35,140
But the actual prefab is just here sitting down in the folder, saved as a prefab on our hard drive.

105
00:04:35,170 --> 00:04:37,870
This is just an instance of that prefab.

106
00:04:37,870 --> 00:04:39,400
Now say we want to make a change.

107
00:04:39,400 --> 00:04:41,050
So I get there, I say, Oh, orange.

108
00:04:41,050 --> 00:04:41,950
What was I thinking?

109
00:04:41,950 --> 00:04:42,870
What are you thinking, Ricky?

110
00:04:42,880 --> 00:04:43,330
Crazy.

111
00:04:43,330 --> 00:04:46,000
No one wants orange nowadays in the video games, they want yellow.

112
00:04:46,030 --> 00:04:47,350
I changed that one to yellow.

113
00:04:47,380 --> 00:04:50,590
It doesn't matter which of these instances we change.

114
00:04:50,590 --> 00:04:55,660
I change circle to group these down here so they're together or else it's going to freak me out a little

115
00:04:55,660 --> 00:04:55,870
bit.

116
00:04:55,870 --> 00:04:56,230
There we go.

117
00:04:56,260 --> 00:04:57,430
They're all nice and together.

118
00:04:57,430 --> 00:05:00,250
And put that down there just so my OCD is all fine.

119
00:05:00,250 --> 00:05:02,380
I'm going to have four circles and one of them.

120
00:05:02,460 --> 00:05:03,330
Let's see this one here.

121
00:05:03,330 --> 00:05:03,730
Click on it.

122
00:05:03,750 --> 00:05:05,310
Circle two is yellow.

123
00:05:05,340 --> 00:05:11,220
What I can do to have all of my circles now become yellow is up in the top right hand corner on the

124
00:05:11,220 --> 00:05:12,720
inspector you see overrides.

125
00:05:12,720 --> 00:05:13,700
That's part of the prefab.

126
00:05:13,710 --> 00:05:17,070
It says prefab open, prefab select and then overrides overrides.

127
00:05:17,070 --> 00:05:20,490
You can see that there's one change we've made which is part of the Sprite renderer.

128
00:05:20,490 --> 00:05:22,260
The Sprite renderer is where we change the colour.

129
00:05:22,260 --> 00:05:27,030
If I click apply all block now all of my instances are yellow.

130
00:05:27,030 --> 00:05:34,110
It applied back to the prefab and if it's on the prefab then the change propagates out into the instances.

131
00:05:34,320 --> 00:05:37,560
Now what if I clicked on this and change the scale a little bit?

132
00:05:37,560 --> 00:05:40,080
So hit ah and scale it up, okay.

133
00:05:40,080 --> 00:05:45,180
And then go to overrides and apply all they all change on the scale as well.

134
00:05:45,180 --> 00:05:49,170
There's another way I could do this is by working directly on the prefab.

135
00:05:49,170 --> 00:05:55,290
So if I double click on the prefab in my folder, that opens up the fresh prefab setting all on its

136
00:05:55,290 --> 00:05:57,420
own, nothing else to distract us.

137
00:05:57,420 --> 00:06:00,600
And if I was to make a change within here, say, changed it to green.

138
00:06:00,600 --> 00:06:04,020
Now you can see the prefab has been changed to green up in the top left.

139
00:06:04,020 --> 00:06:08,010
A little bit hard to find this one, but in the hierarchy there's a little Chevron Arrow thing next

140
00:06:08,010 --> 00:06:08,850
to where it says Circle.

141
00:06:08,850 --> 00:06:11,520
If you click on that, it'll take us back to the scene.

142
00:06:11,520 --> 00:06:11,850
View.

143
00:06:11,850 --> 00:06:15,660
We're no longer editing within inside the prefab and you can see it's made the change.

144
00:06:15,660 --> 00:06:17,070
I don't have to go into that view.

145
00:06:17,070 --> 00:06:22,620
I could click on the prefab circle and then change the colour to a blue colour and you can see that's

146
00:06:22,620 --> 00:06:24,030
going to change it all on the fly.

147
00:06:24,060 --> 00:06:28,560
I like this because then I can see other instances in our seen a change as well.

148
00:06:28,560 --> 00:06:33,840
At the time that I'm changing the prefab one last place that we can click into the prefab, you see

149
00:06:33,840 --> 00:06:36,510
this little arrow thing next to my instance.

150
00:06:36,510 --> 00:06:42,750
If I click on that arrow, I'm now editing the prefab, I'm adding editing this guy down in here.

151
00:06:42,750 --> 00:06:47,700
And any changes I make to this particular instance is actually this particular prefab.

152
00:06:47,700 --> 00:06:50,940
So once again, if I go and change the colour to something else, it propagates.

153
00:06:50,940 --> 00:06:54,540
But this is a nice way of looking at it because I can see them all within context.

154
00:06:54,540 --> 00:07:00,690
If I double click on here onto the prefab, it's not necessarily going to show me all of those in context.

155
00:07:00,690 --> 00:07:04,530
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

156
00:07:04,530 --> 00:07:05,010
prefab.

157
00:07:05,010 --> 00:07:09,000
I'll just click back into the prefab by what you see over here in the hierarchy.

158
00:07:09,000 --> 00:07:12,540
I don't see anything else, but what I see up at the top here is circle.

159
00:07:12,540 --> 00:07:16,110
And then if I click back, then you can see now it's showing me I'm on the sample scene.

160
00:07:16,110 --> 00:07:18,450
It doesn't have that circle on there as it did before.

161
00:07:19,020 --> 00:07:22,560
Now, one of the important thing I want to show you here is so I go and change one of my instances.

162
00:07:22,560 --> 00:07:26,830
Circle three, let's go and change circle three to I don't know, we'll make it purple.

163
00:07:26,880 --> 00:07:27,600
I haven't had that yet.

164
00:07:27,600 --> 00:07:27,990
Purple.

165
00:07:27,990 --> 00:07:28,470
That's great.

166
00:07:28,470 --> 00:07:29,970
And I want that to stay purple.

167
00:07:29,970 --> 00:07:30,960
But oh you know what?

168
00:07:30,960 --> 00:07:33,660
I want all of my other instances to be a different colour.

169
00:07:33,660 --> 00:07:40,290
So now if I go and click on the prefab and change the other instances to more of a red, they changed,

170
00:07:40,290 --> 00:07:41,700
but the purple one didn't.

171
00:07:41,700 --> 00:07:42,420
Why is that?

172
00:07:42,420 --> 00:07:47,220
Well, if you've made a change to the instance, that's different to the prefab.

173
00:07:47,220 --> 00:07:51,870
When you go and make a change to the prefab, it doesn't override that change that you've just made.

174
00:07:51,870 --> 00:07:54,870
So I could change the scale, I could change colour.

175
00:07:54,870 --> 00:07:58,830
I don't have a lot of other things on this particular object at the moment, but any of the components,

176
00:07:58,830 --> 00:08:00,630
this same logic applies to that.

177
00:08:00,630 --> 00:08:03,900
By making these changes I've gone and change the scale.

178
00:08:03,900 --> 00:08:09,000
And from this point on, if I go and change anything on the prefab and the X scale, it's not going

179
00:08:09,000 --> 00:08:10,620
to change the X scale.

180
00:08:10,620 --> 00:08:13,680
On my instance that I went and made a modification on that scale.

181
00:08:13,680 --> 00:08:19,350
However, if I was to go and change the Y scale up like that, they all change on the Y scale because

182
00:08:19,350 --> 00:08:21,840
I hadn't gone and made a change to the instance.

183
00:08:22,200 --> 00:08:23,520
Okay, hopefully that makes sense.

184
00:08:23,520 --> 00:08:29,190
If we change something on the instance then making a change on that same particular thing on the prefab

185
00:08:29,190 --> 00:08:33,900
is not going to change the instance or roll out and it's a lot easier to see on the light theme than

186
00:08:33,900 --> 00:08:34,860
it is on the dark theme.

187
00:08:34,860 --> 00:08:39,659
But any time you've changed something on the instance that's different to the prefab, it bolds the

188
00:08:39,659 --> 00:08:40,559
particular element.

189
00:08:40,559 --> 00:08:41,880
So here colour is bolded.

190
00:08:41,880 --> 00:08:45,240
I know it's hard to see and our x value is bolded as well.

191
00:08:45,240 --> 00:08:49,350
And if I look at and say, well, I really do believe that the colour is the best colour, but I don't

192
00:08:49,350 --> 00:08:51,150
actually want to change the scale.

193
00:08:51,150 --> 00:08:54,510
Then I can go up to overrides and choose a particular thing.

194
00:08:54,510 --> 00:08:55,020
What was it?

195
00:08:55,020 --> 00:08:56,010
Yeah, the sprite renderer.

196
00:08:56,040 --> 00:08:57,690
That's the one I want to apply.

197
00:08:57,720 --> 00:08:59,460
That's what the prefab has.

198
00:08:59,460 --> 00:09:01,050
That's what I've got on my instance.

199
00:09:01,050 --> 00:09:05,100
And I can click, apply and apply that and all of them will be purple.

200
00:09:05,100 --> 00:09:08,730
But we keep the scaling that they've all got on the particular objects.

201
00:09:08,730 --> 00:09:11,130
And the other thing to mention in here is that we're dragging these in.

202
00:09:11,160 --> 00:09:15,960
This is called instantiating our object or instantiating our prefab.

203
00:09:15,960 --> 00:09:17,790
So here's a prefab, I'm instantiating it.

204
00:09:17,790 --> 00:09:21,270
That creates an instance we can also instantiate in our code.

205
00:09:21,270 --> 00:09:22,980
And that's something we'll do later on in this section.

206
00:09:22,980 --> 00:09:27,480
We'll, we'll say, write some code that says Instantiate a thing and it appears on our screen.

207
00:09:27,480 --> 00:09:29,730
So that's another good use for prefab.

208
00:09:29,730 --> 00:09:35,130
So just to summarize then, prefab is a great it allows us to have a lot of objects in our scene, but

209
00:09:35,130 --> 00:09:36,780
it's all coming from one object.

210
00:09:36,780 --> 00:09:38,760
So it's an efficient way of creating things.

211
00:09:38,760 --> 00:09:44,010
It allows us to make changes to one game object on a prefab, for example, change the colour and then

212
00:09:44,010 --> 00:09:47,040
that will propagate to all the other things in our scene.

213
00:09:47,040 --> 00:09:49,170
That's an instance of that prefab.

214
00:09:49,170 --> 00:09:51,810
It allows us to load things from scene to scene.

215
00:09:51,810 --> 00:09:57,540
If we're on the next level or the next scene, we can easily add new circles to that next scene without

216
00:09:57,540 --> 00:09:58,860
having to create it from scratch.

217
00:09:58,860 --> 00:10:02,220
We've got this template that we can work from, so prefabs are really.

218
00:10:02,330 --> 00:10:05,000
Fundamental aspect of unity, very important.

219
00:10:05,000 --> 00:10:08,360
And from this point on, we'll be using prefabs a lot more freely.

220
00:10:08,360 --> 00:10:09,140
So good stuff.

221
00:10:09,140 --> 00:10:13,070
In this lecture, I hope you had a good chance to experiment, and I'll see you in the next lecture.

