Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion Project/Assets/Editor/ExtractMemoryEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,13 @@ public class ExtractMemoryEditor: EditorWindow
[MenuItem("Window/Extract Profiler Memory")]
public static void ShowWindow()
{
#if UNITY_2018_3_OR_NEWER
EditorApplication.ExecuteMenuItem("Window/Analysis/Profiler");
#else
EditorApplication.ExecuteMenuItem("Window/Profiler");
if (Window == null)
#endif

if(Window == null)
{
Window = CreateInstance<ExtractMemoryEditor>();
}
Expand Down
32 changes: 19 additions & 13 deletions Project/Assets/Editor/MemoryElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ public static MemoryElement Create(Dynamic srcMemoryElement, int depth, int filt
var dstMemoryElement = new MemoryElement { _depth = depth };
Dynamic.CopyFrom(dstMemoryElement, srcMemoryElement.InnerObject,
BindingFlags.Instance | BindingFlags.Public | BindingFlags.GetField);
if (dstMemoryElement.name == null)
{
dstMemoryElement.name = "";
}

var srcChildren = srcMemoryElement.PublicInstanceField<IList>("children");
if (srcChildren == null) return dstMemoryElement;
Expand All @@ -44,28 +48,30 @@ public static MemoryElement Create(Dynamic srcMemoryElement, int depth, int filt

public override string ToString()
{
var text = string.IsNullOrEmpty(name) ? "-" : name;
var text2 = "KB";
var num = totalMemory / 1024f;
if (num > 512f)
var displayName = string.IsNullOrEmpty(name) ? "-" : name;
string numText = null;
if (totalMemory < 1024)
{
num /= 1024f;
text2 = "MB";
numText = totalMemory.ToString() + "B";
}

var resultString = string.Format(new string('\t', _depth) + " {0}, {1}{2}", text, num, text2);
return resultString;
else if(totalMemory < 1024 * 1024)
{
numText = (totalMemory / 1024f).ToString("F2") + "KB";
}
else
{
numText = (totalMemory / (1024 * 1024f)).ToString("F2") + "MB";
}
return $"{new string('\t', _depth)}{displayName}, {numText}";
}

public int CompareTo(MemoryElement other)
{
if (other.totalMemory != totalMemory)
{
return (int) (other.totalMemory - totalMemory);
return other.totalMemory.CompareTo(totalMemory);
}

if (string.IsNullOrEmpty(name)) return -1;
return !string.IsNullOrEmpty(other.name) ? string.Compare(name, other.name, StringComparison.Ordinal) : 1;

return other.name.CompareTo(name);
}
}
13 changes: 12 additions & 1 deletion Project/Assets/Editor/ProfilerWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using UnityEditor;
using UnityEditorInternal;
using UnityEngine;
using UnityEngine.Profiling;

public static class ProfilerWindow
{
Expand Down Expand Up @@ -40,7 +41,17 @@ public static MemoryElement GetMemoryDetailRoot(int filterDepth, float filterSiz
{
var windowDynamic = _GetWindow(ProfilerArea.Memory);
if (windowDynamic == null) return null;
var listViewDynamic = new Dynamic(windowDynamic.PrivateInstanceField("m_MemoryListView"));
Dynamic listViewDynamic = null;

#if UNITY_2018_3_OR_NEWER
var modules = new Dynamic(windowDynamic.PrivateInstanceField("m_ProfilerModules"));
var enumerable = modules.InnerObject as IList;
var memModule = enumerable[3];
var memModuleDyc = new Dynamic(memModule);
listViewDynamic = new Dynamic(memModuleDyc.PrivateInstanceField("m_MemoryListView"));
#else
listViewDynamic = new Dynamic(windowDynamic.PrivateInstanceField("m_MemoryListView"));
#endif
var rootDynamic = listViewDynamic.PrivateInstanceField("m_Root");
return rootDynamic != null ? MemoryElement.Create(new Dynamic(rootDynamic), 0, filterDepth, filterSize) : null;
}
Expand Down