Skip to content

Commit f05ccbf

Browse files
Create MonoSingleton.cs (#4)
* Create MonoSingleton.cs Signed-off-by: averyocean65 <111367792+averyocean65@users.noreply.github.com> * Update MonoSingleton.cs Co-authored-by: Raven <raven@az-raven.com> Signed-off-by: averyocean65 <111367792+averyocean65@users.noreply.github.com> --------- Signed-off-by: averyocean65 <111367792+averyocean65@users.noreply.github.com> Co-authored-by: Raven <raven@az-raven.com>
1 parent 5d81bd9 commit f05ccbf

1 file changed

Lines changed: 26 additions & 0 deletions

File tree

MonoSingleton.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using UnityEngine;
2+
3+
namespace SubsurfaceStudios.Utilities.Instancing
4+
{
5+
public abstract class MonoSingleton<T> : MonoBehaviour where T : MonoSingleton<T>
6+
{
7+
public static T Instance { get; private set; }
8+
9+
protected virtual void Awake()
10+
{
11+
if (Instance != null && Instance != this)
12+
{
13+
Destroy(this);
14+
return;
15+
}
16+
17+
Instance = (T)this;
18+
}
19+
20+
protected virtual void OnDestroy()
21+
{
22+
if (Instance == this)
23+
Instance = null;
24+
}
25+
}
26+
}

0 commit comments

Comments
 (0)