Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:04,689 --> 00:00:09,700
So let's take another look at how we tend to use the singleton in unity.
2
00:00:09,820 --> 00:00:17,650
We're only really using it because if we didn't have the pattern, this object would appear again and
3
00:00:17,650 --> 00:00:18,520
again and again.
4
00:00:18,520 --> 00:00:23,590
Every time we reload the scene or go to a new scene, we would get a new instance and it would also
5
00:00:23,590 --> 00:00:25,660
be set as do not destroy on load.
6
00:00:25,750 --> 00:00:29,830
So what we're looking for isn't really the singleton pattern.
7
00:00:29,830 --> 00:00:34,240
It's just so happens that this golden hammer has been used to solve the problem.
8
00:00:34,570 --> 00:00:42,730
What we're looking for is a way to create an object that is persistent across scenes, and the way I
9
00:00:42,730 --> 00:00:48,610
would prefer to do that is to make that the responsibility of a different Class A, perhaps a persistent
10
00:00:48,610 --> 00:00:49,390
object spawns.
11
00:00:49,420 --> 00:00:54,940
Now, if you've taken the RPG series, you will have come across this because I have introduced that
12
00:00:54,940 --> 00:00:56,130
class over there as well.
13
00:00:56,140 --> 00:01:00,220
But I think it's good to talk about it in the context of the singleton pattern as well.
14
00:01:00,280 --> 00:01:05,980
So what we can do is we can go back to our ambient audio player behavior and I'm going to strip out
15
00:01:06,190 --> 00:01:10,360
the stuff here that is to do with only once initialization.
16
00:01:10,360 --> 00:01:11,350
In fact, I'm going to remove that.
17
00:01:11,350 --> 00:01:12,880
Don't destroy and load all of this stuff.
18
00:01:13,270 --> 00:01:19,960
Let's just make it an ambient audio player behavior, and all it should do is that it has a fade music
19
00:01:19,960 --> 00:01:23,650
method, which is it's only one responsibility.
20
00:01:24,040 --> 00:01:29,530
However, let's introduce the persistent object Spooner now the persistent objects.
21
00:01:29,530 --> 00:01:34,120
Warner does have some static state, but you'll notice that it has little state.
22
00:01:34,120 --> 00:01:39,730
As I can possibly find, it is a persistent object that has a single Boolean.
23
00:01:39,880 --> 00:01:42,700
So it's just saying whether we had spawned or not.
24
00:01:43,120 --> 00:01:44,290
And how does this work?
25
00:01:44,650 --> 00:01:50,500
Instead of trying to spawn a class or anything like that, it spawns a prefab.
26
00:01:50,860 --> 00:01:58,300
So we configure it to spawn a particular prefab, and it will go ahead and do that unless it has already
27
00:01:58,300 --> 00:01:58,870
spawned one.
28
00:01:59,320 --> 00:02:04,270
So what happens is in a week, if we've already spawned one because we've done that in the previous
29
00:02:04,270 --> 00:02:05,620
scene, nothing happens.
30
00:02:06,040 --> 00:02:11,980
Otherwise, we spawn the persistent object and we set that we have spawned to true and you can see down
31
00:02:11,980 --> 00:02:13,450
here and spawn persistent objects.
32
00:02:13,600 --> 00:02:19,420
All it's doing is the instantiating that and setting it to don't destroy on LOTE so that it will continue
33
00:02:19,420 --> 00:02:22,030
to go on through into following scenes.
34
00:02:22,240 --> 00:02:29,530
Now my challenge to you is to go ahead and implement this for our ambient audio player behavior because
35
00:02:29,530 --> 00:02:38,290
as you can see in unity at the moment, if I go and load the scene and I hit reload several times,
36
00:02:38,380 --> 00:02:42,990
what I'm getting is, well, first of all, my singleton hasn't even don't destroy a load.
37
00:02:43,000 --> 00:02:51,490
So assuming that it was then destroy and load, if I had that set up in a week to destroy on load the
38
00:02:51,490 --> 00:02:57,100
game object just the sake of testing it here, we're going to remove that very soon and we go and play
39
00:02:57,100 --> 00:02:58,120
over in unity.
40
00:02:58,330 --> 00:03:03,790
Then we're going to see that when we reload, each time we reload, that new singleton is being created.
41
00:03:04,510 --> 00:03:08,010
So I'd like you to try and use that spawn and mono behavior.
42
00:03:08,050 --> 00:03:09,220
Put it in your scene.
43
00:03:09,220 --> 00:03:10,960
Link it to a prefab.
44
00:03:10,960 --> 00:03:16,690
Create a prefab that is with the ambient audio player, so create an audio player prefab.
45
00:03:17,200 --> 00:03:21,580
Add the spawn into the scene instead and try reloading the scene.
46
00:03:21,580 --> 00:03:27,520
Now, see what happens and see that you can use this kind of pattern to achieve the same effect as the
47
00:03:27,520 --> 00:03:28,060
singleton.
48
00:03:28,060 --> 00:03:34,150
But without a singleton and with far less static state around that static state does need to be there,
49
00:03:34,150 --> 00:03:41,710
unfortunately, but is restricted to this one class, not to the ambient audio player that could now
50
00:03:41,710 --> 00:03:43,030
be unit tested.
51
00:03:43,040 --> 00:03:47,350
It could have all sorts of other good advantages that we don't need to deal with.
52
00:03:47,380 --> 00:03:53,260
We also don't have that global access to it anymore, so it can't just go anywhere in a scene, and
53
00:03:53,260 --> 00:03:56,530
things are a lot better with this setup.
54
00:03:59,420 --> 00:03:59,960
OK.
55
00:03:59,990 --> 00:04:00,480
Welcome back.
56
00:04:00,500 --> 00:04:06,170
Let's give this a stab say, the first thing we're going to do is, as I said, we're going to take
57
00:04:06,170 --> 00:04:10,460
this persistent object spawned and we're going to put it in a scene.
58
00:04:10,460 --> 00:04:11,750
So we're going to create a new empty.
59
00:04:12,200 --> 00:04:14,620
Call it the object, Spooner.
60
00:04:17,120 --> 00:04:22,160
And you could use this for everything that you need to instantiate, and he wants, not just your audio
61
00:04:22,160 --> 00:04:28,160
play, you can put everything that needs to be instantiated only once into a prefab, which is quite
62
00:04:28,160 --> 00:04:29,150
kind of quite nice.
63
00:04:29,180 --> 00:04:33,590
You don't have to worry about doing this singleton pattern again and again and again.
64
00:04:34,250 --> 00:04:41,060
So this is telling me that there is not a match here, so I'm going to fix my own typos here.
65
00:04:41,060 --> 00:04:44,810
That should have been the persistent object spawn, and I think I must have accidentally deleted this
66
00:04:45,110 --> 00:04:46,010
at some point.
67
00:04:46,430 --> 00:04:55,160
Now I'm going to go over and drank this into object spawn in the scene, and then we're going to want
68
00:04:55,170 --> 00:04:57,560
to create a prefab.
69
00:04:57,750 --> 00:05:04,550
There's going to be a persistent prefab, so I'm going to create an empty, persistent objects that's
70
00:05:04,550 --> 00:05:11,480
going to be turned into a prefab after I've created put my singleton example object underneath and a
71
00:05:11,480 --> 00:05:14,420
drag that into anywhere in my assets folder.
72
00:05:14,420 --> 00:05:21,020
At the moment, I'm going to delete it from my scene because I'm going to be spawning it via this persistent
73
00:05:21,020 --> 00:05:21,740
object supporter.
74
00:05:22,370 --> 00:05:24,960
So I pop that in to the persistent object spawn.
75
00:05:24,970 --> 00:05:27,740
Now I can go ahead and hit play.
76
00:05:28,340 --> 00:05:33,130
And now you can see that the persistent object clone is here and don't destroy unload.
77
00:05:33,140 --> 00:05:34,040
We've only got one.
78
00:05:34,490 --> 00:05:41,360
I can go ahead and hit reload, and you can see that no matter how many times I reload that scene,
79
00:05:41,750 --> 00:05:47,450
it's not going to spawn those persistent objects again because of the static state, that static variable.
80
00:05:48,140 --> 00:05:55,280
So that is my personal solution to how I do persistent objects in unity and avoid the use of the singleton
81
00:05:55,280 --> 00:05:55,840
pattern.
82
00:05:55,880 --> 00:05:58,720
I hope you find that useful in your own projects.
83
00:05:58,740 --> 00:06:02,510
Feel free to use that code, adapt it and improve it as you see fit.
84
00:06:02,750 --> 00:06:08,330
And if you do improve it, share it down in the discussions so other students can have a go to.
8789
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.