Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:04,480 --> 00:00:10,060
OK, so let's take a look at a reasonably common situation where we can apply a design pattern that's
2
00:00:10,060 --> 00:00:15,940
quite common to unity and game development in general, which is object pooling, it doesn't only relate
3
00:00:15,940 --> 00:00:21,700
to game development, it can be used in browsers, for example, but we'll get to that in just a second.
4
00:00:21,730 --> 00:00:27,880
So imagine that we have a very simple launcher on a behavior, and this is how it works.
5
00:00:27,880 --> 00:00:31,270
It has a bullet prefab and an update.
6
00:00:31,270 --> 00:00:36,560
If you hold down the space, then it goes ahead and creates a bullet.
7
00:00:36,580 --> 00:00:39,190
So let's go into and see this in action.
8
00:00:39,190 --> 00:00:46,870
We've got an object spawn object here, and you can see the object spawn has sorry, this is the wrong
9
00:00:46,900 --> 00:00:47,520
objects --.
10
00:00:47,530 --> 00:00:49,450
He wants to hide that object spawn.
11
00:00:49,450 --> 00:00:55,420
In fact, what we want here is the object pool example, and in the object pool example, we've got
12
00:00:55,420 --> 00:00:56,170
a launcher.
13
00:00:56,170 --> 00:01:03,010
And in here you can see the launch script has a link to the bullet prefab, the bullet prefab here,
14
00:01:03,400 --> 00:01:10,390
if we go and have a look at it, just has a bullet object as a Sprite renderer, and it has a speed
15
00:01:10,390 --> 00:01:11,690
basically on this bullet.
16
00:01:11,710 --> 00:01:15,580
Now we can go and have a look at the bullet speed and the bullet script just now.
17
00:01:16,240 --> 00:01:21,340
So the bullet script has a very, very simple, configurable vector.
18
00:01:21,340 --> 00:01:26,050
Three speed in updates updates its position using the speed and delta time.
19
00:01:26,800 --> 00:01:29,650
When it becomes invisible, it destroys itself.
20
00:01:29,860 --> 00:01:34,240
So the launch is responsible for instantiating it when we hit the spacebar, and it's responsible for
21
00:01:34,240 --> 00:01:36,220
destroying itself when it becomes invisible.
22
00:01:36,940 --> 00:01:39,040
This is a pretty common paradigm.
23
00:01:39,250 --> 00:01:40,570
You may not necessarily do it this way.
24
00:01:40,570 --> 00:01:42,400
You might do it based on the extent of the screen.
25
00:01:42,700 --> 00:01:46,510
But the effect is that I hit spacebar.
26
00:01:46,840 --> 00:01:51,100
The bullets appear on the screen and they clear themselves up.
27
00:01:51,100 --> 00:01:56,920
So you can see here in the sample scene the bullet clones are getting created and then they're getting
28
00:01:56,920 --> 00:01:57,700
destroyed again.
29
00:01:57,850 --> 00:02:05,740
Now this is a little bit wasteful because as you can see here, we are creating and destroying continuously.
30
00:02:05,770 --> 00:02:07,550
That's not really necessary.
31
00:02:07,570 --> 00:02:13,030
Better would be to recycle these bullets when they go off screen instead of creating a new one.
32
00:02:13,030 --> 00:02:14,080
We just use that bullet.
33
00:02:14,080 --> 00:02:19,960
We move it back to the spawn point at the beginning of the gun or whatever else it is.
34
00:02:20,680 --> 00:02:22,850
Now that would be the smart way.
35
00:02:22,870 --> 00:02:26,080
How do we achieve this while using the object pull pattern?
36
00:02:26,080 --> 00:02:30,370
So the situation we're currently in is we've got a client which is like gun.
37
00:02:30,790 --> 00:02:37,210
It goes ahead and creates a new object over the course of doing its job when a space bar is hit, for
38
00:02:37,210 --> 00:02:37,730
example.
39
00:02:38,170 --> 00:02:42,290
And then at some point later down the line, we decide we don't need that object anymore.
40
00:02:42,310 --> 00:02:43,450
Maybe it's the client doing this.
41
00:02:43,450 --> 00:02:47,140
Maybe it's the object itself doesn't really matter because Hadden destroys the object.
42
00:02:47,500 --> 00:02:52,630
Then later on, we go ahead and create yet another object and do the same process over and over again.
43
00:02:52,630 --> 00:02:59,680
And initialization destruction is costly not just in unity, but in many, many situations.
44
00:02:59,680 --> 00:03:05,740
You have to create a lot of memory, a lot of things, and that is fine when you got a small number
45
00:03:05,740 --> 00:03:06,280
of objects.
46
00:03:06,670 --> 00:03:11,470
The more and more projectiles you have, if you have a particularly projectile heavy game, this might
47
00:03:11,470 --> 00:03:12,700
be more of a problem.
48
00:03:12,970 --> 00:03:17,900
So the solution is to recycle, reduce, reuse and recycle.
49
00:03:17,920 --> 00:03:23,800
So the way we're going to do that is by having some sort of intermediary object in between called an
50
00:03:23,800 --> 00:03:24,490
object pool.
51
00:03:24,520 --> 00:03:31,330
Now what happens is instead of the client creating and destroying objects itself directly, it's going
52
00:03:31,330 --> 00:03:34,660
to ask the object pool to get it an object.
53
00:03:35,290 --> 00:03:42,400
And if the object pool doesn't have any objects, it will go ahead and create one, and then it will
54
00:03:42,400 --> 00:03:44,140
return that object to the client.
55
00:03:45,130 --> 00:03:50,290
Now, if the client has finished with an object or if the object itself has finished with the object,
56
00:03:50,530 --> 00:03:55,000
then it can go ahead and release it back to the object pool in question.
57
00:03:55,630 --> 00:04:00,370
And that's going to go ahead and call something like disable on the object.
58
00:04:00,370 --> 00:04:02,980
It's going to do something to disable that object.
59
00:04:03,670 --> 00:04:08,980
And when the client goes to get again, the object pool has kept that object around.
60
00:04:08,980 --> 00:04:09,640
It's kept it in.
61
00:04:09,640 --> 00:04:16,450
It's deactivated pool and it will go ahead and simply enable the object and return the same object.
62
00:04:16,459 --> 00:04:23,770
So now, instead of creating and destroying objects repeatedly, we're simply doing a much cheaper operation,
63
00:04:23,770 --> 00:04:28,900
disabling and enabling those objects and keeping them around inside of an object pool.
64
00:04:29,560 --> 00:04:33,610
Now, fortunately, in unity, we have a mechanism built in.
65
00:04:33,610 --> 00:04:40,420
Now, basically, as of Unity 2021, we now have object pooling built right into unity, and it's pretty
66
00:04:40,420 --> 00:04:41,560
cool how this works.
67
00:04:41,920 --> 00:04:46,450
You can use this object, pool, object and interface.
68
00:04:46,450 --> 00:04:52,390
They see that I object pool interface, which is implemented by the I object pool to you to create your
69
00:04:52,390 --> 00:04:54,130
own object systems.
70
00:04:54,580 --> 00:04:55,990
So how does this work?
71
00:04:56,020 --> 00:05:01,090
Well, we would create a pool on the client, on whoever's using it.
72
00:05:01,090 --> 00:05:03,730
In our case, it's the launcher and you would have to insert.
73
00:05:03,810 --> 00:05:10,410
And shake that pool from something specific, so this is the not a great example.
74
00:05:10,440 --> 00:05:11,880
I'm going to give you a better example.
75
00:05:12,150 --> 00:05:14,880
Let's go into the code that we've got here.
76
00:05:14,910 --> 00:05:17,010
Download it if you want to follow along.
77
00:05:17,460 --> 00:05:23,490
So the client here is the launcher and it wants to be interacting with an object pool, as we saw in
78
00:05:23,490 --> 00:05:24,030
our slides.
79
00:05:24,030 --> 00:05:31,860
So the way we would do this is to add in a private variable here, which is going to be a type I object
80
00:05:32,280 --> 00:05:32,820
pool.
81
00:05:33,450 --> 00:05:37,740
And we have to say what it's a pool of, what's the type that's going to be in this pool?
82
00:05:38,070 --> 00:05:40,350
It's going to be a bullet's pool.
83
00:05:40,650 --> 00:05:47,190
So we'll call this the bullet pool and we need to instantiate this in a week.
84
00:05:48,210 --> 00:05:55,380
So it's create an awake method here, and we do need to include using Unity Engine Pool in order to
85
00:05:55,380 --> 00:05:56,280
get access to this.
86
00:05:56,880 --> 00:06:03,030
Then we need to instantiate it to a concrete implementation of the I object pool interface.
87
00:06:03,510 --> 00:06:07,050
So the way we do this is with something like object pool.
88
00:06:07,050 --> 00:06:08,460
There are other types of pool here.
89
00:06:08,460 --> 00:06:16,170
There's like a linked pool, there's a hash set pool that just have different mechanisms of storing
90
00:06:16,170 --> 00:06:17,140
the objects under the hood.
91
00:06:17,160 --> 00:06:22,020
I think the easiest for us to deal with at this particular point, and probably the one you're going
92
00:06:22,020 --> 00:06:24,270
to use most often is just the object pool.
93
00:06:24,270 --> 00:06:25,470
So let's just go with the object pool.
94
00:06:25,920 --> 00:06:34,500
We're going to put in an object pool with the bullets type there again, and then we have a bunch of
95
00:06:34,500 --> 00:06:37,320
arguments that we need to pass in here.
96
00:06:37,800 --> 00:06:41,280
So let's have a look at what it will complain about.
97
00:06:41,400 --> 00:06:44,640
Well, the first thing I'm doing wrong is I need to use the new keyword.
98
00:06:44,820 --> 00:06:51,060
And then if I hover over the constructor that we're using, you can see the different functions that
99
00:06:51,060 --> 00:06:51,660
this takes.
100
00:06:51,660 --> 00:06:56,610
It takes a create function it takes and this is not the easiest place to see it.
101
00:06:56,610 --> 00:07:00,170
So let's go over to the documentation and see what it takes.
102
00:07:00,180 --> 00:07:05,040
We go down to the section in the documentation about the object pool itself.
103
00:07:05,040 --> 00:07:09,750
And don't worry, these links will be in the resources for you to have a look at, and you can see that
104
00:07:09,960 --> 00:07:13,440
these are the parameters to the object pool constructor.
105
00:07:13,470 --> 00:07:20,760
We have a create function, so this is what gets called when we need to create a new instance in the
106
00:07:20,760 --> 00:07:21,090
pool.
107
00:07:21,090 --> 00:07:27,480
If there wasn't already an instance already, as we saw in that diagram, we have an action on get.
108
00:07:27,630 --> 00:07:32,580
So this happens every time an object is got from the pool, so that would be enabling would happen.
109
00:07:32,880 --> 00:07:35,340
Like that action release?
110
00:07:35,370 --> 00:07:41,780
So when we return something back to the pool handle it, does it get disabled so that it's, you know,
111
00:07:41,790 --> 00:07:44,370
isn't affecting gameplay or whatever?
112
00:07:44,520 --> 00:07:48,240
Basically, this is something that can happen whenever we like.
113
00:07:48,570 --> 00:07:50,520
And then we've got the action on destroy.
114
00:07:50,790 --> 00:07:55,140
So this is if it gets returned to the pool and there's too many items in the pool, it and the pool
115
00:07:55,410 --> 00:07:58,050
is configured to have fewer items.
116
00:07:58,050 --> 00:08:03,450
As you can see, there are there are properties further down here for configuring the capacity of the
117
00:08:03,450 --> 00:08:06,900
pool because it sends the pool pools to full and we want to destroy it.
118
00:08:07,230 --> 00:08:12,280
Then this tells it how it can destroy an object in the pool.
119
00:08:12,300 --> 00:08:17,520
So this is all configured through passing in functions to the constructor, which will see how to do
120
00:08:17,790 --> 00:08:18,970
in just a second.
121
00:08:18,990 --> 00:08:22,050
So the first thing we want to pass in is a create function.
122
00:08:22,050 --> 00:08:27,000
So we're going to have I'm going to call it, create bullet like so again, because we're passing in
123
00:08:27,000 --> 00:08:29,200
the function itself and we don't want to be called now.
124
00:08:29,200 --> 00:08:34,020
We want it to be called at a later date, then we don't want to put the parentheses on the end.
125
00:08:34,140 --> 00:08:41,159
Now can you use control dot to generate a method and you'll see that it will go ahead and give it the
126
00:08:41,159 --> 00:08:47,670
correct signature here, which is that it should return the type of bullet it should return a bullet
127
00:08:47,670 --> 00:08:48,660
for us to use.
128
00:08:49,290 --> 00:08:51,760
So how do we instantiate a bullet?
129
00:08:51,780 --> 00:08:57,720
Well, we've got our bullet prefab here so you can go ahead and just use this instantiate call from
130
00:08:57,720 --> 00:08:58,980
Line twenty one.
131
00:08:58,980 --> 00:09:05,960
So I'm just going to cut it out there and I'm going to paste in the instantiate bullet prefab like so
132
00:09:05,970 --> 00:09:13,980
so that's going to go ahead and create a bullet prefab, and we want to make sure that we return that
133
00:09:13,980 --> 00:09:15,570
bullet prefab like so.
134
00:09:16,170 --> 00:09:18,870
And now how do we go ahead?
135
00:09:18,870 --> 00:09:25,050
And instead of an update instantiating we need to be getting an object from the pool so that it has
136
00:09:25,050 --> 00:09:28,770
the opportunity to reuse from that pool if it wants to.
137
00:09:29,430 --> 00:09:36,960
So the way we do this is to do bullet pool dot get instead of having what we were doing previously,
138
00:09:37,260 --> 00:09:40,160
basically instead of having it instantiate directly.
139
00:09:40,170 --> 00:09:47,400
So this now should have the opportunity to reuse if objects are being properly returned to the pool.
140
00:09:47,520 --> 00:09:49,500
But at the moment they are not.
141
00:09:49,500 --> 00:09:52,230
They're just simply being destroyed by the bullet.
142
00:09:52,830 --> 00:09:59,340
So what happens here is that we need our bullet to have a reference to the pool in order to release
143
00:09:59,340 --> 00:09:59,850
itself.
144
00:10:00,420 --> 00:10:03,390
So the way we're going to do this is to have a.
145
00:10:03,560 --> 00:10:10,190
Private variable on the bullet, which references the pool, so it's going to be I object pool and it's
146
00:10:10,190 --> 00:10:16,400
going to be a type bullet, and we'll call this the bullet pool again.
147
00:10:18,170 --> 00:10:25,040
And we need to have the using statements using Unity Engine Pool for this to work, we're also going
148
00:10:25,040 --> 00:10:30,590
to have a public method here, which I'm just going to call void set pool.
149
00:10:31,430 --> 00:10:39,380
And it's going to take in an I object pool and we're going to use this as setter as you might imagine
150
00:10:39,440 --> 00:10:42,230
going to set the pool from that parameter.
151
00:10:42,890 --> 00:10:49,320
Now that means that's in on became visible instead of destroying the current game object.
152
00:10:49,340 --> 00:10:56,930
What we're going to do is we're going to return it to the pool so we can do bullet pool dot release
153
00:10:57,290 --> 00:11:01,010
and we can pass in our selves because it takes a bullet.
154
00:11:01,310 --> 00:11:04,250
We can pass it ourselves to release ourselves when we become invisible.
155
00:11:04,910 --> 00:11:09,560
Now, the final step of the puzzle is to make that link between the bullet and the pool that it's part
156
00:11:09,560 --> 00:11:14,960
of, so we can do that and launcher in launcher, we instantiate this bullet prefab.
157
00:11:15,260 --> 00:11:21,070
Now, once we've got hold of that, let's just introduce a little local bullet variable by doing a controlled
158
00:11:21,110 --> 00:11:23,210
alternate, factoring into a variable.
159
00:11:23,840 --> 00:11:28,880
Then once we've got a bullet before we return it, we want to give it a link to the pool.
160
00:11:28,880 --> 00:11:34,700
So it's going to be bullet dot set pool and the pool we're passing in is the bullet pool that we instantiate
161
00:11:34,700 --> 00:11:35,330
in a week.
162
00:11:35,870 --> 00:11:37,070
So hopefully you're following along.
163
00:11:37,070 --> 00:11:41,990
What we've got here is a bullet pool owned by the launcher, created by the launcher.
164
00:11:42,770 --> 00:11:49,700
It will automatically create bullets using this create bullet function that we have created here in
165
00:11:49,700 --> 00:11:50,270
the launcher.
166
00:11:51,170 --> 00:11:54,560
It gives the bullet a link to the pool that it's dealing with.
167
00:11:55,130 --> 00:12:01,250
And now, instead of dealing with instantiating and destruction directly, we allow the pool to do that
168
00:12:01,250 --> 00:12:04,910
and we interface with the pool to get and release objects into it.
169
00:12:05,630 --> 00:12:11,120
So let's go ahead and see if this alleviates some of the issue we were having previously.
170
00:12:11,780 --> 00:12:16,730
So if we go back to unity and hit play and then we have a look.
171
00:12:18,010 --> 00:12:21,610
And what's the behavior if I go ahead and spawn a bullet?
172
00:12:21,910 --> 00:12:27,700
You can see once it's gone off screen, it does not disappear from our scene.
173
00:12:27,700 --> 00:12:30,820
Hierarchy isn't being destroyed, it's being returned to the pool.
174
00:12:31,150 --> 00:12:33,250
However, it's not being disabled.
175
00:12:33,370 --> 00:12:40,480
And also, when I hit space, you can see it's not being reset to be at the starting location where
176
00:12:40,480 --> 00:12:41,470
it should be fired from.
177
00:12:41,480 --> 00:12:42,610
That's something that should happen.
178
00:12:42,820 --> 00:12:48,340
Every time we get a bullet from the pool, its location should be reset and it should be enabled.
179
00:12:48,610 --> 00:12:52,450
So those are two things that I would like to challenge you to do.
180
00:12:52,780 --> 00:12:59,520
The way we're going to do this is by passing in a couple more parameters here to create two object pool
181
00:12:59,530 --> 00:13:00,270
and we create it.
182
00:13:00,280 --> 00:13:06,010
So here, as well as create bullets, we're going to have an on get.
183
00:13:06,610 --> 00:13:16,510
And let's have also an on release method and these two methods we're going to go ahead and implement.
184
00:13:16,580 --> 00:13:23,210
It's going to be controlled darts to generate the method on launcher and the same again for on release.
185
00:13:23,230 --> 00:13:25,090
So now we've got on get on release.
186
00:13:25,090 --> 00:13:29,630
I'm just going to reorder these into the order that makes the most sense.
187
00:13:29,650 --> 00:13:37,180
So again, create bullet on, get and then release being the final thing in the lifecycle and what I
188
00:13:37,180 --> 00:13:39,130
would like you to do as your challenge.
189
00:13:39,130 --> 00:13:42,220
No challenge slide here is implement on get.
190
00:13:42,700 --> 00:13:49,140
So they re enables a deactivated bullet and it also updates its location to be at the starting point
191
00:13:49,150 --> 00:13:54,190
of the launcher and on release should disable the bullet so it's no longer visible.
192
00:13:54,610 --> 00:13:56,170
Pause the video and have a go.
193
00:13:59,280 --> 00:13:59,810
OK.
194
00:13:59,880 --> 00:14:03,000
Welcome back, so hopefully you've had a little play around in this.
195
00:14:03,330 --> 00:14:08,730
So the first thing to do with on let's try with on release, actually, how are we going to disable
196
00:14:08,730 --> 00:14:09,180
the bullet?
197
00:14:09,420 --> 00:14:10,930
Well, I don't want to just disable the bullet.
198
00:14:10,930 --> 00:14:14,490
I actually want to disable its entire game object.
199
00:14:14,490 --> 00:14:17,340
So I'll do objects, which is in this case, is the bullet.
200
00:14:17,340 --> 00:14:22,560
So let's rename that straight away to bullet, which I think is a better name for this parameter.
201
00:14:22,830 --> 00:14:27,950
Then we're going to do Bullet Dot Game Object Dot set active false.
202
00:14:28,980 --> 00:14:36,030
So that disables the game object on which the bullet is a script now on Get is going to do the opposite
203
00:14:36,030 --> 00:14:36,540
of this.
204
00:14:36,540 --> 00:14:43,130
So the parameter again, I'm going to rename two bullets, then I'm going into a bullet game object
205
00:14:43,140 --> 00:14:45,540
set active, equal to true.
206
00:14:45,720 --> 00:14:49,200
So now all the scripts in that bullet are going to run.
207
00:14:49,770 --> 00:14:54,720
But like I said, the other thing we need to do here is set the transform of the bullets or rather its
208
00:14:54,720 --> 00:14:55,200
position.
209
00:14:55,210 --> 00:15:01,380
So bullet transformed opposition is equal to the launchers transformed opposition.
210
00:15:01,830 --> 00:15:09,120
This is something we weren't actually doing initially, so this may update the location of our launch.
211
00:15:09,220 --> 00:15:10,260
Let's check where our launches.
212
00:15:10,260 --> 00:15:11,310
It's down here at the bottom.
213
00:15:12,000 --> 00:15:15,330
So I'm going to put it over on the left hand side of the screen, give our bullet a little bit more
214
00:15:15,330 --> 00:15:17,010
travel time across the screen as well.
215
00:15:18,000 --> 00:15:20,280
Now I've got an error somewhere in my code.
216
00:15:20,760 --> 00:15:27,540
I got an extra comma here in this list of methods that I'm passing into an object pool constructor.
217
00:15:27,540 --> 00:15:28,490
So let's remove that.
218
00:15:28,500 --> 00:15:30,420
And now it should hopefully compile.
219
00:15:30,660 --> 00:15:35,490
And once it does, let's go ahead and hit play and start spawning some bullets.
220
00:15:35,490 --> 00:15:36,780
So we spawn all these bullets.
221
00:15:36,780 --> 00:15:42,300
They start to go off the screen and you can see as they go off screen, the bullets in the hierarchy
222
00:15:42,300 --> 00:15:43,530
are being disabled.
223
00:15:43,530 --> 00:15:45,840
So that's the release process happening.
224
00:15:45,840 --> 00:15:48,690
The bullets get disabled when they are released.
225
00:15:49,170 --> 00:15:52,200
Now what happens if I spawn some new bullets?
226
00:15:52,380 --> 00:15:57,900
Well, actually, no new bullets get spawn, but those bullets that went back into the object pool got
227
00:15:57,900 --> 00:16:04,770
re-enabled and reset to the previous location, so you can see that it spawns as many bullets as it
228
00:16:04,770 --> 00:16:05,350
needs.
229
00:16:05,460 --> 00:16:08,880
But then, once those are spawned, it no longer needs them.
230
00:16:08,880 --> 00:16:11,100
It goes ahead and disables them.
231
00:16:11,730 --> 00:16:17,910
Now, one more thing that you can do with this object pool that was some other stuff in here, and one
232
00:16:17,910 --> 00:16:20,070
of them was the max size.
233
00:16:20,730 --> 00:16:23,270
Now that's just an interesting parameter for us to set.
234
00:16:23,910 --> 00:16:27,780
By the way, you can set one of these parameters by name just like us.
235
00:16:27,780 --> 00:16:28,240
So.
236
00:16:28,240 --> 00:16:33,540
So I can do max size to be, let's say, three, for example.
237
00:16:33,540 --> 00:16:37,050
And let's put a comma on the last line, the app that already works.
238
00:16:37,050 --> 00:16:40,160
So that saying, set the parameter maxes to three.
239
00:16:40,480 --> 00:16:41,460
What's that going to do?
240
00:16:41,670 --> 00:16:43,380
Let's go ahead and try this out.
241
00:16:43,560 --> 00:16:50,520
So let's restart, play and spawn some bullets so you can see I can spawn as many bullets as I want.
242
00:16:50,520 --> 00:16:52,650
That's not what max size is doing.
243
00:16:53,010 --> 00:16:56,280
However, let's let the steady state just die down.
244
00:16:56,730 --> 00:16:59,460
Well, nothing here has particularly happened.
245
00:16:59,970 --> 00:17:01,860
So why is that?
246
00:17:01,920 --> 00:17:06,060
That's because we haven't given it away to destroy bullets.
247
00:17:06,420 --> 00:17:11,940
So we need an on destroy call back to pass into our constructor.
248
00:17:12,329 --> 00:17:17,520
We can go ahead and generate that method in launcher, and this is going to be pretty straightforward.
249
00:17:17,530 --> 00:17:21,720
How do we go ahead and destroy a bullet while we do it by destroying its game object?
250
00:17:22,140 --> 00:17:26,130
So let's rename the parameter again as familiar to Bullet.
251
00:17:26,579 --> 00:17:33,960
And we're also going to call the destroy method on bullets dots game object, so that should destroy
252
00:17:33,960 --> 00:17:34,380
a bullet.
253
00:17:34,740 --> 00:17:43,740
Now, if we head back into unity and start spawning some of these, then you can see that when it gets
254
00:17:43,740 --> 00:17:50,790
destroyed, some of them actually do get destroyed and you can see that what's left is only three bullets,
255
00:17:50,790 --> 00:17:53,240
because that is the max size of the object pool.
256
00:17:53,250 --> 00:18:00,420
So what happens is with an object pool, if you configure a max size, then it will trim back the object
257
00:18:00,420 --> 00:18:06,600
pool when objects are released and some of them will get destroyed if the pool is already over that
258
00:18:06,600 --> 00:18:07,060
size.
259
00:18:07,080 --> 00:18:12,210
So you can see we're only ever left with a pool of three bullets in this particular configuration,
260
00:18:12,600 --> 00:18:17,700
although it will create more bullets if needed at any given point in time.
261
00:18:17,880 --> 00:18:19,440
Now, why might you do this?
262
00:18:19,450 --> 00:18:23,910
You might do this for resource saving purposes, because if you spawn loads and loads of bullets and
263
00:18:23,910 --> 00:18:29,790
you keep them around all the time, then that is increasing the amount of memory your game uses and
264
00:18:29,790 --> 00:18:32,640
the amount of resources even though they are disabled.
265
00:18:32,910 --> 00:18:39,630
So that is the object pooling pattern and now easier than ever to implement in unity, thanks to native
266
00:18:39,630 --> 00:18:42,570
support for this object pool interface.
27488
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.