How to use Singletons in Unity3D without breaking everything...

How to use Singletons in Unity3D without breaking everything...

Jason Weimann

4 года назад

52,197 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

Sam Cavalheiro
Sam Cavalheiro - 21.03.2023 20:37

Please, fix this microphone

Ответить
Nikola Zagorac
Nikola Zagorac - 15.01.2023 13:56

There is a workaround for singletons, but if you are working on a team you must communicate how the system works. What you do is make your singleton base class abstract, add a protected abstract function called Initialize or OnAwake or whatever. Now also in the base Singleton class in the Awake function, after all the base class related code just call the Initialize function. So now all the child classes of the Singleton class require you to implement the Initialize function which is automatically called on the Awake function from your base class. Just make sure to never use the Awake function in your child classes. One step further would be to have a static function in your base class with the RuntimeInitializeOnLoadMethodAttribute attribute to automatically create an instance of your class when the scene loads.

Ответить
DustyGPU
DustyGPU - 12.01.2023 01:47

Singletons are mid and Scriptable Objects are based

Ответить
Chloe_The_Cat
Chloe_The_Cat - 05.09.2022 17:04

I really want to watch this video, but the changing volume makes it hard for me to listen and understand. I don't know, maybe it's easier for native English speakers. 🤔

Ответить
Zeki Özdemir
Zeki Özdemir - 01.03.2022 13:31

thank you

Ответить
David Marino
David Marino - 03.01.2022 04:30

If singletons are not great for global state management, what pattern would you recommend?

Ответить
Slex
Slex - 26.11.2021 09:03

Need a re-recording of this one day. The audio triggers my condition and I can't seem to make it sit right no matter what I do 😭

Ответить
Pirate Skeleton
Pirate Skeleton - 12.11.2021 00:17

I use a static class called Conventions to hold all of the useful functions that I tend to use throughout my different objects.

Ответить
My name is resh
My name is resh - 18.10.2021 22:02

Here is a problem I came across when using singleton pattern on my prefab.
I have 3 box (same prefab). each of them has one same script called changeColor (because it is prefab) ; I use singleton in this class like
if(isntance == null) { instance = this;} else{ destroy(gameObject)} . When game stars all of other boxes ( 2 boxes of 3 ) are disapppearing. I found the problem is when Instance in not null it destroys other boxes and keep only one box.

What should I do here.?

Ответить
Thygrrr
Thygrrr - 18.08.2021 19:55

You should retract this video, it's poor software engineering and you don't "new a property".

Ответить
PanTarZan
PanTarZan - 17.06.2021 15:33

So the main two points i got from it:
1. Singleton makes stuff easier than:
GameObject.FindObjectOfType() on every manager my game has, instead i have just static instance of one object, and i can event prevent duplicates :D
2.
static field values on MonoBehaviour are the same for ALL monobehaviours of same type, you dont have to cycle through every one to set one specific global value. hmm TIL :D
TYVM for you hard work Jason! :D

Ответить
Tim Chang
Tim Chang - 07.06.2021 20:33

English is not my first language. Your voice is unclear and loudness is not constant so I really had a hard time understanding your content!

Ответить
Beastly
Beastly - 17.04.2021 12:40

A video on object oriented vs process oriented programming? A GDC talk by the dude that built Thumper argued for it but I cant get my head around the differences. I don't think I've ever built anything purely on functions without classes or objects

Ответить
gregridd
gregridd - 22.12.2020 03:11

most of my scripts are singletons :S

Ответить
Leo Zhang
Leo Zhang - 14.08.2020 01:12

what's the VS code extension you used that gives the function arguments a label next to it? Like for example in the beginning of the video, you have Vector3.Distance(a: t.transform.position, b: point)

Ответить
Alevry13
Alevry13 - 26.07.2020 13:27

Interesting video, I recently started getting into Unity and your channel very helpful for me, thanks!

But I got a question, I created abstract class similar to AutoCleanupSingleton with virtual awake with cleanup and I still can add something to Awake if I need to. Is there still any downsides of using it compared to establishing singleton separately in each class i want to use as singleton?

Ответить
Andre Milani Martin
Andre Milani Martin - 21.05.2020 15:51

pls set up yiut mic properly. my ears are hurting

Ответить
Gordon MacLeod
Gordon MacLeod - 18.05.2020 22:08

All I wanted to know was how to use Singletons to create a class which contains global variables. I thought this would address this. Alas, this was such a confusing tutorial and introduced so much stuff which has nothing to do specifically with singletons. While I have enjoyed and learned a lot from your tutorials Jason, this one floors me! :(

Ответить
Noblauch Nobi-Knobi
Noblauch Nobi-Knobi - 08.04.2020 12:06

Line 11, never forget.

Ответить