Skip to content

Instantly share code, notes, and snippets.

@anchan828
Last active December 24, 2023 00:49
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save anchan828/5045439 to your computer and use it in GitHub Desktop.
Save anchan828/5045439 to your computer and use it in GitHub Desktop.
//参考: http://wiki.unity3d.com/index.php?title=Singleton
using UnityEngine;
public abstract class MonoSingleton<T> : MonoBehaviour where T : MonoSingleton<T>
{
private static T instance = null;
public static T Instance {
get {
//Scene内にあったら取得
instance = instance ?? (FindObjectOfType (typeof(T)) as T);
//TをアタッチしたGameObject生成してT取得
instance = instance ?? new GameObject (typeof(T).ToString (), typeof(T)).GetComponent<T> ();
return instance;
}
}
private void OnApplicationQuit ()
{
instance = null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment