Skip to content

hibzzgames/Hibzz.Singletons

Repository files navigation

Hibzz.Singletons

LICENSE Twitter Follow Discord Unity C#

A library of singletons for Unity

Installation

Via Github This package can be installed in the Unity Package Manager using the following git URL.

https://github.com/hibzzgames/Hibzz.Singletons.git

Alternatively, you can download the latest release from the releases page and manually import the package into your project.


Basic Usage

The singleton classes are located inside the namespace Hibzz.Singletons and any of the Unity MonoBehaviors can be converted into a singleton using the following syntax.

using Hibzz.Singletons;

public class AIManager : Singleton<AIManager>
{
    public int LiveNPCCount;
}

Now this Singleton can be accessed using the Instance field.

int _currentNPCCount = AIManager.Instance.LiveNPCCount;
Debug.Log($"Number of Live NPC's in scene: {_currentNPCCount}");

Scriptable Singletons

This package includes support for ScriptableSingletons. These are Singletons that exists as a ScriptableObject asset in the Resources folder. This allows a Scriptable Singleton to be accessed from anywhere in the project without having to be in the scene.

Moreover, these objects can be automatically created when the class is tagged with the [CreateScriptableSingletonAsset] attribute and the "Create Scriptable Singleton Asset" menu item is selected from the "Hibzz/Singletons" menu.


[StaticAccess] Attribute

The Singletons package comes with a powerful tool for accessing instanced members of a Singleton class without the Instance field. This is done by tagging the member with a [StaticAccess] attribute.

// defining the singleton
public partial class AIManager : Singleton<AIManager>
{
    [StaticAccess] int _liveNPCCount;
}

// Accessing the member
var npcCount = AIManager.LiveNPCCount;

Learn more about this package in the documentation.

Have a question or want to contribute?

If you have any questions or want to contribute, feel free to join the Discord server or Twitter. I'm always looking for feedback and ways to improve this tool. Thanks!

Additionally, you can support the development of these open-source projects via GitHub Sponsors and gain early access to the projects.