From 0b72d489d989007a827c1f8ca33248441a6e85f9 Mon Sep 17 00:00:00 2001 From: hch <305670599@qq.com> Date: 星期六, 02 八月 2025 23:06:46 +0800 Subject: [PATCH] 122 子 【武将】武将系统 / 【武将】武将系统-客户端 - 布阵 --- Main/Core/ResModule/GameObjectPoolManager.cs | 64 ++++++++++++++++++++++++++++++++ 1 files changed, 64 insertions(+), 0 deletions(-) diff --git a/Main/Core/ResModule/GameObjectPoolManager.cs b/Main/Core/ResModule/GameObjectPoolManager.cs index 5214318..942a36e 100644 --- a/Main/Core/ResModule/GameObjectPoolManager.cs +++ b/Main/Core/ResModule/GameObjectPoolManager.cs @@ -2,6 +2,11 @@ using UnityEngine; using System; using Cysharp.Threading.Tasks; +using System.Linq; + +#if UNITY_EDITOR +using UnityEngine.Profiling; +#endif public class GameObjectPoolManager : SingletonMonobehaviour<GameObjectPoolManager> @@ -10,6 +15,7 @@ private bool m_ShowDebugPanel = false; private Vector2 m_ScrollPosition = Vector2.zero; + private void OnGUI() { if (!m_ShowDebugPanel) return; @@ -19,6 +25,7 @@ m_ScrollPosition = GUILayout.BeginScrollView(m_ScrollPosition, GUILayout.Width(380), GUILayout.Height(580)); GUILayout.Label("瀵硅薄姹犺皟璇曢潰鏉�", GUILayout.Height(30)); + LogPoolMemoryUsage(); GUILayout.Space(10); foreach (var poolEntry in m_PoolDict) @@ -49,6 +56,63 @@ Instance.m_ShowDebugPanel = !Instance.m_ShowDebugPanel; } } + + public void LogPoolMemoryUsage() + { + long totalMemory = 0; + long totalFreeMemory = 0; + var prefabMemoryDict = new Dictionary<string, long>(); + + foreach (var poolEntry in m_PoolDict) + { + int prefabInstanceId = poolEntry.Key; + GameObjectPool pool = poolEntry.Value; + string prefabName = pool.Prefab.name; + long prefabMemory = 0; + long freeMemory = 0; + + // 璁$畻娲昏穬瀵硅薄鐨勫唴瀛樺崰鐢� + foreach (var gameObject in pool.m_ActiveHashSet) + { + if (gameObject != null) + { + long memory = Profiler.GetRuntimeMemorySizeLong(gameObject); + prefabMemory += memory; + } + } + + // 璁$畻绌洪棽瀵硅薄鐨勫唴瀛樺崰鐢� + foreach (var gameObject in pool.m_FreeQueue) + { + if (gameObject != null) + { + long memory = Profiler.GetRuntimeMemorySizeLong(gameObject); + prefabMemory += memory; + freeMemory += memory; + } + } + + totalMemory += prefabMemory; + totalFreeMemory += freeMemory; + prefabMemoryDict[prefabName] = prefabMemory; + } + + // 鎸夊唴瀛樺崰鐢ㄦ帓搴� + var sortedPrefabs = prefabMemoryDict.OrderByDescending(kv => kv.Value).Take(3).ToList(); + + GUILayout.Label($"鎬诲唴瀛樺崰鐢�: {totalMemory / 1024} KB", GUILayout.Height(30)); + GUILayout.Label($"绌洪棽鍐呭瓨鍗犵敤: {totalFreeMemory / 1024} KB", GUILayout.Height(30)); + GUILayout.Label("鍗犵敤鏈�楂樼殑鍓�3棰勫埗浣撳悕:", GUILayout.Height(30)); + foreach (var prefabInstance in sortedPrefabs) + { + GUILayout.BeginHorizontal("Box"); + GUILayout.Label($"{prefabInstance.Key}({prefabInstance.Value / 1024} KB)", GUILayout.Height(25)); + GUILayout.EndHorizontal(); + } + + } + + #endif // 姹犵粺璁℃暟鎹� public Dictionary<int, PoolStats> PoolStatistics { get; private set; } = new Dictionary<int, PoolStats>(); -- Gitblit v1.8.0