Skip to content

Instantly share code, notes, and snippets.

@mfdeveloper
Last active April 13, 2022 16:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mfdeveloper/79eb0bd0f8dbdcbea457a746ef34482f to your computer and use it in GitHub Desktop.
Save mfdeveloper/79eb0bd0f8dbdcbea457a746ef34482f to your computer and use it in GitHub Desktop.
Unity: Singleton pattern implementation

Unity: Singleton

Unity Singleton Monobehaviour component, that can be attached to Game Objects. You can use SingletonPersistent to persist the instance among scenes or just Singleton class to use the same instance on the only one scene.

Main use cases

  • Managers that should use the same instance among scripts (e.g GameManager, ScoreManager, InputManager...)
  • When you need use any component that depends of a Game object in the scene (e.g access AudioSource inside of an singleton)

Consider use a ScriptableObject instead

Getting started

Create a script that inherits from Singleton<MyScript> passing the script class by generics:

public class GameManager : Singleton<GameManager> {
  ...
}

In any other script, access the Instance property. The value of this property should be equal from any script:

public class PlayerController : Monobehaviour {
  
  private void Awake() {
      // Get the singleton instance
      var gameManager = GameManager.Instance;
  }
}

References

The implementation here was based in this gist above!!

using UnityEngine;
namespace Unity.Patterns
{
/// <summary>
/// Generic Singleton MonoBehaviour to use with non persistent GameObjects
/// </summary>
/// <remarks>
/// <b>Based in:</b><br/>
/// <a href="https://gist.github.com/mstevenson/4325117">Generic Singleton classes for Unity</a><br/>
/// <a href="https://www.youtube.com/watch?v=Ova7l0UB26U">Design Pattern: Singletons in Unity</a>
/// </remarks>
/// <typeparam name="T">Class to use as a singleton</typeparam>
public class Singleton<T> : MonoBehaviour where T : Component
{
private static T _instance;
public static T Instance
{
get
{
if (_instance == null)
{
GameObject obj = new GameObject
{
name = typeof(T).Name,
hideFlags = HideFlags.HideAndDontSave
};
_instance = obj.AddComponent<T>();
}
return _instance;
}
}
protected virtual void Awake()
{
if (_instance == null)
{
_instance = FindObjectOfType<T>(true);
}
}
private void OnDestroy()
{
if (_instance == this)
{
_instance = null;
}
}
}
}
using UnityEngine;
namespace Unity.Patterns
{
/// <summary>
/// Generic Singleton MonoBehaviour persistent among scenes
/// </summary>
/// <remarks>
/// <b>Based in:</b><br/>
/// <a href="https://gist.github.com/mstevenson/4325117">Generic Singleton classes for Unity</a><br/>
/// <a href="https://www.youtube.com/watch?v=Ova7l0UB26U">Design Pattern: Singletons in Unity</a>
/// </remarks>
/// <typeparam name="T">Class to use as a singleton</typeparam>
public class SingletonPersistent<T> : MonoBehaviour where T : Component
{
public static T Instance { get; private set; }
protected virtual void Awake()
{
if (Instance == null)
{
Instance = FindObjectOfType<T>(true);
if (Instance == null)
{
Instance = this as T;
DontDestroyOnLoad(this);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment