All language subtitles for 005 Better Than A Singleton_en

af Afrikaans
ak Akan
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bem Bemba
bn Bengali
bh Bihari
bs Bosnian
br Breton
bg Bulgarian
km Cambodian
ca Catalan
ceb Cebuano
chr Cherokee
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
ee Ewe
fo Faroese
tl Filipino
fi Finnish
fr French
fy Frisian
gaa Ga
gl Galician
ka Georgian
de German
el Greek
gn Guarani
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ia Interlingua
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
rw Kinyarwanda
rn Kirundi
kg Kongo
ko Korean
kri Krio (Sierra Leone)
ku Kurdish
ckb Kurdish (Soranรฎ)
ky Kyrgyz
lo Laothian
la Latin
lv Latvian
ln Lingala
lt Lithuanian
loz Lozi
lg Luganda
ach Luo
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mfe Mauritian Creole
mo Moldavian
mn Mongolian
my Myanmar (Burmese)
sr-ME Montenegrin
ne Nepali
pcm Nigerian Pidgin
nso Northern Sotho
no Norwegian
nn Norwegian (Nynorsk)
oc Occitan
or Oriya
om Oromo
ps Pashto
fa Persian
pl Polish
pt-BR Portuguese (Brazil)
pt Portuguese (Portugal)
pa Punjabi
qu Quechua
ro Romanian
rm Romansh
nyn Runyakitara
sm Samoan
gd Scots Gaelic
sr Serbian
sh Serbo-Croatian
st Sesotho
tn Setswana
crs Seychellois Creole
sn Shona
sd Sindhi
si Sinhalese
sk Slovak
sl Slovenian
so Somali
es Spanish
es-419 Spanish (Latin American)
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
tt Tatar
te Telugu
th Thai
ti Tigrinya
to Tonga
lua Tshiluba
tum Tumbuka
tr Turkish
tk Turkmen
tw Twi
ug Uighur
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
wo Wolof
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu

Original subtitles

So let's take another look at how we tend to use the singleton in unity.

We're only really using it because if we didn't have the pattern, this object would appear again and

again and again.

Every time we reload the scene or go to a new scene, we would get a new instance and it would also

be set as do not destroy on load.

So what we're looking for isn't really the singleton pattern.

It's just so happens that this golden hammer has been used to solve the problem.

What we're looking for is a way to create an object that is persistent across scenes, and the way I

would prefer to do that is to make that the responsibility of a different Class A, perhaps a persistent

object spawns.

Now, if you've taken the RPG series, you will have come across this because I have introduced that

class over there as well.

But I think it's good to talk about it in the context of the singleton pattern as well.

So what we can do is we can go back to our ambient audio player behavior and I'm going to strip out

the stuff here that is to do with only once initialization.

In fact, I'm going to remove that.

Don't destroy and load all of this stuff.

Let's just make it an ambient audio player behavior, and all it should do is that it has a fade music

method, which is it's only one responsibility.

However, let's introduce the persistent object Spooner now the persistent objects.

Warner does have some static state, but you'll notice that it has little state.

As I can possibly find, it is a persistent object that has a single Boolean.

So it's just saying whether we had spawned or not.

And how does this work?

Instead of trying to spawn a class or anything like that, it spawns a prefab.

So we configure it to spawn a particular prefab, and it will go ahead and do that unless it has already

spawned one.

So what happens is in a week, if we've already spawned one because we've done that in the previous

scene, nothing happens.

Otherwise, we spawn the persistent object and we set that we have spawned to true and you can see down

here and spawn persistent objects.

All it's doing is the instantiating that and setting it to don't destroy on LOTE so that it will continue

to go on through into following scenes.

Now my challenge to you is to go ahead and implement this for our ambient audio player behavior because

as you can see in unity at the moment, if I go and load the scene and I hit reload several times,

what I'm getting is, well, first of all, my singleton hasn't even don't destroy a load.

So assuming that it was then destroy and load, if I had that set up in a week to destroy on load the

game object just the sake of testing it here, we're going to remove that very soon and we go and play

over in unity.

Then we're going to see that when we reload, each time we reload, that new singleton is being created.

So I'd like you to try and use that spawn and mono behavior.

Put it in your scene.

Link it to a prefab.

Create a prefab that is with the ambient audio player, so create an audio player prefab.

Add the spawn into the scene instead and try reloading the scene.

Now, see what happens and see that you can use this kind of pattern to achieve the same effect as the

singleton.

But without a singleton and with far less static state around that static state does need to be there,

unfortunately, but is restricted to this one class, not to the ambient audio player that could now

be unit tested.

It could have all sorts of other good advantages that we don't need to deal with.

We also don't have that global access to it anymore, so it can't just go anywhere in a scene, and

things are a lot better with this setup.

OK.

Welcome back.

Let's give this a stab say, the first thing we're going to do is, as I said, we're going to take

this persistent object spawned and we're going to put it in a scene.

So we're going to create a new empty.

Call it the object, Spooner.

And you could use this for everything that you need to instantiate, and he wants, not just your audio

play, you can put everything that needs to be instantiated only once into a prefab, which is quite

kind of quite nice.

You don't have to worry about doing this singleton pattern again and again and again.

So this is telling me that there is not a match here, so I'm going to fix my own typos here.

That should have been the persistent object spawn, and I think I must have accidentally deleted this

at some point.

Now I'm going to go over and drank this into object spawn in the scene, and then we're going to want

to create a prefab.

There's going to be a persistent prefab, so I'm going to create an empty, persistent objects that's

going to be turned into a prefab after I've created put my singleton example object underneath and a

drag that into anywhere in my assets folder.

At the moment, I'm going to delete it from my scene because I'm going to be spawning it via this persistent

object supporter.

So I pop that in to the persistent object spawn.

Now I can go ahead and hit play.

And now you can see that the persistent object clone is here and don't destroy unload.

We've only got one.

I can go ahead and hit reload, and you can see that no matter how many times I reload that scene,

it's not going to spawn those persistent objects again because of the static state, that static variable.

So that is my personal solution to how I do persistent objects in unity and avoid the use of the singleton

pattern.

I hope you find that useful in your own projects.

Feel free to use that code, adapt it and improve it as you see fit.

And if you do improve it, share it down in the discussions so other students can have a go to.

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.