HKI Core
MemoryInfo.cs
Go to the documentation of this file.
1 using UnityEngine;
2 using UnityEngine.UI;
3 using System.Collections.Generic;
4 
5 namespace HKI.Core.Debuging
6 {
10  public class MemoryInfo : MonoBehaviour
11  {
12  // Public variables
13  [SerializeField] Color32 GraphColor = new Color(0.2f, 0.6f, 1.0f, 1.0f);
14  [SerializeField] Text TotalMemoryUsageText = null;
15  [SerializeField] Text MonoMemoryUsageText = null;
16 
17  [SerializeField] RawImage GraphImage = null;
18 
19  [SerializeField] int GraphWidth = 200;
20  [SerializeField] int GraphHeight = 50;
21 
22  // private variables
23  const string format = "n2";
24  const float mb = 1024.0f * 1024.0f;
25 
26  Color[] cleanGraph = null;
27  Texture2D graphTexture = null;
28  List<int> memoryGraphBarsVal = null;
29 
30  int graphDataPoints = 200;
31 
32  // Awake function
33  void Awake()
34  {
35  graphTexture = new Texture2D(GraphWidth, GraphHeight);
36  graphTexture.wrapMode = TextureWrapMode.Clamp;
37  graphTexture.filterMode = FilterMode.Point;
38 
39  graphTexture.Apply();
40 
41  cleanGraph = new Color[GraphWidth * GraphHeight];
42 
43  graphDataPoints = GraphWidth / 2;
44  memoryGraphBarsVal = new List<int>(graphDataPoints);
45 
46  for (int i = 0; i < graphDataPoints; i++)
47  memoryGraphBarsVal.Add(0);
48 
49  graphTexture = new Texture2D(GraphWidth, GraphHeight);
50 
51  Color emptyColor = new Color(0, 0, 0, 0);
52 
53  for (int i = 0; i < GraphWidth; i++)
54  {
55  for (int j = 0; j < GraphHeight; j++)
56  cleanGraph[i * GraphHeight + j] = emptyColor;
57  }
58 
59  graphTexture.SetPixels(cleanGraph);
60  graphTexture.Apply();
61  GraphImage.texture = graphTexture;
62 
63  if (TotalMemoryUsageText != null && MonoMemoryUsageText != null)
64  {
65  TotalMemoryUsageText.text = (((float)UnityEngine.Profiling.Profiler.GetTotalAllocatedMemoryLong()) / mb).ToString(format);
66  MonoMemoryUsageText.text = (((float)UnityEngine.Profiling.Profiler.GetMonoUsedSizeLong()) / mb).ToString(format);
67  }
68  }
69 
70  // Interface function
71  public void UpdateInfo()
72  {
73  memoryGraphBarsVal.Add(Mathf.RoundToInt(UnityEngine.Profiling.Profiler.GetTotalAllocatedMemoryLong() / mb));
74  memoryGraphBarsVal.RemoveAt(0);
75 
76  float max = 100;
77  for (int i = 0; i < memoryGraphBarsVal.Count; i++)
78  {
79  if (memoryGraphBarsVal[i] > max)
80  max = memoryGraphBarsVal[i];
81  }
82 
83  graphTexture.SetPixels(cleanGraph);
84 
85  for (int i = 0; i < GraphWidth; i += 2)
86  {
87  for (int j = 0; j < (int)((memoryGraphBarsVal[i / 2] / max) * GraphHeight); j++)
88  {
89  graphTexture.SetPixel(i, j, GraphColor);
90  graphTexture.SetPixel(i + 1, j, GraphColor);
91  }
92  }
93 
94  graphTexture.wrapMode = TextureWrapMode.Clamp;
95  graphTexture.filterMode = FilterMode.Point;
96  graphTexture.Apply();
97 
98 
99  if (TotalMemoryUsageText != null && MonoMemoryUsageText != null)
100  {
101  TotalMemoryUsageText.text = (((float)UnityEngine.Profiling.Profiler.GetTotalAllocatedMemoryLong()) / mb).ToString(format);
102  MonoMemoryUsageText.text = (((float)UnityEngine.Profiling.Profiler.GetMonoUsedSizeLong()) / mb).ToString(format);
103  }
104  }
105  }
106 }
This MonoBehaviour fetches the memory info from Unity and updates the UI with this information...
Definition: MemoryInfo.cs:10