Creating singletons as ScriptableObjects
Imagine you and your design team need to create, configure, and test singleton classes in the Unity Editor. Using ScriptableObjects
as data containers not only frees you from attaching your scripts to GameObjects
but also gives you the freedom to create singleton assets from the Asset menu. Both added features make scriptable objects a good fit to address testing concerns with the Singleton pattern, and it is much easier for non-programming members of your team to work with them.
Your last task in this chapter is to create a generic singleton as a ScriptableObject
asset in the project. In the Scripts folder, create a new C# script, name it ScriptableSingleton
, and update its code to match the following code. This script creates a generic ScriptableObject
class that we can use to fetch a unique instance from the project resources when queried:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// 1
public...