45 public class SingletonComponent<T> : MonoBehaviour where T : UnityEngine.MonoBehaviour
47 private static T s_Instance =
null;
48 public static T Instance
52 if( s_Instance !=
null )
57 T[] instances = Resources.FindObjectsOfTypeAll<T>();
58 if( instances !=
null )
62 for(
int i = 0; i < instances.Length; ++i )
64 T instance = instances[ i ];
65 if( instance ==
null )
70 if( instance.hideFlags != HideFlags.None )
76 if(
string.IsNullOrEmpty(instance.gameObject.scene.name) )
81 s_Instance = instance;
84 if(s_Instance.gameObject.transform.parent ==
null && Application.isPlaying)
96 && Application.isPlaying)
98 string name =
string.Format(
"__{0}__", typeof( T ).FullName );
99 GameObject singletonGo =
new GameObject( name );
100 s_Instance = singletonGo.AddComponent<T>();
101 if(s_Instance.gameObject.transform.parent ==
null){
110 internal static bool IsInstantiated => s_Instance !=
null;
112 protected virtual void Awake()
114 if(s_Instance ==
null)
116 s_Instance = Instance;