A modern singleton in Unity

The singleton pattern is useful when writing game components like a game manager or localization manager in Unity. I would like to share with you the implementation I am currently using in my project.

Inspiration

While creating my implementation I took inspiration from several sources:

Code

Note: This code requires enabling the .NET 4 scripting runtime to access the Lazy<T> type.

  1. Open player settings in the Unity Inspector by selecting Edit > Project Settings > Player.
  2. Under the Configuration heading, click the Scripting Runtime Version drop-down and select .NET 4.x Equivalent. You will have to restart Unity for the change to take effect.

Explanation

My singleton is implemented as a generic class deriving from MonoBehaviour. This is necessary to have access to the DontDestroyOnLoad method we call in CreateSingleton.

The type argument should also derive from MonoBehaviour so that we can use the instance as a component and receive all messages like Awake, Start and Update. When used in a derived class, you pass the concrete type as the type argument. This looks a bit circular when written, but it is basically just saying “class X is a singleton of type X”.

For the pattern itself, I went with the version using Lazy<T> suggested by Jon Skeet in his article. The advantage of this is that Lazy<T> handles the thread-safety out of the box and we don’t have to do any locking ourselves.

The singleton initialization itself is very straightforward – we create an empty GameObject, attach our component to it and finally mark it so that it won’t be destroyed when new scene is loaded.

 

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.