In Unity3D having a singleton class is very useful, whether for “global” state or simply for the convenience of having a static accessor so you don’t have to have lots of: FindObjectOfType(typeof(Builder)) as Builder;
So you code up a C# singleton and then realize that you actually need it to be a MonoBehavior, not just a ScriptableObject – say you want the singleton to run coroutines, or have a transform, or any other MonoBehavior feature. But monobehaviors can’t be initialized with a constructor.
So what you want is a monobehavior pseudo singleton pattern. The unity community wiki has some good info about the various options.
In my current Unity project I realized that I was using lots of singletons for various state manager classes and wanted to get rid of all the boilerplate singleton stuff. So I wrote a parametrized generic pseudo singleton monobehavior class. I attempted to compile all the best advice out there, so it is threadsafe, has some extra stuff for setting a parent, doesn’t implement an Awake, and initializes lazily.
The key was the C# where keyword, used to constrain the parameterizing type. So in this case both the class is a MonoBehavior and its parameterization type is a MonoBehavior.
So now this blog has its first code section:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | |