初始化
This commit is contained in:
6
UnityGame/Assets/Scripts/ThirdParty/YIUIFramework/Plugins/YIUISingleton/Code/ISingleton.cs
vendored
Normal file
6
UnityGame/Assets/Scripts/ThirdParty/YIUIFramework/Plugins/YIUISingleton/Code/ISingleton.cs
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
namespace YIUIFramework
|
||||
{
|
||||
public interface ISingleton : IDisposer
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7dc01c9c44f340d888617bce800f46a4
|
||||
timeCreated: 1689062128
|
||||
87
UnityGame/Assets/Scripts/ThirdParty/YIUIFramework/Plugins/YIUISingleton/Code/SingletonMgr.cs
vendored
Normal file
87
UnityGame/Assets/Scripts/ThirdParty/YIUIFramework/Plugins/YIUISingleton/Code/SingletonMgr.cs
vendored
Normal file
@@ -0,0 +1,87 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace YIUIFramework
|
||||
{
|
||||
/// <summary>
|
||||
/// 所有继承了ISingleton的任意类型单例
|
||||
/// 主要目的为统计
|
||||
/// 不要轻易调用Dispose 一键释放所有单例
|
||||
/// </summary>
|
||||
public static class SingletonMgr
|
||||
{
|
||||
private static List<ISingleton> g_Singles = new List<ISingleton>();
|
||||
|
||||
public static bool Disposing { get; private set; } = true;
|
||||
|
||||
public static int Count => g_Singles.Count;
|
||||
|
||||
public static bool IsQuitting { get; private set; }
|
||||
|
||||
static SingletonMgr()
|
||||
{
|
||||
Application.quitting -= OnQuitting;
|
||||
Application.quitting += OnQuitting;
|
||||
}
|
||||
|
||||
private static void OnQuitting()
|
||||
{
|
||||
//Debug.LogError("OnQuitting");
|
||||
IsQuitting = true;
|
||||
}
|
||||
|
||||
//只能由一个地方 真的需要彻底清除时调用
|
||||
//游戏退出时不必调用 因为游戏都退了 所有都会被清空
|
||||
//需要调用的时机 如不退出游戏 但是要重置全部的情况下使用
|
||||
//一般情况是不需要使用的
|
||||
public static void Dispose()
|
||||
{
|
||||
if (IsQuitting)
|
||||
{
|
||||
//Debug.Log("正在退出游戏 不必清理");
|
||||
return;
|
||||
}
|
||||
|
||||
Disposing = true;
|
||||
|
||||
//Debug.Log($"SingletonMgr.清除所有单例");
|
||||
var singles = g_Singles.ToArray();
|
||||
for (int i = 0; i < singles.Length; i++)
|
||||
{
|
||||
var inst = singles[i];
|
||||
|
||||
if (inst == null || inst.Disposed) continue;
|
||||
|
||||
inst.Dispose();
|
||||
singles[i] = null;
|
||||
}
|
||||
|
||||
g_Singles.Clear();
|
||||
}
|
||||
|
||||
//初始化
|
||||
public static void Initialize()
|
||||
{
|
||||
if (IsQuitting)
|
||||
{
|
||||
Debug.Log("正在退出游戏 禁止初始化");
|
||||
return;
|
||||
}
|
||||
|
||||
//Debug.Log($"SingletonMgr.初始化");
|
||||
Disposing = false;
|
||||
}
|
||||
|
||||
internal static void Add(ISingleton single)
|
||||
{
|
||||
//Debug.Log($"添加{single.GetType().Name}");
|
||||
g_Singles.Add(single);
|
||||
}
|
||||
|
||||
internal static void Remove(ISingleton single)
|
||||
{
|
||||
//Debug.Log($"移除{single.GetType().Name}");
|
||||
g_Singles.Remove(single);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9bb35a2b97b0444e93a55003422d212d
|
||||
timeCreated: 1688982499
|
||||
Reference in New Issue
Block a user