Skip to content

A DontDestroyObject of singleton sample

Notifications You must be signed in to change notification settings

KittoChanKC/SingletonSample

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 

Repository files navigation

SingletonSample

A Sample of Singleton.

Handling singleton with single scene is very simple.

Handling singleton as DontDestoryObject is not so simple.

This is a very simple idea for handling singleton as DontDestoryObject.

  1. class SingletonSample : Singleton

    • make any singleton class like this
  2. class Singleton : SingletonBase where T : MonoBehaviour

    • destroy other same T in the hierarchy automatically
    • set single static T Instance { get; private set; }
  3. class SingletonBase : MonoBehaviour

    • public virtual void SceneInit() for initialize on scene load
  4. SingletonLoader

    • exists on every scene
    • handle existing singletons
    • build not existing singletons
    • run SceneInit();
    • destroy self automatically

Setup is simple.

Make all your singleton gameobjects as prefabs.

Setup the list on SingletonLoader.

Make the SingletonLoader as prefab.

You need no singletone gameobjects in the scene but only one SingletonLoader in every scene.

Please refer to the SampleScene in Assets.

About

A DontDestroyObject of singleton sample

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages