AssetBundleDebuggerWindow.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. #if UNITY_2019_4_OR_NEWER
  2. using System;
  3. using System.Collections.Generic;
  4. using UnityEditor;
  5. using UnityEngine;
  6. using UnityEngine.UIElements;
  7. using UnityEditor.UIElements;
  8. using UnityEditor.Networking.PlayerConnection;
  9. using UnityEngine.Networking.PlayerConnection;
  10. namespace YooAsset.Editor
  11. {
  12. public class AssetBundleDebuggerWindow : EditorWindow
  13. {
  14. [MenuItem("YooAsset/AssetBundle Debugger", false, 104)]
  15. public static void OpenWindow()
  16. {
  17. AssetBundleDebuggerWindow wnd = GetWindow<AssetBundleDebuggerWindow>("资源包调试工具", true, WindowsDefine.DockedWindowTypes);
  18. wnd.minSize = new Vector2(800, 600);
  19. }
  20. /// <summary>
  21. /// 视图模式
  22. /// </summary>
  23. private enum EViewMode
  24. {
  25. /// <summary>
  26. /// 内存视图
  27. /// </summary>
  28. MemoryView,
  29. /// <summary>
  30. /// 资源对象视图
  31. /// </summary>
  32. AssetView,
  33. /// <summary>
  34. /// 资源包视图
  35. /// </summary>
  36. BundleView,
  37. }
  38. private readonly Dictionary<int, RemotePlayerSession> _playerSessions = new Dictionary<int, RemotePlayerSession>();
  39. private Label _playerName;
  40. private ToolbarMenu _viewModeMenu;
  41. private SliderInt _frameSlider;
  42. private DebuggerAssetListViewer _assetListViewer;
  43. private DebuggerBundleListViewer _bundleListViewer;
  44. private EViewMode _viewMode;
  45. private string _searchKeyWord;
  46. private DebugReport _currentReport;
  47. private RemotePlayerSession _currentPlayerSession;
  48. private int _rangeIndex = 0;
  49. public void CreateGUI()
  50. {
  51. try
  52. {
  53. VisualElement root = rootVisualElement;
  54. // 加载布局文件
  55. var visualAsset = UxmlLoader.LoadWindowUXML<AssetBundleDebuggerWindow>();
  56. if (visualAsset == null)
  57. return;
  58. visualAsset.CloneTree(root);
  59. // 采样按钮
  60. var sampleBtn = root.Q<Button>("SampleButton");
  61. sampleBtn.clicked += SampleBtn_onClick;
  62. // 导出按钮
  63. var exportBtn = root.Q<Button>("ExportButton");
  64. exportBtn.clicked += ExportBtn_clicked;
  65. // 用户列表菜单
  66. _playerName = root.Q<Label>("PlayerName");
  67. _playerName.text = "Editor player";
  68. // 视口模式菜单
  69. _viewModeMenu = root.Q<ToolbarMenu>("ViewModeMenu");
  70. _viewModeMenu.menu.AppendAction(EViewMode.AssetView.ToString(), OnViewModeMenuChange, OnViewModeMenuStatusUpdate, EViewMode.AssetView);
  71. _viewModeMenu.menu.AppendAction(EViewMode.BundleView.ToString(), OnViewModeMenuChange, OnViewModeMenuStatusUpdate, EViewMode.BundleView);
  72. _viewModeMenu.text = EViewMode.AssetView.ToString();
  73. // 搜索栏
  74. var searchField = root.Q<ToolbarSearchField>("SearchField");
  75. searchField.RegisterValueChangedCallback(OnSearchKeyWordChange);
  76. // 帧数相关
  77. {
  78. _frameSlider = root.Q<SliderInt>("FrameSlider");
  79. _frameSlider.label = "Frame:";
  80. _frameSlider.highValue = 0;
  81. _frameSlider.lowValue = 0;
  82. _frameSlider.value = 0;
  83. _frameSlider.RegisterValueChangedCallback(evt =>
  84. {
  85. OnFrameSliderChange(evt.newValue);
  86. });
  87. var frameLast = root.Q<ToolbarButton>("FrameLast");
  88. frameLast.clicked += OnFrameLast_clicked;
  89. var frameNext = root.Q<ToolbarButton>("FrameNext");
  90. frameNext.clicked += OnFrameNext_clicked;
  91. var frameClear = root.Q<ToolbarButton>("FrameClear");
  92. frameClear.clicked += OnFrameClear_clicked;
  93. }
  94. // 加载视图
  95. _assetListViewer = new DebuggerAssetListViewer();
  96. _assetListViewer.InitViewer();
  97. // 加载视图
  98. _bundleListViewer = new DebuggerBundleListViewer();
  99. _bundleListViewer.InitViewer();
  100. // 显示视图
  101. _viewMode = EViewMode.AssetView;
  102. _assetListViewer.AttachParent(root);
  103. // 远程调试
  104. EditorConnection.instance.Initialize();
  105. EditorConnection.instance.RegisterConnection(OnHandleConnectionEvent);
  106. EditorConnection.instance.RegisterDisconnection(OnHandleDisconnectionEvent);
  107. EditorConnection.instance.Register(RemoteDebuggerDefine.kMsgSendPlayerToEditor, OnHandlePlayerMessage);
  108. RemoteDebuggerInRuntime.EditorHandleDebugReportCallback = OnHandleDebugReport;
  109. }
  110. catch (Exception e)
  111. {
  112. Debug.LogError(e.ToString());
  113. }
  114. }
  115. public void OnDestroy()
  116. {
  117. // 远程调试
  118. EditorConnection.instance.UnregisterConnection(OnHandleConnectionEvent);
  119. EditorConnection.instance.UnregisterDisconnection(OnHandleDisconnectionEvent);
  120. EditorConnection.instance.Unregister(RemoteDebuggerDefine.kMsgSendPlayerToEditor, OnHandlePlayerMessage);
  121. _playerSessions.Clear();
  122. }
  123. private void OnHandleConnectionEvent(int playerId)
  124. {
  125. Debug.Log($"Game player connection : {playerId}");
  126. _playerName.text = $"Connected player : {playerId}";
  127. }
  128. private void OnHandleDisconnectionEvent(int playerId)
  129. {
  130. Debug.Log($"Game player disconnection : {playerId}");
  131. _playerName.text = $"Disconneced player : {playerId}";
  132. }
  133. private void OnHandlePlayerMessage(MessageEventArgs args)
  134. {
  135. var debugReport = DebugReport.Deserialize(args.data);
  136. OnHandleDebugReport(args.playerId, debugReport);
  137. }
  138. private void OnHandleDebugReport(int playerId, DebugReport debugReport)
  139. {
  140. Debug.Log($"Handle player {playerId} debug report !");
  141. _currentPlayerSession = GetOrCreatePlayerSession(playerId);
  142. _currentPlayerSession.AddDebugReport(debugReport);
  143. _frameSlider.highValue = _currentPlayerSession.MaxRangeValue;
  144. _frameSlider.value = _currentPlayerSession.MaxRangeValue;
  145. UpdateFrameView(_currentPlayerSession);
  146. }
  147. private void OnFrameSliderChange(int sliderValue)
  148. {
  149. if (_currentPlayerSession != null)
  150. {
  151. _rangeIndex = _currentPlayerSession.ClampRangeIndex(sliderValue); ;
  152. UpdateFrameView(_currentPlayerSession, _rangeIndex);
  153. }
  154. }
  155. private void OnFrameLast_clicked()
  156. {
  157. if (_currentPlayerSession != null)
  158. {
  159. _rangeIndex = _currentPlayerSession.ClampRangeIndex(_rangeIndex - 1);
  160. _frameSlider.value = _rangeIndex;
  161. UpdateFrameView(_currentPlayerSession, _rangeIndex);
  162. }
  163. }
  164. private void OnFrameNext_clicked()
  165. {
  166. if (_currentPlayerSession != null)
  167. {
  168. _rangeIndex = _currentPlayerSession.ClampRangeIndex(_rangeIndex + 1);
  169. _frameSlider.value = _rangeIndex;
  170. UpdateFrameView(_currentPlayerSession, _rangeIndex);
  171. }
  172. }
  173. private void OnFrameClear_clicked()
  174. {
  175. if (_currentPlayerSession != null)
  176. {
  177. _frameSlider.label = $"Frame:";
  178. _frameSlider.value = 0;
  179. _frameSlider.lowValue = 0;
  180. _frameSlider.highValue = 0;
  181. _currentPlayerSession.ClearDebugReport();
  182. _assetListViewer.ClearView();
  183. _bundleListViewer.ClearView();
  184. }
  185. }
  186. private RemotePlayerSession GetOrCreatePlayerSession(int playerId)
  187. {
  188. if (_playerSessions.TryGetValue(playerId, out RemotePlayerSession session))
  189. {
  190. return session;
  191. }
  192. else
  193. {
  194. RemotePlayerSession newSession = new RemotePlayerSession(playerId);
  195. _playerSessions.Add(playerId, newSession);
  196. return newSession;
  197. }
  198. }
  199. private void UpdateFrameView(RemotePlayerSession playerSession)
  200. {
  201. if (playerSession != null)
  202. {
  203. UpdateFrameView(playerSession, playerSession.MaxRangeValue);
  204. }
  205. }
  206. private void UpdateFrameView(RemotePlayerSession playerSession, int rangeIndex)
  207. {
  208. if (playerSession == null)
  209. return;
  210. var debugReport = playerSession.GetDebugReport(rangeIndex);
  211. if (debugReport != null)
  212. {
  213. _currentReport = debugReport;
  214. _frameSlider.label = $"Frame: {debugReport.FrameCount}";
  215. _assetListViewer.FillViewData(debugReport, _searchKeyWord);
  216. _bundleListViewer.FillViewData(debugReport, _searchKeyWord);
  217. }
  218. }
  219. private void SampleBtn_onClick()
  220. {
  221. // 发送采集数据的命令
  222. RemoteCommand command = new RemoteCommand();
  223. command.CommandType = (int)ERemoteCommand.SampleOnce;
  224. command.CommandParam = string.Empty;
  225. byte[] data = RemoteCommand.Serialize(command);
  226. EditorConnection.instance.Send(RemoteDebuggerDefine.kMsgSendEditorToPlayer, data);
  227. RemoteDebuggerInRuntime.EditorRequestDebugReport();
  228. }
  229. private void ExportBtn_clicked()
  230. {
  231. if (_currentReport == null)
  232. {
  233. Debug.LogWarning("Debug report is null.");
  234. return;
  235. }
  236. string resultPath = EditorTools.OpenFolderPanel("Export JSON", "Assets/");
  237. if (resultPath != null)
  238. {
  239. // 注意:排序保证生成配置的稳定性
  240. foreach (var packageData in _currentReport.PackageDatas)
  241. {
  242. packageData.ProviderInfos.Sort();
  243. foreach (var providerInfo in packageData.ProviderInfos)
  244. {
  245. providerInfo.DependBundleInfos.Sort();
  246. }
  247. }
  248. string filePath = $"{resultPath}/{nameof(DebugReport)}_{_currentReport.FrameCount}.json";
  249. string fileContent = JsonUtility.ToJson(_currentReport, true);
  250. FileUtility.WriteAllText(filePath, fileContent);
  251. }
  252. }
  253. private void OnSearchKeyWordChange(ChangeEvent<string> e)
  254. {
  255. _searchKeyWord = e.newValue;
  256. if (_currentReport != null)
  257. {
  258. _assetListViewer.FillViewData(_currentReport, _searchKeyWord);
  259. _bundleListViewer.FillViewData(_currentReport, _searchKeyWord);
  260. }
  261. }
  262. private void OnViewModeMenuChange(DropdownMenuAction action)
  263. {
  264. var viewMode = (EViewMode)action.userData;
  265. if (_viewMode != viewMode)
  266. {
  267. _viewMode = viewMode;
  268. VisualElement root = this.rootVisualElement;
  269. _viewModeMenu.text = viewMode.ToString();
  270. if (viewMode == EViewMode.AssetView)
  271. {
  272. _assetListViewer.AttachParent(root);
  273. _bundleListViewer.DetachParent();
  274. }
  275. else if (viewMode == EViewMode.BundleView)
  276. {
  277. _assetListViewer.DetachParent();
  278. _bundleListViewer.AttachParent(root);
  279. }
  280. else
  281. {
  282. throw new NotImplementedException(viewMode.ToString());
  283. }
  284. }
  285. }
  286. private DropdownMenuAction.Status OnViewModeMenuStatusUpdate(DropdownMenuAction action)
  287. {
  288. var viewMode = (EViewMode)action.userData;
  289. if (_viewMode == viewMode)
  290. return DropdownMenuAction.Status.Checked;
  291. else
  292. return DropdownMenuAction.Status.Normal;
  293. }
  294. }
  295. }
  296. #endif