Created
January 20, 2021 23:38
-
-
Save matiasvallejosdev/9f035464a986975464caf9e05964d503 to your computer and use it in GitHub Desktop.
Singleton Class for Unity3D
This file contains 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; | |
public class Singlenton<T> : MonoBehaviour where T : Singlenton<T> | |
{ | |
// Generic Class with T = Tipo de variable | |
private static T instance; | |
public static T Instance | |
{ | |
get { return instance; } | |
} | |
public static bool isInitalized | |
{ | |
get { return instance != null; } | |
} | |
protected virtual void Awake() | |
{ | |
if(instance != null) | |
{ | |
Debug.LogError("[Singleton] Trying to instantiate a sceond instance of a singleton class."); | |
} | |
else | |
{ | |
instance = (T)this; | |
} | |
} | |
protected virtual void OnDestroy() | |
{ | |
if (instance == this) | |
instance = null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment