| 1234567891011121314151617181920212223242526272829303132333435363738 |
- #if !NOT_UNITY
- using UnityEngine;
- public class Singleton<T> : MonoBehaviour where T : MonoBehaviour
- {
- protected static T _instance;
- private static bool missed = false;
- public static T instance
- {
- get
- {
- if (!_instance)// && (!Application.isPlaying || !missed))
- {
- _instance = Object.FindObjectOfType<T>(true);
- if (!_instance)
- {
- missed = true;
- }
- }
- return _instance;
- }
- }
- protected virtual void Awake()
- {
- _instance = this as T;
- missed = false;
- }
- private void OnDestroy()
- {
- _instance = null;
- missed = false;
- }
- }
- #endif
|