Tutorials Singletons in Unity: Embracing Unity Through All Types of Singleton

Singletons in Unity: Embracing Unity Through All Types of Singleton

Photo of author
By
/

When you buy through my links, I may earn a commission which will support me in creating more helpful content for you.

The world of game development continues to evolve exponentially, and Unity continues to improve as a reliable platform that enables developers to bring their ideas to life.

Singletons play a significant role in optimizing code, improving performance, and facilitating communication between game elements, which makes it among the various essential structures within Unity.

Check out this list of the Top Game Engines For Beginners.

Understanding Singletons in Unity

Singletons Defined:

Singleton is a creational design pattern that lets you ensure that a class has only one instance while providing a global access point to this instance.

The Power of Unity Singletons:

Singletons in Unity streamline code architecture by offering a centralized point for managing game elements. Whether it's player data, game settings, or crucial functionality, having a singleton instance ensures consistency and simplicity in implementation.

 Let's make it simple!!

Types of Singletons in Unity

1. Classic Singletons:

To implement singleton we start by creating a single class instance during runtime, then we make it accessible globally. This method is suitable when there is a need for a unique and global point of control.

public class ClassicSingleton
{
    private static ClassicSingleton instance;

    // Private constructor to prevent instantiation outside the class
    private ClassicSingleton() { }

    // Public method to access the singleton instance
    public static ClassicSingleton Instance
    {
        get
        {
            // If the instance doesn't exist, create it
            if (instance == null)
            {
                instance = new ClassicSingleton();
            }
            return instance;
        }
    }

    // Other methods and properties of the singleton class can be added
    public void PrintMessage()
    {
        Debug.Log("Classic Singleton Instance is working!");
    }
}

In this example, the ClassicSingleton class has a private static instance variable. The constructor is also private to prevent creating instances from outside the class. 

To access the singleton instance, we use the public Instance property. If the instance doesn't exist, it creates one. Otherwise, it returns the existing instance. 

The PrintMessage method is just an example of a method that can be called on the singleton instance.

// Access the singleton instance
ClassicSingleton mySingleton = ClassicSingleton.Instance;
// Call a method on the singleton instance
mySingleton.PrintMessage();

2. Lazy singletons

Lazy singletons are instantiated only when needed. This helps with the optimization of resources and memory use which will be allocated only if an action triggers the instantiation.

public class LazySingleton
{
    private static Lazy<LazySingleton> lazyInstance =
 new Lazy<LazySingleton>(() => new LazySingleton());
    private LazySingleton() { }

    // Public method to access the lazy singleton instance
    public static LazySingleton Instance
    {
        get { return lazyInstance.Value; }
    }

    public void PrintMessage()
    {
        Debug.Log("Lazy Singleton Instance is working!");
    }
}

In this code example, the LazySingleton class uses the Lazy<T> class to achieve lazy instantiation, which means that the instance of the object is created only when it is needed for the first time.

The lazyInstance variable is a static, lazy-initialized instance of the LazySingleton class, which is created when the Value property of the lazy instance is accessed for the first time.

Want to Start Learning Unity Today? Here is a list of the Best Unity Courses.

The private constructor ensures that the class cannot be instantiated outside itself, which means the class can only be instantiated from within its code. Finally, the public Instance property is used to access the lazy singleton instance created earlier.

3. Scriptable Object Singletons:

Scriptable Objects in Unity offer a script-based approach to creating singletons that persist across scenes. This singleton type is useful when managing game data, configurations, and other persistent elements.

[CreateAssetMenu(fileName = "GameSettings", menuName = "Singletons/GameSettings")]
public class GameSettings: ScriptableObject
{
    private static GameSettings instance;
    public static GameSettings Instance
    {
        get
        {
            if (instance == null)
            {
                instance = Resources.Load<GameSettings>("GameSettings");
                if (instance == null)
                {
                    instance = CreateInstance<GameSettings>();

                    UnityEditor.AssetDatabase.CreateAsset(instance, "Assets/Resources/GameSettings.asset");
                   UnityEditor.AssetDatabase.SaveAssets();
                }
            }
            return instance;}}
      
// Game settings properties
    public float soundVolume = 0.5f;
    public int difficultyLevel = 2;

    // Additional settings can be added as needed
}

The GameSettings class is a class that can be serialized and edited in the Unity Editor because it derives from ScriptableObject. By using the CreateAssetMenu attribute, you can create an instance of this ScriptableObject from the "Create" menu in the Unity Editor.

To access the singleton instance, you can use the Instance property. If the instance doesn't exist, it will try to load it from the project assets. If it's not found, a new instance will be created, saved as an asset, and loaded.

To use the ScriptableObject Singleton:

  • Create a new ScriptableObject in the Unity Editor using the "Create" menu.
  • Access the singleton instance in your code:
// Access the ScriptableObjectSingleton instance
GameSettings gameSettings = GameSettings.Instance;
// Access the soundVolume property
float currentSoundVolume = gameSettings.soundVolume;
// Now you can use the currentSoundVolume in your code
Debug.Log("Current Sound Volume: " + currentSoundVolume);

4. DontDestroyOnLoad Singletons:

Using the DontDestroyOnLoad function ensures that the singleton GameObject remains intact between scene transitions, preserving its state and functionality.

public class DontDestroySingleton : MonoBehaviour
{
    private static DontDestroySingleton instance;

    // Public property to access the singleton instance
    public static DontDestroySingleton Instance
    {
        get { return instance; }
    }

    private void Awake()
    {
        // Singleton pattern enforcement
        if (instance == null)
        {
            instance = this;
            DontDestroyOnLoad(gameObject);
        }


        else
        {
            // If an instance already exists, destroy the duplicate
            Destroy(gameObject);
        }
    }

    // Other methods and properties of the singleton can be added here
    public void PrintMessage()
    {
        Debug.Log("DontDestroyOnLoad Singleton Instance is working!");
    }
}

In this particular example, the DontDestroySingleton class is linked to a GameObject present in your scene. The Awake method is used to handle the Singleton pattern.

It first checks whether an instance already exists. If not, it sets itself as the instance and employs DontDestroyOnLoad to ensure that the GameObject persists between scene transitions. However, if an instance already exists, it gets rid of the duplicate GameObject.

Implementing Singletons Effectively

1. Encapsulation:

It is important to encapsulate the singleton instance to limit access to its internal workings. This can prevent unintended modifications and promote a more controlled and secure implementation.

2. Static Initialization:

Use static initialization methods to instantiate the singleton only once, avoiding overhead and potential conflicts.

3. Lazy Initialization:

For situations where conserving resources is crucial, implement lazy initialization strategies. Only instantiate the singleton when it is necessary for specific functionality.

Advantages of Using Singletons

1. Centralised Control:

Singletons provide a centralized approach to managing the necessary components of a game, resulting in a more organized and maintainable codebase.

2. Improved Performance:

Singletons help to reduce the number of object instances, which in turn enhances the overall performance and efficiency of the system.

3. Seamless Communication:

Singletons serve as efficient connectors between different components, allowing smooth communication and interaction within a gaming environment.

Potential Challenges and Best Practices

1. Global State Management:

It's important to manage the global state effectively to avoid unintended side effects and conflicts, while singletons offer global accessibility.

2. Singleton Abuse:

Avoid using singletons for every class. Only use them when a single, global instance is truly necessary.

What’s concluded

Understanding and using singletons is crucial in Unity game development for creating efficient and optimised games.

There are various types of singletons, such as classic singletons, lazy singletons, or Unity's Scriptable Objects. However, the key to success is implementing them thoughtfully while choosing best practices.

By embracing singletons, developers can enhance their Unity experience, create a harmonious relationship between code elements, and unlock the full potential of their creative projects.

Related Article: The Game Development Process