SingletonLauncher

Yet another singleton work around (C#)
This implementation of a singletons for unity results in the following:
 * No need to place a prefab in every scene
 * Awake gets called on any singleton prior to it being used ( guaranteed )
 * Any script used for a singleton can be configured completely in the inspector.
 * One game object slipped into the scene for all singletons ( single ).
 * Does not do a null/exists check every time a singleton is accessed.

Requirements

 * One prefab with each singleton type component on it named "Singletons" and placed in a folder named "Resources" anywhere in the Assets folder.
 * If using JS or boo with this script, you should place this script into the Standard Assets or Plugins folder so it is compiled first.

The Script
Filename should be SingletonEnsure.cs

Example
Then making a prefab gameobject in Resources named Singletons and adding the above example to the game object inside the prefab.

Then in other parts of the game you would access this by the following ( snippet )

or alternatively accessing the singleton directly

More Notes
To explain it more, once any singleton script's .singleton gets requested if the entire singleton prefab has not been loaded from resources it does so prior to returning the loaded in value. This way you can make sure that the system will be there as long as it is in the prefab itself.

The generics system allows for easy upfront access to the singleton loaded via prefab, and structure. The fact that the prefab is loaded from resources gives you the benefit of configuring the scripts inside the editor as well.

The way that SingletonMonBehaviour works theres also no sort of lookup involved, and any calls to SingletonMonoBehavior.Initializer.Bind is simply a empty void function which ensures the static constructor loads in the resources needed when it is needed, and never lets go.

More Options
Sometimes you may not even need to actually instantiate the prefab which would be a valid way to go as well. But this approach in general i think supports most cases and does not tie into any messages a script may choose to. With that said you could remove the Instantiate line and use the prefab itself as long as you do not modify any members off it and do not require Awake, Start, OnDestroy, Update or other messages that may be sent.

You're allowed to put scripts that are not singleton types onto the prefab which do not need a public singleton accessor. These will init like normal and if you wanted the singleton off of the prefab in game you have the option of doing the following this may be more ideal to some.

Important
This system is based off strong static initialization. Unity does not like some things done in static initialization such as loading from resources, using Random, or instantiating. The trick to this script is that the static initialization is not that of a object unity would create but rather a subclass of one which will not static initialize until something on it is called ( in this case Initializer.Bind ). This approach is safe as long as you do not call any singletons or functions which use singletons in a static constructor(initializer) of a unity type such as MonoBehaviour or ScriptableObject.

so these are not okay:

where these are okay:

Lastly as the singletons are loaded as needed, if you do need or want the system to be loaded prior like if you needed to log into some service. At the title insert a gameobject with the SingletonEnsure component. This will make sure that the singletons will be initialized.

If using Unity3.4 or above you'll want to set the calling order on SingletonEnsure to a lower value so that it gets called prior to others if you plan on using it.

With all that said, all singletons load together so if you access one singleton all singletons are loaded by the time you get the singleton back which again if that turns out to be intensive based on how you initialize your singleton types SingletonEnsure is a good idea to place in the intro / first loaded scene so you can avoid a hit later on while playing.

Finally
All in all i think its pretty flexible. The few restrictions are in my opinion worth it and structurally sound.

Feel free to edit this article to support the other languages.