AssetBundleDebuggerWindow.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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 ShowExample()
  16. {
  17. AssetBundleDebuggerWindow wnd = GetWindow<AssetBundleDebuggerWindow>("资源包调试工具", true, EditorDefine.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 = EditorHelper.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. _playerName = root.Q<Label>("PlayerName");
  64. _playerName.text = "Editor player";
  65. // 视口模式菜单
  66. _viewModeMenu = root.Q<ToolbarMenu>("ViewModeMenu");
  67. _viewModeMenu.menu.AppendAction(EViewMode.AssetView.ToString(), OnViewModeMenuChange, OnViewModeMenuStatusUpdate, EViewMode.AssetView);
  68. _viewModeMenu.menu.AppendAction(EViewMode.BundleView.ToString(), OnViewModeMenuChange, OnViewModeMenuStatusUpdate, EViewMode.BundleView);
  69. _viewModeMenu.text = EViewMode.AssetView.ToString();
  70. // 搜索栏
  71. var searchField = root.Q<ToolbarSearchField>("SearchField");
  72. searchField.RegisterValueChangedCallback(OnSearchKeyWordChange);
  73. // 帧数相关
  74. {
  75. _frameSlider = root.Q<SliderInt>("FrameSlider");
  76. _frameSlider.label = "Frame:";
  77. _frameSlider.highValue = 0;
  78. _frameSlider.lowValue = 0;
  79. _frameSlider.value = 0;
  80. _frameSlider.RegisterValueChangedCallback(evt =>
  81. {
  82. OnFrameSliderChange(evt.newValue);
  83. });
  84. var frameLast = root.Q<ToolbarButton>("FrameLast");
  85. frameLast.clicked += OnFrameLast_clicked;
  86. var frameNext = root.Q<ToolbarButton>("FrameNext");
  87. frameNext.clicked += OnFrameNext_clicked;
  88. var frameClear = root.Q<ToolbarButton>("FrameClear");
  89. frameClear.clicked += OnFrameClear_clicked;
  90. }
  91. // 加载视图
  92. _assetListViewer = new DebuggerAssetListViewer();
  93. _assetListViewer.InitViewer();
  94. // 加载视图
  95. _bundleListViewer = new DebuggerBundleListViewer();
  96. _bundleListViewer.InitViewer();
  97. // 显示视图
  98. _viewMode = EViewMode.AssetView;
  99. _assetListViewer.AttachParent(root);
  100. // 远程调试
  101. EditorConnection.instance.Initialize();
  102. EditorConnection.instance.RegisterConnection(OnHandleConnectionEvent);
  103. EditorConnection.instance.RegisterDisconnection(OnHandleDisconnectionEvent);
  104. EditorConnection.instance.Register(RemoteDebuggerDefine.kMsgSendPlayerToEditor, OnHandlePlayerMessage);
  105. RemoteDebuggerInRuntime.EditorHandleDebugReportCallback = OnHandleDebugReport;
  106. }
  107. catch (Exception e)
  108. {
  109. Debug.LogError(e.ToString());
  110. }
  111. }
  112. public void OnDestroy()
  113. {
  114. // 远程调试
  115. EditorConnection.instance.UnregisterConnection(OnHandleConnectionEvent);
  116. EditorConnection.instance.UnregisterDisconnection(OnHandleDisconnectionEvent);
  117. EditorConnection.instance.Unregister(RemoteDebuggerDefine.kMsgSendPlayerToEditor, OnHandlePlayerMessage);
  118. _playerSessions.Clear();
  119. }
  120. private void OnHandleConnectionEvent(int playerId)
  121. {
  122. Debug.Log($"Game player connection : {playerId}");
  123. _playerName.text = $"Connected player : {playerId}";
  124. }
  125. private void OnHandleDisconnectionEvent(int playerId)
  126. {
  127. Debug.Log($"Game player disconnection : {playerId}");
  128. _playerName.text = $"Disconneced player : {playerId}";
  129. }
  130. private void OnHandlePlayerMessage(MessageEventArgs args)
  131. {
  132. var debugReport = DebugReport.Deserialize(args.data);
  133. OnHandleDebugReport(args.playerId, debugReport);
  134. }
  135. private void OnHandleDebugReport(int playerId, DebugReport debugReport)
  136. {
  137. Debug.Log($"Handle player {playerId} debug report !");
  138. _currentPlayerSession = GetOrCreatePlayerSession(playerId);
  139. _currentPlayerSession.AddDebugReport(debugReport);
  140. _frameSlider.highValue = _currentPlayerSession.MaxRangeValue;
  141. _frameSlider.value = _currentPlayerSession.MaxRangeValue;
  142. UpdateFrameView(_currentPlayerSession);
  143. }
  144. private void OnFrameSliderChange(int sliderValue)
  145. {
  146. if (_currentPlayerSession != null)
  147. {
  148. _rangeIndex = _currentPlayerSession.ClampRangeIndex(sliderValue); ;
  149. UpdateFrameView(_currentPlayerSession, _rangeIndex);
  150. }
  151. }
  152. private void OnFrameLast_clicked()
  153. {
  154. if (_currentPlayerSession != null)
  155. {
  156. _rangeIndex = _currentPlayerSession.ClampRangeIndex(_rangeIndex - 1);
  157. _frameSlider.value = _rangeIndex;
  158. UpdateFrameView(_currentPlayerSession, _rangeIndex);
  159. }
  160. }
  161. private void OnFrameNext_clicked()
  162. {
  163. if (_currentPlayerSession != null)
  164. {
  165. _rangeIndex = _currentPlayerSession.ClampRangeIndex(_rangeIndex + 1);
  166. _frameSlider.value = _rangeIndex;
  167. UpdateFrameView(_currentPlayerSession, _rangeIndex);
  168. }
  169. }
  170. private void OnFrameClear_clicked()
  171. {
  172. if (_currentPlayerSession != null)
  173. {
  174. _frameSlider.label = $"Frame:";
  175. _frameSlider.value = 0;
  176. _frameSlider.lowValue = 0;
  177. _frameSlider.highValue = 0;
  178. _currentPlayerSession.ClearDebugReport();
  179. _assetListViewer.ClearView();
  180. _bundleListViewer.ClearView();
  181. }
  182. }
  183. private RemotePlayerSession GetOrCreatePlayerSession(int playerId)
  184. {
  185. if (_playerSessions.TryGetValue(playerId, out RemotePlayerSession session))
  186. {
  187. return session;
  188. }
  189. else
  190. {
  191. RemotePlayerSession newSession = new RemotePlayerSession(playerId);
  192. _playerSessions.Add(playerId, newSession);
  193. return newSession;
  194. }
  195. }
  196. private void UpdateFrameView(RemotePlayerSession playerSession)
  197. {
  198. if (playerSession != null)
  199. {
  200. UpdateFrameView(playerSession, playerSession.MaxRangeValue);
  201. }
  202. }
  203. private void UpdateFrameView(RemotePlayerSession playerSession, int rangeIndex)
  204. {
  205. if (playerSession == null)
  206. return;
  207. var debugReport = playerSession.GetDebugReport(rangeIndex);
  208. if (debugReport != null)
  209. {
  210. _currentReport = debugReport;
  211. _frameSlider.label = $"Frame: {debugReport.FrameCount}";
  212. _assetListViewer.FillViewData(debugReport, _searchKeyWord);
  213. _bundleListViewer.FillViewData(debugReport, _searchKeyWord);
  214. }
  215. }
  216. private void SampleBtn_onClick()
  217. {
  218. // 发送采集数据的命令
  219. RemoteCommand command = new RemoteCommand();
  220. command.CommandType = (int)ERemoteCommand.SampleOnce;
  221. command.CommandParam = string.Empty;
  222. byte[] data = RemoteCommand.Serialize(command);
  223. EditorConnection.instance.Send(RemoteDebuggerDefine.kMsgSendEditorToPlayer, data);
  224. RemoteDebuggerInRuntime.EditorRequestDebugReport();
  225. }
  226. private void OnSearchKeyWordChange(ChangeEvent<string> e)
  227. {
  228. _searchKeyWord = e.newValue;
  229. if (_currentReport != null)
  230. {
  231. _assetListViewer.FillViewData(_currentReport, _searchKeyWord);
  232. _bundleListViewer.FillViewData(_currentReport, _searchKeyWord);
  233. }
  234. }
  235. private void OnViewModeMenuChange(DropdownMenuAction action)
  236. {
  237. var viewMode = (EViewMode)action.userData;
  238. if (_viewMode != viewMode)
  239. {
  240. _viewMode = viewMode;
  241. VisualElement root = this.rootVisualElement;
  242. _viewModeMenu.text = viewMode.ToString();
  243. if (viewMode == EViewMode.AssetView)
  244. {
  245. _assetListViewer.AttachParent(root);
  246. _bundleListViewer.DetachParent();
  247. }
  248. else if (viewMode == EViewMode.BundleView)
  249. {
  250. _assetListViewer.DetachParent();
  251. _bundleListViewer.AttachParent(root);
  252. }
  253. else
  254. {
  255. throw new NotImplementedException(viewMode.ToString());
  256. }
  257. }
  258. }
  259. private DropdownMenuAction.Status OnViewModeMenuStatusUpdate(DropdownMenuAction action)
  260. {
  261. var viewMode = (EViewMode)action.userData;
  262. if (_viewMode == viewMode)
  263. return DropdownMenuAction.Status.Checked;
  264. else
  265. return DropdownMenuAction.Status.Normal;
  266. }
  267. }
  268. }
  269. #endif