3 using System.Collections.Generic;
5 namespace HKI.Core.Debuging
13 [SerializeField] Color32 GraphColor =
new Color(0.2f, 0.6f, 1.0f, 1.0f);
14 [SerializeField] Text TotalMemoryUsageText = null;
15 [SerializeField] Text MonoMemoryUsageText = null;
17 [SerializeField] RawImage GraphImage = null;
19 [SerializeField]
int GraphWidth = 200;
20 [SerializeField]
int GraphHeight = 50;
23 const string format =
"n2";
24 const float mb = 1024.0f * 1024.0f;
26 Color[] cleanGraph = null;
27 Texture2D graphTexture = null;
28 List<int> memoryGraphBarsVal = null;
30 int graphDataPoints = 200;
35 graphTexture =
new Texture2D(GraphWidth, GraphHeight);
36 graphTexture.wrapMode = TextureWrapMode.Clamp;
37 graphTexture.filterMode = FilterMode.Point;
41 cleanGraph =
new Color[GraphWidth * GraphHeight];
43 graphDataPoints = GraphWidth / 2;
44 memoryGraphBarsVal =
new List<int>(graphDataPoints);
46 for (
int i = 0; i < graphDataPoints; i++)
47 memoryGraphBarsVal.Add(0);
49 graphTexture =
new Texture2D(GraphWidth, GraphHeight);
51 Color emptyColor =
new Color(0, 0, 0, 0);
53 for (
int i = 0; i < GraphWidth; i++)
55 for (
int j = 0; j < GraphHeight; j++)
56 cleanGraph[i * GraphHeight + j] = emptyColor;
59 graphTexture.SetPixels(cleanGraph);
61 GraphImage.texture = graphTexture;
63 if (TotalMemoryUsageText != null && MonoMemoryUsageText != null)
65 TotalMemoryUsageText.text = (((float)
UnityEngine.Profiling.Profiler.GetTotalAllocatedMemoryLong()) / mb).ToString(format);
66 MonoMemoryUsageText.text = (((float)
UnityEngine.Profiling.Profiler.GetMonoUsedSizeLong()) / mb).ToString(format);
73 memoryGraphBarsVal.Add(Mathf.RoundToInt(
UnityEngine.Profiling.Profiler.GetTotalAllocatedMemoryLong() / mb));
74 memoryGraphBarsVal.RemoveAt(0);
77 for (
int i = 0; i < memoryGraphBarsVal.Count; i++)
79 if (memoryGraphBarsVal[i] > max)
80 max = memoryGraphBarsVal[i];
83 graphTexture.SetPixels(cleanGraph);
85 for (
int i = 0; i < GraphWidth; i += 2)
87 for (
int j = 0; j < (int)((memoryGraphBarsVal[i / 2] / max) * GraphHeight); j++)
89 graphTexture.SetPixel(i, j, GraphColor);
90 graphTexture.SetPixel(i + 1, j, GraphColor);
94 graphTexture.wrapMode = TextureWrapMode.Clamp;
95 graphTexture.filterMode = FilterMode.Point;
99 if (TotalMemoryUsageText != null && MonoMemoryUsageText != null)
101 TotalMemoryUsageText.text = (((float)
UnityEngine.Profiling.Profiler.GetTotalAllocatedMemoryLong()) / mb).ToString(format);
102 MonoMemoryUsageText.text = (((float)
UnityEngine.Profiling.Profiler.GetMonoUsedSizeLong()) / mb).ToString(format);
This MonoBehaviour fetches the memory info from Unity and updates the UI with this information...