Skip to content

Instantly share code, notes, and snippets.

@mendesbarreto
Created November 26, 2014 15:12
Show Gist options
  • Select an option

  • Save mendesbarreto/de89934f64f7a5b22d01 to your computer and use it in GitHub Desktop.

Select an option

Save mendesbarreto/de89934f64f7a5b22d01 to your computer and use it in GitHub Desktop.
Abstract class for easy Singleton in Unity3d
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