Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions Assets/Scripts/SceneSingleton.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

// "Scene Singleton Pattern" or "Local Singleton Pattern"
// It differs from the traditional "Global singleton pattern",
// The purpose of "Scene Singleton Pattern" is to ensure that there is only one instance of the class in a given scene, but it does not require that instance to persist across scenes.
// This pattern is useful when you need to manage certain assets or features in a single scene, but you don't want those assets or features to remain present when the scene is switched.

public abstract class SceneSingleton<T> : MonoBehaviour where T : Component
{

#region Fields

/// <summary>
/// The instance.
/// </summary>
private static T instance;

#endregion

#region Properties

/// <summary>
/// Gets the instance.
/// </summary>
/// <value>The instance.</value>
public static T Instance
{
get
{
if (instance == null)
{
instance = FindObjectOfType<T>();
if (instance == null)
{
GameObject obj = new GameObject();
obj.name = typeof(T).Name;
instance = obj.AddComponent<T>();
}
}
return instance;
}
}

#endregion

#region Methods

/// <summary>
/// Use this for initialization.
/// </summary>
protected virtual void Awake()
{
if (instance == null)
{
instance = this as T;
}
else
{
Destroy(gameObject);
}
}

#endregion

}
11 changes: 11 additions & 0 deletions Assets/Scripts/SceneSingleton.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.