Created
November 26, 2014 15:12
-
-
Save mendesbarreto/de89934f64f7a5b22d01 to your computer and use it in GitHub Desktop.
Abstract class for easy Singleton in Unity3d
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 UnityEngine; | |
| namespace AquelaFrameWork.Core | |
| { | |
| /// <summary> | |
| /// | |
| /// | |
| /// This class was build to make easy to get a Singleton class in the Unity Engine System. | |
| /// | |
| /// If would you want a single instance of a specific class, extends this class and get access from it by the static function "Instance" | |
| /// | |
| /// </summary> | |
| /// | |
| public abstract class ASingleton<T> : AFObject where T : MonoBehaviour | |
| { | |
| protected static T m_instance; | |
| protected ASingleton() | |
| { | |
| } | |
| /// <summary> | |
| /// Method returns the single instance of the Object | |
| /// </summary> | |
| public static T Instance | |
| { | |
| get | |
| { | |
| if (m_instance == null) | |
| { | |
| m_instance = AFObject.Create<T>(); | |
| GameObject.DontDestroyOnLoad(m_instance); | |
| } | |
| return m_instance; | |
| } | |
| } | |
| /// <summary> | |
| /// Destroys the single instance of the Object in the system | |
| /// </summary> | |
| public static void DestroyInstance() | |
| { | |
| Destroy(m_instance.gameObject); | |
| m_instance = null; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment