Skip to content

Commit 77c325f

Browse files
committed
Revised the singletons implementation
Added PersistentMonoSingleton which is persistent across scenes Made MonoSingleton non-persistent Added ISingleton interface for unifying the methods and calls Add assembly definition Added namespace (UnityCommunity.UnitySingleton)
1 parent 84faf4b commit 77c325f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+4738
-497
lines changed

.vsconfig

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"version": "1.0",
3+
"components": [
4+
"Microsoft.VisualStudio.Workload.ManagedGame"
5+
]
6+
}

Assets/Scripts/GameManager.cs

-22
This file was deleted.

Assets/Scripts/MonoSingleton.cs

-109
This file was deleted.

Assets/Scripts/SceneSingleton.meta renamed to Assets/Scripts/Runtime.meta

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Scripts/Runtime/ISingleton.cs

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
4+
using UnityEngine;
5+
6+
namespace UnityCommunity.UnitySingleton
7+
{
8+
9+
/// <summary>
10+
/// The singleton interface.
11+
/// </summary>
12+
public interface ISingleton
13+
{
14+
15+
public void InitializeSingleton();
16+
17+
public void ClearSingleton();
18+
19+
}
20+
21+
}

Assets/Scripts/SceneSingleton/SceneSingleton.cs.meta renamed to Assets/Scripts/Runtime/ISingleton.cs.meta

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
+155
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
4+
using UnityCommunity.UnitySingleton;
5+
6+
using UnityEngine;
7+
8+
namespace UnityCommunity.UnitySingleton
9+
{
10+
11+
/// <summary>
12+
/// The basic MonoBehaviour singleton implementation, this singleton is destroyed after scene changes, use <see cref="PersistentMonoSingleton{T}"/> if you want a persistent and global singleton instance.
13+
/// </summary>
14+
/// <typeparam name="T"></typeparam>
15+
public abstract class MonoSingleton<T> : MonoBehaviour, ISingleton where T : MonoSingleton<T>
16+
{
17+
18+
#region Fields
19+
20+
/// <summary>
21+
/// The instance.
22+
/// </summary>
23+
private static T instance;
24+
25+
/// <summary>
26+
/// The initialization status of the singleton's instance.
27+
/// </summary>
28+
private SingletonInitializationStatus initializationStatus = SingletonInitializationStatus.None;
29+
30+
#endregion
31+
32+
#region Properties
33+
34+
/// <summary>
35+
/// Gets the instance.
36+
/// </summary>
37+
/// <value>The instance.</value>
38+
public static T Instance
39+
{
40+
get
41+
{
42+
if (instance == null)
43+
{
44+
instance = FindObjectOfType<T>();
45+
if (instance == null)
46+
{
47+
GameObject obj = new GameObject();
48+
obj.name = typeof(T).Name;
49+
instance = obj.AddComponent<T>();
50+
instance.OnMonoSingletonCreated();
51+
}
52+
}
53+
return instance;
54+
}
55+
}
56+
57+
/// <summary>
58+
/// Gets whether the singleton's instance is initialized.
59+
/// </summary>
60+
public virtual bool IsInitialized => this.initializationStatus == SingletonInitializationStatus.Initialized;
61+
62+
#endregion
63+
64+
#region Unity Messages
65+
66+
/// <summary>
67+
/// Use this for initialization.
68+
/// </summary>
69+
protected virtual void Awake()
70+
{
71+
if (instance == null)
72+
{
73+
instance = this as T;
74+
75+
// Initialize existing instance
76+
InitializeSingleton();
77+
}
78+
else
79+
{
80+
81+
// Destory duplicates
82+
if (Application.isPlaying)
83+
{
84+
Destroy(gameObject);
85+
}
86+
else
87+
{
88+
DestroyImmediate(gameObject);
89+
}
90+
}
91+
}
92+
93+
#endregion
94+
95+
#region Protected Methods
96+
97+
/// <summary>
98+
/// This gets called once the singleton's instance is created.
99+
/// </summary>
100+
protected virtual void OnMonoSingletonCreated()
101+
{
102+
103+
}
104+
105+
protected virtual void OnInitializing()
106+
{
107+
108+
}
109+
110+
protected virtual void OnInitialized()
111+
{
112+
113+
}
114+
115+
#endregion
116+
117+
#region Public Methods
118+
119+
public virtual void InitializeSingleton()
120+
{
121+
if (this.initializationStatus != SingletonInitializationStatus.None)
122+
{
123+
return;
124+
}
125+
126+
this.initializationStatus = SingletonInitializationStatus.Initializing;
127+
OnInitializing();
128+
this.initializationStatus = SingletonInitializationStatus.Initialized;
129+
OnInitialized();
130+
}
131+
132+
public virtual void ClearSingleton() { }
133+
134+
public static void CreateInstance()
135+
{
136+
DestroyInstance();
137+
instance = Instance;
138+
}
139+
140+
public static void DestroyInstance()
141+
{
142+
if (instance == null)
143+
{
144+
return;
145+
}
146+
147+
instance.ClearSingleton();
148+
instance = default(T);
149+
}
150+
151+
#endregion
152+
153+
}
154+
155+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
4+
using UnityCommunity.UnitySingleton;
5+
6+
using UnityEngine;
7+
8+
namespace UnityCommunity.UnitySingleton
9+
{
10+
11+
/// <summary>
12+
/// This singleton is persistent across scenes by calling <see cref="UnityEngine.Object.DontDestroyOnLoad(Object)"/>.
13+
/// </summary>
14+
/// <typeparam name="T"></typeparam>
15+
public abstract class PersistentMonoSingleton<T> : MonoSingleton<T> where T : MonoSingleton<T>
16+
{
17+
18+
#region Protected Methods
19+
20+
protected override void OnInitializing()
21+
{
22+
base.OnInitializing();
23+
if (Application.isPlaying)
24+
{
25+
DontDestroyOnLoad(gameObject);
26+
}
27+
}
28+
29+
#endregion
30+
31+
}
32+
33+
}

Assets/Scripts/Runtime/PersistentMonoSingleton.cs.meta

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)