源战役客户端
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

152 行
4.2 KiB

  1. using System.Collections.Generic;
  2. using System.ComponentModel;
  3. using System.Linq;
  4. using Assets.Editor.Treemap;
  5. using Treemap;
  6. using UnityEditor;
  7. using UnityEngine;
  8. using System;
  9. using System.Net;
  10. using NUnit.Framework.Constraints;
  11. using UnityEditor.MemoryProfiler;
  12. using Object = UnityEngine.Object;
  13. using System.IO;
  14. namespace MemoryProfilerWindow
  15. {
  16. public class MemoryProfilerWindow : EditorWindow
  17. {
  18. [NonSerialized]
  19. UnityEditor.MemoryProfiler.PackedMemorySnapshot _snapshot;
  20. [SerializeField]
  21. PackedCrawlerData _packedCrawled;
  22. [NonSerialized]
  23. CrawledMemorySnapshot _unpackedCrawl;
  24. Vector2 _scrollPosition;
  25. [NonSerialized]
  26. private bool _registered = false;
  27. public Inspector _inspector;
  28. TreeMapView _treeMapView;
  29. [MenuItem("Window/MemoryProfiler")]
  30. static void Create()
  31. {
  32. EditorWindow.GetWindow<MemoryProfilerWindow>();
  33. }
  34. [MenuItem("Window/MemoryProfilerInspect")]
  35. static void Inspect()
  36. {
  37. }
  38. public void OnDisable()
  39. {
  40. // UnityEditor.MemoryProfiler.MemorySnapshot.OnSnapshotReceived -= IncomingSnapshot;
  41. if (_treeMapView != null)
  42. _treeMapView.CleanupMeshes ();
  43. }
  44. public void Initialize()
  45. {
  46. if (_treeMapView == null)
  47. _treeMapView = new TreeMapView ();
  48. if (!_registered)
  49. {
  50. UnityEditor.MemoryProfiler.MemorySnapshot.OnSnapshotReceived += IncomingSnapshot;
  51. _registered = true;
  52. }
  53. if (_unpackedCrawl == null && _packedCrawled != null && _packedCrawled.valid)
  54. Unpack();
  55. }
  56. void OnGUI()
  57. {
  58. Initialize();
  59. GUILayout.BeginHorizontal();
  60. if (GUILayout.Button("Take Snapshot"))
  61. {
  62. UnityEditor.MemoryProfiler.MemorySnapshot.RequestNewSnapshot();
  63. }
  64. EditorGUI.BeginDisabledGroup(_snapshot == null);
  65. if (GUILayout.Button("Save Snapshot..."))
  66. {
  67. PackedMemorySnapshotUtility.SaveToFile(_snapshot);
  68. }
  69. EditorGUI.EndDisabledGroup();
  70. if (GUILayout.Button("Load Snapshot..."))
  71. {
  72. PackedMemorySnapshot packedSnapshot = PackedMemorySnapshotUtility.LoadFromFile();
  73. if(packedSnapshot != null)
  74. IncomingSnapshot(packedSnapshot);
  75. }
  76. GUILayout.EndHorizontal();
  77. if (_treeMapView != null)
  78. _treeMapView.Draw();
  79. if (_inspector != null)
  80. _inspector.Draw();
  81. //RenderDebugList();
  82. }
  83. public void SelectThing(ThingInMemory thing)
  84. {
  85. _inspector.SelectThing(thing);
  86. _treeMapView.SelectThing(thing);
  87. }
  88. public void SelectGroup(Group group)
  89. {
  90. _treeMapView.SelectGroup(group);
  91. }
  92. private void RenderDebugList()
  93. {
  94. _scrollPosition = GUILayout.BeginScrollView(_scrollPosition);
  95. foreach (var thing in _unpackedCrawl.allObjects)
  96. {
  97. var mo = thing as ManagedObject;
  98. if (mo != null)
  99. GUILayout.Label("MO: " + mo.typeDescription.name);
  100. var gch = thing as GCHandle;
  101. if (gch != null)
  102. GUILayout.Label("GCH: " + gch.caption);
  103. var sf = thing as StaticFields;
  104. if (sf != null)
  105. GUILayout.Label("SF: " + sf.typeDescription.name);
  106. }
  107. GUILayout.EndScrollView();
  108. }
  109. void Unpack()
  110. {
  111. _unpackedCrawl = CrawlDataUnpacker.Unpack(_packedCrawled);
  112. _inspector = new Inspector(this, _unpackedCrawl, _snapshot);
  113. if(_treeMapView != null)
  114. _treeMapView.Setup(this, _unpackedCrawl);
  115. }
  116. void IncomingSnapshot(PackedMemorySnapshot snapshot)
  117. {
  118. _snapshot = snapshot;
  119. _packedCrawled = new Crawler().Crawl(_snapshot);
  120. Unpack();
  121. }
  122. }
  123. }