Created
April 7, 2020 13:51
-
-
Save joonjoonjoon/0c5a37dc692dbeb5cbea2a6264df8db6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using UnityEngine; | |
using UnityEditor; | |
using UnityEngine.Profiling; | |
class SimpleProfilerWindow : EditorWindow | |
{ | |
const int sampleCount = 100; // width of the texture and also sample buffer size | |
const int height = 30; // height of the texture | |
const float delay = 0.5f; | |
Texture2D _waveform; | |
static FixedSizedListBuffer<long> samples; | |
Color32[] _clear; | |
float _counter; | |
[MenuItem("Joon/Profiling/Show Simple Profiler Window")] | |
public static void ShowWindow() | |
{ | |
var window = (SimpleProfilerWindow) GetWindow(typeof(SimpleProfilerWindow)); | |
window.titleContent.text = "Memory Profiler"; | |
window.Show(); | |
} | |
void OnEnable() | |
{ | |
if (_waveform != null) DestroyImmediate(_waveform); | |
Repaint(); | |
} | |
void OnGUI() | |
{ | |
if (_waveform == null) | |
{ | |
_waveform = new Texture2D(sampleCount, height, TextureFormat.RGBA32, false); | |
_waveform.filterMode = FilterMode.Point; | |
if(samples == null) samples = new FixedSizedListBuffer<long>(sampleCount); | |
} | |
if (_clear == null) | |
{ | |
_clear = new Color32[_waveform.GetPixels32().Length]; | |
for (int i = 0; i < _clear.Length; i++) | |
{ | |
_clear[i] = new Color32(200,200,200,255); | |
} | |
} | |
long max = 0; | |
long last = 0; | |
if (samples.Count > 0) | |
{ | |
max = samples.Max(); | |
last = samples.Last(); | |
} | |
_waveform.SetPixels32(_clear); | |
for (int i = 0; i < samples.Count; i++) | |
{ | |
var y = (int) (samples[i] / (max *1.2f) * height); | |
for (int j = 0; j < y; j++) | |
{ | |
_waveform.SetPixel(i,j, Color.white); | |
} | |
} | |
_waveform.Apply(); | |
EditorGUI.DrawPreviewTexture(new Rect(0, 0, (int) position.width, (int) position.height), _waveform); | |
GUILayout.Label("current: " + FormatBytes(last)); | |
GUILayout.Label("max: " + FormatBytes(max)); | |
} | |
void Update() | |
{ | |
if (_counter + delay < Time.realtimeSinceStartup) | |
{ | |
_counter = Time.realtimeSinceStartup; | |
if(samples == null) samples = new FixedSizedListBuffer<long>(sampleCount); | |
samples.Add(GetCurrentValue()); | |
Repaint(); | |
} | |
} | |
// https://stackoverflow.com/questions/1242266/converting-bytes-to-gb-in-c/25395808#25395808 | |
private static string FormatBytes(long bytes) | |
{ | |
string[] Suffix = { "B", "KB", "MB", "GB", "TB" }; | |
int i; | |
double dblSByte = bytes; | |
for (i = 0; i < Suffix.Length && bytes >= 1024; i++, bytes /= 1024) | |
{ | |
dblSByte = bytes / 1024.0; | |
} | |
return String.Format("{0:0.##} {1}", dblSByte, Suffix[i]); | |
} | |
long GetCurrentValue() | |
{ | |
return Profiler.GetAllocatedMemoryForGraphicsDriver(); | |
} | |
public class FixedSizedListBuffer<T> : List<T> | |
{ | |
public int size { get; private set; } | |
public FixedSizedListBuffer(int size) | |
{ | |
this.size = size; | |
} | |
public new void Add(T obj) | |
{ | |
base.Add(obj); | |
if(Count > size) RemoveAt(0); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment