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
2 changes: 1 addition & 1 deletion Assets/Scripts/GameManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using UnityEngine;
using UnityEngine.SceneManagement;

public class GameManager : Singleton<GameManager>
public class GameManager : MonoSingleton<GameManager>
{

[SerializeField]
Expand Down
63 changes: 63 additions & 0 deletions Assets/Scripts/MonoSingleton.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public abstract class MonoSingleton<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;
DontDestroyOnLoad ( gameObject );
}
else
{
Destroy ( gameObject );
}
}

#endregion

}
12 changes: 12 additions & 0 deletions Assets/Scripts/MonoSingleton.cs.meta

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

8 changes: 8 additions & 0 deletions Assets/Scripts/SceneSingleton.meta

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

135 changes: 77 additions & 58 deletions Assets/Scripts/Singleton.cs
Original file line number Diff line number Diff line change
@@ -1,63 +1,82 @@
using System.Collections;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public abstract class Singleton<T> : MonoBehaviour where T : Component
public abstract class Singleton<T> where T : Singleton<T>, new()
{

#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;
DontDestroyOnLoad ( gameObject );
}
else
{
Destroy ( gameObject );
}
}

#endregion


#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)
{
//ensure that only one thread can execute
lock (typeof(T))
{
if (Instance == null)
{
instance = new T();
instance.Init();
}
}
}

return instance;
}
}


#endregion

#region Methods

public static void CreateInstance()
{
DestroyInstance();
instance = Instance;
}

public static void DestroyInstance()
{
if (instance == null) return;

instance.Clear();
instance = default(T);
}

protected void Init()
{
OnInit();
}

public void Clear()
{
OnClear();
}

protected virtual void OnInit()
{
}

protected virtual void OnClear()
{
}
#endregion

}
5 changes: 2 additions & 3 deletions Assets/Scripts/Singleton.cs.meta

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

3 changes: 2 additions & 1 deletion ProjectSettings/ProjectVersion.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
m_EditorVersion: 2017.1.0p4
m_EditorVersion: 2021.3.6f1c1
m_EditorVersionWithRevision: 2021.3.6f1c1 (07401303b748)