Created
October 11, 2018 20:46
-
-
Save darkdukey/0b4b289bf4691a847af3fcf9339ae1bd to your computer and use it in GitHub Desktop.
Unity Singleton
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
namespace Samsung | |
{ | |
public abstract class Singleton<T> : Singleton where T : MonoBehaviour | |
{ | |
#region Fields | |
[CanBeNull] | |
private static T _instance; | |
[NotNull] | |
// ReSharper disable once StaticMemberInGenericType | |
private static readonly object Lock = new object(); | |
[SerializeField] | |
private bool _persistent = true; | |
#endregion | |
#region Properties | |
[NotNull] | |
public static T Instance | |
{ | |
get | |
{ | |
if (Quitting) | |
{ | |
Debug.LogWarning($"[{nameof(Singleton)}<{typeof(T)}>] Instance will not be returned because the application is quitting."); | |
// ReSharper disable once AssignNullToNotNullAttribute | |
return null; | |
} | |
lock (Lock) | |
{ | |
if (_instance != null) | |
return _instance; | |
var instances = FindObjectsOfType<T>(); | |
var count = instances.Length; | |
if (count > 0) | |
{ | |
if (count == 1) | |
return _instance = instances[0]; | |
Debug.LogWarning($"[{nameof(Singleton)}<{typeof(T)}>] There should never be more than one {nameof(Singleton)} of type {typeof(T)} in the scene, but {count} were found. The first instance found will be used, and all others will be destroyed."); | |
for (var i = 1; i < instances.Length; i++) | |
Destroy(instances[i]); | |
return _instance = instances[0]; | |
} | |
Debug.Log($"[{nameof(Singleton)}<{typeof(T)}>] An instance is needed in the scene and no existing instances were found, so a new instance will be created."); | |
return _instance = new GameObject($"({nameof(Singleton)}){typeof(T)}") | |
.AddComponent<T>(); | |
} | |
} | |
} | |
#endregion | |
#region Methods | |
private void Awake() | |
{ | |
if (_persistent) | |
DontDestroyOnLoad(gameObject); | |
OnAwake(); | |
} | |
protected virtual void OnAwake() { } | |
#endregion | |
} | |
public abstract class Singleton : MonoBehaviour | |
{ | |
#region Properties | |
public static bool Quitting { get; private set; } | |
#endregion | |
#region Methods | |
private void OnApplicationQuit() | |
{ | |
Quitting = true; | |
} | |
#endregion | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Duplicate the gameobject when the scene reload