Skip to content

DerKekser/unity-power-singleton

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Unity - Power Singleton

Power Singleton is a flexible Singleton Pattern package that's designed to streamline your Unity game development. Unlike traditional Singletons, these are defined merely by an attribute and require only to implement Unity's MonoBehaviour.

Contents

Simple Example

You can define a singleton by adding the PowerSingleton attribute to a class. The class must inherit from MonoBehaviour and the attribute must be applied to the class.

using Kekser.PowerSingleton.Attributes;
using UnityEngine;

[PowerSingleton]
public class GameManager : MonoBehaviour
{
    public void AddScore(int score)
    {
        // ...
    }
    
    // ...
}

You can access the singleton by using the Single class.

using Kekser.PowerSingleton;

Single<GameManager>.Instance.AddScore(10);

Interface / Abstract Class

You can also define a singleton by an interface or an abstract class. This widened flexibility allows you to use multiple Singleton classes, given they implement the specified interface. The system intuitively chooses the active class in the current scene.

public interface IGameManager
{
    void AddScore(int score);
}
using Kekser.PowerSingleton.Attributes;
using UnityEngine;

[PowerSingleton(typeof(IGameManager))]
public class GameManager : MonoBehaviour, IGameManager
{
    public void AddScore(int score)
    {
        // ...
    }
    
    // ...
}

You can access the singleton by using the Single class and specifying the interface.

using Kekser.PowerSingleton;

Single<IGameManager>.Instance.AddScore(10);

Creation

If Needed Creation

You can define that the singleton should only be created when it is needed. This is useful if you want to use the singleton in a scene where it is not yet present. The singleton will be created as soon as it is needed.

using Kekser.PowerSingleton.Attributes;
using UnityEngine;

[PowerSingleton(typeof(GameManager), Creation = PowerSingletonCreation.IfNeeded, CreationName = "GameManager")]
public class GameManager : MonoBehaviour
{
    public void AddScore(int score)
    {
        // ...
    }
    
    // ...
}

Auto Creation

You can define that the singleton should be created automatically. This is useful if you want to use the singleton in a scene where it is not yet present. The singleton will be created as soon as the scene is loaded.

using Kekser.PowerSingleton.Attributes;
using UnityEngine;

[PowerSingleton(typeof(GameManager), Creation = PowerSingletonCreation.Always, CreationName = "GameManager")]
public class GameManager : MonoBehaviour
{
    public void AddScore(int score)
    {
        // ...
    }
    
    // ...
}

DontDestroyOnLoad

You can define that the singleton should not be destroyed when the scene is changed.

using Kekser.PowerSingleton.Attributes;
using UnityEngine;

[PowerSingleton(typeof(GameManager), DontDestroyOnLoad = true)]
public class GameManager : MonoBehaviour
{
    public void AddScore(int score)
    {
        // ...
    }
    
    // ...
}

Manual Binding

You can manually bind a MonoBehaviour to a singleton. This is useful if you cannot use the PowerSingleton attribute or if you want to change the singleton at runtime.

using Kekser.PowerSingleton;

GameManager gameManager = new GameObject("GameManager").AddComponent<GameManager>();
Single<GameManager>.Bind(gameManager);

Install

Install via Unity Package

Download the latest release and import the package into your Unity project.

Install via git URL

You can add this package to your project by adding this git URL in the Package Manager:

https://github.com/DerKekser/unity-power-singleton.git?path=Assets/Kekser/PowerSingleton

Package Manager

License

This library is under the MIT License.