Skip to content

Instantly share code, notes, and snippets.

@suakig
Created May 8, 2015 15:49
Show Gist options
  • Save suakig/5fff46b4308a177c2bc5 to your computer and use it in GitHub Desktop.
Save suakig/5fff46b4308a177c2bc5 to your computer and use it in GitHub Desktop.
MonoSingleton.cs
using UnityEngine;
public abstract class MonoSingleton<T> : MonoBehaviour where T : MonoSingleton<T>
{
static T m_Instance = null;
public static T instance
{
get
{
if( m_Instance != null )
{
return m_Instance;
}
System.Type type = typeof(T);
T instance = GameObject.FindObjectOfType(type) as T;
if( instance == null )
{
string typeName = type.ToString();
GameObject gameObject = new GameObject( typeName, type );
instance = gameObject.GetComponent<T>();
if( instance == null )
{
Debug.LogError("Problem during the creation of " + typeName,gameObject );
}
}
else
{
Initialize(instance);
}
return m_Instance;
}
}
static void Initialize(T instance)
{
if( m_Instance == null )
{
m_Instance = instance;
m_Instance.OnInitialize();
}
else if( m_Instance != instance )
{
DestroyImmediate( instance.gameObject );
}
}
static void Destroyed(T instance)
{
if( m_Instance == instance )
{
m_Instance.OnFinalize();
}
}
public virtual void OnInitialize() {}
public virtual void OnFinalize() {
//FindObjectOfTypeは負荷が多いため繰り返す場合ば解放しない
m_Instance = null;
}
void Awake()
{
Initialize( this as T );
}
void OnDestroy()
{
Destroyed( this as T );
}
void OnApplicationQuit()
{
Destroyed( this as T );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment