Created
January 10, 2021 21:04
-
-
Save LiamKarlMitchell/5283928e56d9b6c079b80f55f8de0e86 to your computer and use it in GitHub Desktop.
Useful Unity Scripts
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 UnityEditor; | |
using UnityEngine; | |
namespace UnityTemplateProjects.Misc | |
{ | |
/// <summary> | |
/// Inherit from this base class to create a singleton. | |
/// e.g. public class MyClassName : Singleton<MyClassName> {} | |
/// </summary> | |
public class Singleton<T> : MonoBehaviour where T : MonoBehaviour | |
{ | |
// Check to see if we're about to be destroyed. | |
private static bool m_ShuttingDown = false; | |
private static object m_Lock = new object(); | |
private static T m_Instance; | |
/// <summary> | |
/// Access singleton instance through this propriety. | |
/// </summary> | |
public static T Instance | |
{ | |
get | |
{ | |
if (m_ShuttingDown && !Application.isEditor) | |
{ | |
Debug.LogWarning("[Singleton] Instance '" + typeof(T) + | |
"' already destroyed. Returning null."); | |
return null; | |
} | |
lock (m_Lock) | |
{ | |
if (m_Instance == null) | |
{ | |
// Search for existing instance. | |
m_Instance = FindObjectOfType(typeof(T)) as T; | |
if (m_Instance == null) | |
{ | |
var o = GameObject.Find(typeof(T).ToString() + " (Singleton)"); | |
if (o) | |
{ | |
// Find by name | |
m_Instance = o.GetComponent<T>(); | |
// Re-init as this gets lost on code recompile/reload. | |
(m_Instance as Singleton<T>).Init(); | |
} | |
} | |
// Create new instance if one doesn't already exist. | |
if (m_Instance == null) | |
{ | |
// Need to create a new GameObject to attach the singleton to. | |
var singletonObject = new GameObject(); | |
singletonObject.hideFlags = HideFlags.DontSave; | |
m_Instance = singletonObject.AddComponent<T>(); | |
singletonObject.name = typeof(T).ToString() + " (Singleton)"; | |
(m_Instance as Singleton<T>).Init(); | |
if (!Application.isEditor) | |
{ | |
// Make instance persistent. | |
DontDestroyOnLoad(singletonObject); | |
} | |
} | |
} | |
return m_Instance; | |
} | |
} | |
} | |
protected virtual void Start() | |
{ | |
//Debug.Log("Singleton Start called"); | |
} | |
protected virtual void Init() | |
{ | |
} | |
// Start is called before the first frame update | |
protected virtual void Awake() | |
{ | |
if(m_Instance != null && m_Instance != gameObject as T) | |
{ | |
Debug.LogError("[Singleton] Trying to create a second instance of singleton ", this); | |
Destroy(gameObject); | |
} | |
else if (m_Instance == null) | |
{ | |
m_Instance = this as T; | |
} | |
} | |
private void OnApplicationQuit() | |
{ | |
m_ShuttingDown = true; | |
} | |
private void OnDestroy() | |
{ | |
m_ShuttingDown = true; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment