/UnitySingleton

Primary LanguageC#MIT LicenseMIT

UnitySingleton

中文版本

UnitySingleton provides six types of singleton base classes, which can be used to create the corresponding derived types as needed.

Singleton

The most commonly used singleton and the basic implementation of the singleton pattern.

Class definition:

public class MySingleton : Singleton<MySingleton>
{
    public void MyMethod()
    {
    }
}

Use:

MySingleton.Instance.MyMethod();

SingletonMonoBehaviour

Singleton based on MonoBehaviour.

Class definition:

public class MySingletonMonoBehaviour : SingletonMonoBehaviour<MySingletonMonoBehaviour>
{
    public void MyMethod()
    {
    }
}

Use:

MySingletonMonoBehaviour.Instance.MyMethod();

The instance will be displayed in Hierarchy after created:

image_01

SingletonScriptableObject

Singleton based on ScriptableObject.

Class definition:

public class MySingletonScriptableObject : SingletonScriptableObject<MySingletonScriptableObject>
{
    public int intValue;
    public string stringValue;
}

Use:

UnityEngine.Debug.Log($"intValue is {MySingletonScriptableObject.Instance.intValue}");

After the singleton class has been created, it can be edited on the Project Settings page. The path to the page can be specified by adding the SettingsMenuPath attribute.

image_02

Whether to include the instance (asset) of the ScriptableObject in the build process to make it available in runtime player can be defined by overriding the ShouldIncludeInBuild method, e.g.:

protected override bool ShouldIncludeInBuild(BuildReport report)
{
    bool devBuild = (report.summary.options & UnityEditor.BuildOptions.Development) == 0;
    bool android = report.summary.platformGroup == UnityEditor.BuildTargetGroup.Android;
    return android && devBuild;
}

SingletonPersistent

Class definition:

public class MySingletonPersistent : SingletonPersistent<MySingletonPersistent>
{
    public bool enableSound;
    public bool enableMusic;
    public int volume;
}

A SingletonPersistent instance supports runtime persistence, and by default JSON format is used. Overriding 'OnSave' and 'OnLoad' to customize the saving and loading process.

SingletonThreadSafe

Class definition:

public class MySingletonThreadSafe : SingletonThreadSafe<MySingletonThreadSafe>
{
    private int value;

    public int Increase()
    {
        return Interlocked.Increment(ref value);
    }
}

SingletonThreadLocal

Class definition:

public class MySingletonThreadLocal : SingletonThreadLocal<MySingletonThreadLocal>
{
    public void SomeMethod()
    {
        UnityEngine.Debug.Log(Thread.CurrentThread.ManagedThreadId);
    }
}

Installation

Clone this repository and copy it to your project folder, or add https://github.com/aillieo/UnitySingleton.git#upm as a dependency in the Package Manager window.