Skip to content

Commit 8467fd9

Browse files
committed
Add Singleton and MonoSingleton
1 parent ca0f8c7 commit 8467fd9

File tree

7 files changed

+165
-63
lines changed

7 files changed

+165
-63
lines changed

Assets/Scripts/GameManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using UnityEngine;
44
using UnityEngine.SceneManagement;
55

6-
public class GameManager : Singleton<GameManager>
6+
public class GameManager : MonoSingleton<GameManager>
77
{
88

99
[SerializeField]

Assets/Scripts/MonoSingleton.cs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
public abstract class MonoSingleton<T> : MonoBehaviour where T : Component
6+
{
7+
8+
#region Fields
9+
10+
/// <summary>
11+
/// The instance.
12+
/// </summary>
13+
private static T instance;
14+
15+
#endregion
16+
17+
#region Properties
18+
19+
/// <summary>
20+
/// Gets the instance.
21+
/// </summary>
22+
/// <value>The instance.</value>
23+
public static T Instance
24+
{
25+
get
26+
{
27+
if ( instance == null )
28+
{
29+
instance = FindObjectOfType<T> ();
30+
if ( instance == null )
31+
{
32+
GameObject obj = new GameObject ();
33+
obj.name = typeof ( T ).Name;
34+
instance = obj.AddComponent<T> ();
35+
}
36+
}
37+
return instance;
38+
}
39+
}
40+
41+
#endregion
42+
43+
#region Methods
44+
45+
/// <summary>
46+
/// Use this for initialization.
47+
/// </summary>
48+
protected virtual void Awake ()
49+
{
50+
if ( instance == null )
51+
{
52+
instance = this as T;
53+
DontDestroyOnLoad ( gameObject );
54+
}
55+
else
56+
{
57+
Destroy ( gameObject );
58+
}
59+
}
60+
61+
#endregion
62+
63+
}

Assets/Scripts/MonoSingleton.cs.meta

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Scripts/SceneSingleton.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Scripts/Singleton.cs

Lines changed: 77 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,82 @@
1-
using System.Collections;
1+
using System.Collections;
22
using System.Collections.Generic;
33
using UnityEngine;
44

5-
public abstract class Singleton<T> : MonoBehaviour where T : Component
5+
public abstract class Singleton<T> where T : Singleton<T>, new()
66
{
7-
8-
#region Fields
9-
10-
/// <summary>
11-
/// The instance.
12-
/// </summary>
13-
private static T instance;
14-
15-
#endregion
16-
17-
#region Properties
18-
19-
/// <summary>
20-
/// Gets the instance.
21-
/// </summary>
22-
/// <value>The instance.</value>
23-
public static T Instance
24-
{
25-
get
26-
{
27-
if ( instance == null )
28-
{
29-
instance = FindObjectOfType<T> ();
30-
if ( instance == null )
31-
{
32-
GameObject obj = new GameObject ();
33-
obj.name = typeof ( T ).Name;
34-
instance = obj.AddComponent<T> ();
35-
}
36-
}
37-
return instance;
38-
}
39-
}
40-
41-
#endregion
42-
43-
#region Methods
44-
45-
/// <summary>
46-
/// Use this for initialization.
47-
/// </summary>
48-
protected virtual void Awake ()
49-
{
50-
if ( instance == null )
51-
{
52-
instance = this as T;
53-
DontDestroyOnLoad ( gameObject );
54-
}
55-
else
56-
{
57-
Destroy ( gameObject );
58-
}
59-
}
60-
61-
#endregion
62-
7+
8+
#region Fields
9+
10+
/// <summary>
11+
/// The instance.
12+
/// </summary>
13+
private static T instance;
14+
15+
#endregion
16+
17+
#region Properties
18+
19+
/// <summary>
20+
/// Gets the instance.
21+
/// </summary>
22+
/// <value>The instance.</value>
23+
public static T Instance
24+
{
25+
get
26+
{
27+
if (Instance == null)
28+
{
29+
//ensure that only one thread can execute
30+
lock (typeof(T))
31+
{
32+
if (Instance == null)
33+
{
34+
instance = new T();
35+
instance.Init();
36+
}
37+
}
38+
}
39+
40+
return instance;
41+
}
42+
}
43+
44+
45+
#endregion
46+
47+
#region Methods
48+
49+
public static void CreateInstance()
50+
{
51+
DestroyInstance();
52+
instance = Instance;
53+
}
54+
55+
public static void DestroyInstance()
56+
{
57+
if (instance == null) return;
58+
59+
instance.Clear();
60+
instance = default(T);
61+
}
62+
63+
protected void Init()
64+
{
65+
OnInit();
66+
}
67+
68+
public void Clear()
69+
{
70+
OnClear();
71+
}
72+
73+
protected virtual void OnInit()
74+
{
75+
}
76+
77+
protected virtual void OnClear()
78+
{
79+
}
80+
#endregion
81+
6382
}

Assets/Scripts/Singleton.cs.meta

Lines changed: 2 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ProjectSettings/ProjectVersion.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
m_EditorVersion: 2017.1.0p4
1+
m_EditorVersion: 2021.3.6f1c1
2+
m_EditorVersionWithRevision: 2021.3.6f1c1 (07401303b748)

0 commit comments

Comments
 (0)