| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using System.Reflection;
- namespace WildAttack
- {
- /// <summary>
- /// 单例基类
- /// </summary>
- /// <typeparam name="T"></typeparam>
- public class Singleton<T> : ISingletonExpand where T : Singleton<T>, new()
- {
- private static T instance;
- public static T GetInstance()
- {
- if (instance == null)
- {
- instance = new T();
- SingletonManager.singletons.Add(instance);
- }
- return instance;
- }
- public void ClearSingleton()
- {
- if (instance == this)
- {
- instance = null;
- }
- }
- }
- public interface ISingletonExpand
- {
- void ClearSingleton();
- }
- public class SingletonManager
- {
- public static HashSet<ISingletonExpand> singletons = new();
- public static void Clear()
- {
- foreach (var item in singletons)
- {
- item.ClearSingleton();
- }
- singletons.Clear();
- }
- }
- }
|