Skip to content

Easy to use, Singleton pattern on MonoBehaviour class, including swapping between instances while keeping other none instances alive.

License

Notifications You must be signed in to change notification settings

ToxPlayers/Unity-Singleton-MonoBehaviours

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Unity-Singleton-MonoBehaviours

Easy to use, Singleton MonoBehaviours pattern for unity.
Only the SingletonMono.cs in Singeltons folder is needed.
SingletonExtensions.cs will automatically set all instances before the first scene loads.
Works with inheritance.

Usage:

public class ExampleSingleton : SingletonMono<ExampleSingleton>
{
    public int Value { get; private set; }
    protected override void OnInstanceSet()
    {
        Debug.Log("Instance set for SomeClass Singleton");
    }
}
//shared instance base on Inheritance
//everytime a new SomeOtherClass's awake is called it will steal the SomeClass Instance
public class ExampleSingletonInherited : SomeClass
{
    void Start()
    {
        ForceInstance();
    } 
}
// Use SingletonMono<T>.Instance to get the current instance
// Will get ExampleSingletonInherited if it's the instance
public class ClassUsesExampleSingleton : SomeClass
{
    void Start()
    {
        Debug.Log(ExampleSingleton.Instance.Value);
    } 
}

Switch instance using:

SomeClass some = ...;
SomeClass.Instance = some; //set it to null to remove the instance
some.ForceInstance(): //forces this to be the instance class, same as Instance = some

The methods that you can override:

/// <summary>
/// alled when this object is set to Instance of <typeparamref name="T"></typeparamref>
/// </summary>
protected virtual void OnInstanceSet() {} 
/// <summary>
/// Called when this object is no longer the Instance object.
/// </summary>
protected virtual void OnInstanceRemoved() { } 

About

Easy to use, Singleton pattern on MonoBehaviour class, including swapping between instances while keeping other none instances alive.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages