Secure UnitySingleton

Description
This singleton differs from others on this wiki in that the creator must specifically define how the singleton creates using specific methods, and when that singleton is destroyed. This allows the creator to know the potential lifecycle of the singleton when creating it.

Using this script assumes you understand the Singleton Pattern and when to use it in a general program.

This singleton is not a replacement for a generic C# singleton. Instead this singleton is intended for using Unity specific functionality:
 * Access to MonoBehaviour Functions (Update, Start, OnLevelWasLoaded)
 * Data that is convenient to modify using the prefab inspector (Allow designers a nice UI without custom scripting an editor extension)
 * Object requires that gameobjects have a certain parent/child relationship or other scripts attached to the object.
 * Is this object creating secret singletons

Usage: Different UnitySingleton Types
These types are the options for loading the singleton when Instance is called and a new instance must be generated.

When selecting a type for your singleton the most important question is "What kind of data/state does this singleton store?" and that should be used to determine how that singleton is generated and managed.

Exists In Scene
This type searches the current scene for an object with the singleton component attached to it.

This is useful for objects that store data about a specific level, such as enemy generator counts, or level timers. As it allows a designer to modify the object in that scene, and the settings will only be referenced in that scene.

Loaded From Resources
This type creates an instance of a prefab with the singleton component attached to it from a Resources folder.

This is useful for storing custom configuration that is exactly the same across the entire life of the object (most likely the entire project itself), such as an input manager that stores the sensitivity of input, or network manager with configurations settings like port numbers.

Create On New GameObject
This type creates a new GameObject and attaches a new instance of the singleton component to it.

This is useful for objects that do not need any custom configuration, such as a performance tracker for framerate. Such a script would need to exist for the entirety of the project and need to be easy to reference, but does not require any designer input to configure properly.

Usage: Unity type inheritance
Not every script you need to create a singleton for will always inherit from will be directly from MonoBehaviour. The best example of this is the NetworkBehaviour and NetworkLobbyManager added with the 5.1 networking update. Both of these types extend MonoBehaviour, but they also have other functions that you want your singleton to be able to override. You can achieve this goal by creating a singleton "wrapper" for your inherited class as follows:

Secret Singletons (and how to avoid them)
A common problem that many users have when using Unity is creating 'Secret Singletons' or objects that there are only 1 of, and are referenced in that way, but are not getting the full advantages of using a singleton pattern. It is not always apparent that a script might be creating/using secret singletons, however this is more common in Unity due to the prevalence of functions such as these:

Both of these functions call an object with the assumption that only one exist, and that it is actually in the scene. However they do not guarantee that either of those is the case, or warn when they are not. Once these problems are found, they can be easily replaced with a singleton component that both adds clarity to how that script should be used, and allows easier/efficient access to it.

UnitySingletonAttribute.cs
Attribute used to store singleton configuration

UnitySingleton.cs
Singleton abstract class