Sunday, July 7, 2013

Unity and the Singleton design pattern

Singletons are very powerful when used for managers in game, or anything in general which you know you will have exactly one of (Game manager, audio manager, for a pure singleplayer game - the player, etc)

Since Unity expects MonoBehaviour class inheritance, static classes are out of the question, so a more Unity tailored solution was needed.
This class ensures that when you check for it's instance, it will be there no matter what.
You can later mark any class inheriting from this one as "Game static" and it will also live through all of your scenes.

using UnityEngine;
public abstract class MonoSingleton<T> : MonoBehaviour where T : MonoSingleton<T>
{
public bool IsGameStatic;
private static T _instance = null;
public static T Instance
{
get
{
// Instance required for the first time, we look for it
if( _instance == null )
{
_instance = GameObject.FindObjectOfType(typeof(T)) as T;
// Object not found, we create a temporary one
if( _instance == null )
{
Debug.LogWarning("No instance of " + typeof(T).ToString() + ", a temporary one is created.");
_instance = new GameObject("Temp Instance of " + typeof(T).ToString(), typeof(T)).GetComponent<T>();
// Problem during the creation, this should not happen
if( _instance == null )
{
Debug.LogError("Problem during the creation of " + typeof(T).ToString());
}
}
_instance.Init();
}
return _instance;
}
}
// If no other monobehaviour request the instance in an awake function
// executing before this one, no need to search the object.
private void Awake()
{
if( _instance == null )
{
_instance = this as T;
_instance.Init();
if (IsGameStatic)
{
DontDestroyOnLoad(gameObject);
}
}
else (_instance != this)
{
Destroy(this);
}
}
// This function is called when the instance is used the first time
// Put all the initializations you need here, as you would do in Awake
public virtual void Init(){}
// Make sure the instance isn't referenced anymore when the user quit, just in case.
private void OnApplicationQuit()
{
_instance = null;
}
}

Add the newly created component (MySingletonClass) to a gameobject in your scene and then you can safely call it from anywhere in your code, and assume that it was succesfully initialized.

MySingletonClass.Instance.Foo();

Notice:
If you want to implement a custom Awake function for anything that inherits from MonoSingleton, you must override Init.
If you create a new Awake function it will block MonoSingleton's awake function.