Skip to content
bmogluPublic

About

Singleton Pattern For Unity

Resources

Stars

0 stars

Watchers

1 watching

Forks

Latest commit

 

History

5 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 

Repository files navigation

Singleton Pattern

Table of Contents
  1. About The Singleton
  2. Getting Started
  3. Usage

About The Singleton

The singleton pattern is a way to ensure a class has only a single globally accessible instance available at all times. Behaving much like a regular static class but with some advantages. This is very useful for making global manager type classes that hold global variables and functions that many other classes need to access.

Getting Started

This is an example of how you may give instructions on setting up your project locally. To get a local copy up and running follow these simple example steps.

Installation

  1. Clone the repo
    git clone https://github.com/bmoglu/singleton.git
  2. Import package to your project

Usage

To make any class a singleton, simply inherit from the Singleton base class instead of MonoBehaviour, like so:

public class MySingleton : Singleton<MySingleton>
{
    // Then add whatever code to the class you need as you normally would.
    public string MyAwesomeString = "Hello world!";
}

Now you can access all public fields, properties and methods from the class anywhere using ClassName.Instance:

public class MyAwesomeClass : MonoBehaviour
{
    private void OnEnable()
    {
        Debug.Log(MySingleton.Instance.MyAwesomeString);
    }
}

License

Distributed under the MIT License. See LICENSE for more information.

About

Singleton Pattern For Unity

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors