RemoteDebuggerInRuntime.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. using System;
  2. using System.Text;
  3. using UnityEngine;
  4. using UnityEngine.Networking.PlayerConnection;
  5. namespace YooAsset
  6. {
  7. internal class RemoteDebuggerInRuntime : MonoBehaviour
  8. {
  9. #if UNITY_EDITOR
  10. /// <summary>
  11. /// 编辑器下获取报告的回调
  12. /// </summary>
  13. public static Action<int, DebugReport> EditorHandleDebugReportCallback;
  14. /// <summary>
  15. /// 编辑器下请求报告数据
  16. /// </summary>
  17. public static void EditorRequestDebugReport()
  18. {
  19. if(UnityEditor.EditorApplication.isPlaying)
  20. {
  21. var report = AssetSystem.GetDebugReport();
  22. EditorHandleDebugReportCallback?.Invoke(0, report);
  23. }
  24. }
  25. #else
  26. private void OnEnable()
  27. {
  28. PlayerConnection.instance.Register(RemoteDebuggerDefine.kMsgSendEditorToPlayer, OnHandleEditorMessage);
  29. }
  30. private void OnDisable()
  31. {
  32. PlayerConnection.instance.Unregister(RemoteDebuggerDefine.kMsgSendEditorToPlayer, OnHandleEditorMessage);
  33. }
  34. private void OnHandleEditorMessage(MessageEventArgs args)
  35. {
  36. var command = RemoteCommand.Deserialize(args.data);
  37. YooLogger.Log($"On handle remote command : {command.CommandType} Param : {command.CommandParam}");
  38. if (command.CommandType == (int)ERemoteCommand.SampleOnce)
  39. {
  40. var debugReport = AssetSystem.GetDebugReport();
  41. var data = DebugReport.Serialize(debugReport);
  42. PlayerConnection.instance.Send(RemoteDebuggerDefine.kMsgSendPlayerToEditor, data);
  43. }
  44. else
  45. {
  46. throw new NotImplementedException(command.CommandType.ToString());
  47. }
  48. }
  49. #endif
  50. }
  51. }