MonoSingleton.cs 595 B

12345678910111213141516171819202122232425
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public abstract class MonoSingleton<T> : MonoBehaviour where T : MonoBehaviour
  5. {
  6. private static T instance = null;
  7. public static T GetInstance()
  8. {
  9. if (null == instance)
  10. {
  11. instance = FindObjectOfType(typeof(T)) as T;
  12. if (instance == null)
  13. {
  14. instance = new GameObject(typeof(T).ToString() + "Obj", typeof(T)).GetComponent<T>();
  15. }
  16. }
  17. return instance;
  18. }
  19. public virtual void InitManager()
  20. {
  21. }
  22. }