Static Initialization

Is there any way to achieve static initialization in Unity?

Java allows you to do this within a class (static block):

static { // code }

C# allows you to do this within a class (static constructor):

static MyClass() { // code }

However, in Unity the static constructor only seems to get called after recompilation. This is the same as normal constructors then; but while we have Awake() to replace those, I'm unaware of anything that can be used for static initialization.

What would I use it for? Well, there's a bunch of design patterns that I'm aching to use in my game, such as Pluggable Factory. That pattern relies on the fact that subclasses of a product class can register themselves with a Maker before it is used to construct anything.

Using Pluggable factories allows you to specify any number of additional product subclasses without having to alter the code of the Maker that produces them, which means it is very easily to extend a framework with additional functionality.

You can use normal, non-monobehaviour classes with unity

This lets you use the static constructor for your factory without any problems

That way you can instantiate all your factories from a monobehaviour, and they'll all work without issue