Last active
December 24, 2023 00:49
-
-
Save anchan828/5045439 to your computer and use it in GitHub Desktop.
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
//参考: 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