Skip to content

Commit e8a1965

Browse files
committed
Added singleton interface
1 parent 208f8a7 commit e8a1965

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

Runtime/Utilities/Singleton.cs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@
44

55
namespace Lachee.Utilities
66
{
7+
/// <summary>
8+
/// This interface is used to define a singleton. It is used to define the properties of a singleton.
9+
/// </summary>
10+
/// <typeparam name="T"></typeparam>
11+
public interface ISingleton<T>
12+
{
13+
public static System.Type type { get; }
14+
public static T instance { get; }
15+
public static T available { get; }
16+
public static bool referenced { get; }
17+
}
718

819
/// <summary>
920
/// This class creates a Singleton GameObject that will either be lazily initialized when it is referenced for the first time or, grabbed from the scene if an instance already exists.
@@ -21,7 +32,8 @@ namespace Lachee.Utilities
2132
/// }
2233
/// </code>
2334
/// </example>
24-
public abstract class Singleton<T> : MonoBehaviour where T : Singleton<T>
35+
public abstract class Singleton<T> : MonoBehaviour, ISingleton<T>
36+
where T : Singleton<T>
2537
{
2638
internal static T _instance;
2739
private static bool _isquitting = false;
@@ -61,7 +73,7 @@ public static T instance
6173
{
6274
get
6375
{
64-
if (!available)
76+
if (!available)
6577
{
6678
if (_isquitting)
6779
Debug.LogError("Creating a new instance of " + type + " while a OnApplicationQuit has been detected.");
@@ -76,8 +88,8 @@ public static T instance
7688
#else
7789
Debug.LogError($"Singleton {type} cannot be created because DONT_CREATE_SINGLETONS is defined");
7890
#endif
79-
}
80-
else
91+
}
92+
else
8193
{
8294
Debug.LogError($"Singleton {type} cannot be created while not in Play Mode.");
8395
}
@@ -191,7 +203,7 @@ protected virtual void Awake()
191203
/// </summary>
192204
/// <remarks>Singleton inherits a <see cref="MonoBehaviour"/>, so standard Unity functions apply. However, Awake and OnApplicationQuit have been overriden.</remarks>
193205
/// <typeparam name="T">The class that is to inherit Singleton</typeparam>
194-
public abstract class EmphemeralSingleton<T> : Singleton<T>
206+
public abstract class EmphemeralSingleton<T> : Singleton<T>
195207
where T : EmphemeralSingleton<T>
196208
{
197209
protected override bool dontDestroyOnLoad => false;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "com.lachee.utilities",
3-
"version": "1.6.2",
3+
"version": "1.6.3",
44
"displayName": "Lachee's Utilities",
55
"description": "Bunch of utility functionality",
66
"unity": "2019.1",

0 commit comments

Comments
 (0)