12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- using UnityEngine;
- using System.Collections;
- using System;
- namespace Rokid.MRC
- {
- public abstract class Singleton<T> where T : class, new()
- {
- private static T instance;
- public static T Instance
- {
- get
- {
- if(instance == null)
- {
- instance = new T();
- }
- return instance;
- }
- }
- //private Singleton()
- //{
- //}
- public virtual void Init()
- {
- }
- public virtual void OnDestroy()
- {
- }
- }
- public class UnitySingleton<T> : MonoBehaviour
- where T : Component
- {
- private static T _instance;
- public static T Instance
- {
- get
- {
- if(_instance == null)
- {
- _instance = FindObjectOfType(typeof(T)) as T;
- if(_instance == null)
- {
- GameObject obj = new GameObject();
- //obj.hideFlags = HideFlags.HideAndDontSave;
- _instance = (T)obj.AddComponent(typeof(T));
- }
- }
- return _instance;
- }
- }
- public virtual void Awake()
- {
- DontDestroyOnLoad(this.gameObject);
- if(_instance == null)
- {
- _instance = this as T;
- }
- }
- }
- }
|