diff --git a/Project/Assets/Editor/ExtractMemoryEditor.cs b/Project/Assets/Editor/ExtractMemoryEditor.cs index 30705d0..e0c8040 100644 --- a/Project/Assets/Editor/ExtractMemoryEditor.cs +++ b/Project/Assets/Editor/ExtractMemoryEditor.cs @@ -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(); } diff --git a/Project/Assets/Editor/MemoryElement.cs b/Project/Assets/Editor/MemoryElement.cs index c0a46e2..4c4284b 100644 --- a/Project/Assets/Editor/MemoryElement.cs +++ b/Project/Assets/Editor/MemoryElement.cs @@ -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("children"); if (srcChildren == null) return dstMemoryElement; @@ -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); } } \ No newline at end of file diff --git a/Project/Assets/Editor/ProfilerWindow.cs b/Project/Assets/Editor/ProfilerWindow.cs index 7a37498..9016994 100644 --- a/Project/Assets/Editor/ProfilerWindow.cs +++ b/Project/Assets/Editor/ProfilerWindow.cs @@ -7,6 +7,7 @@ using UnityEditor; using UnityEditorInternal; using UnityEngine; +using UnityEngine.Profiling; public static class ProfilerWindow { @@ -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; }