RemoteDebuggerInRuntime.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
  11. private static void OnRuntimeInitialize()
  12. {
  13. _sampleOnce = false;
  14. _autoSample = false;
  15. }
  16. #endif
  17. private static bool _sampleOnce = false;
  18. private static bool _autoSample = false;
  19. private void Awake()
  20. {
  21. #if UNITY_EDITOR
  22. RemotePlayerConnection.Instance.Initialize();
  23. #endif
  24. }
  25. private void OnEnable()
  26. {
  27. #if UNITY_EDITOR
  28. RemotePlayerConnection.Instance.Register(RemoteDebuggerDefine.kMsgEditorSendToPlayer, OnHandleEditorMessage);
  29. #else
  30. PlayerConnection.instance.Register(RemoteDebuggerDefine.kMsgEditorSendToPlayer, OnHandleEditorMessage);
  31. #endif
  32. }
  33. private void OnDisable()
  34. {
  35. #if UNITY_EDITOR
  36. RemotePlayerConnection.Instance.Unregister(RemoteDebuggerDefine.kMsgEditorSendToPlayer);
  37. #else
  38. PlayerConnection.instance.Unregister(RemoteDebuggerDefine.kMsgEditorSendToPlayer, OnHandleEditorMessage);
  39. #endif
  40. }
  41. private void LateUpdate()
  42. {
  43. if (_autoSample || _sampleOnce)
  44. {
  45. _sampleOnce = false;
  46. var debugReport = YooAssets.GetDebugReport();
  47. var data = DebugReport.Serialize(debugReport);
  48. #if UNITY_EDITOR
  49. RemotePlayerConnection.Instance.Send(RemoteDebuggerDefine.kMsgPlayerSendToEditor, data);
  50. #else
  51. PlayerConnection.instance.Send(RemoteDebuggerDefine.kMsgPlayerSendToEditor, data);
  52. #endif
  53. }
  54. }
  55. private static void OnHandleEditorMessage(MessageEventArgs args)
  56. {
  57. var command = RemoteCommand.Deserialize(args.data);
  58. YooLogger.Log($"On handle remote command : {command.CommandType} Param : {command.CommandParam}");
  59. if (command.CommandType == (int)ERemoteCommand.SampleOnce)
  60. {
  61. _sampleOnce = true;
  62. }
  63. else if (command.CommandType == (int)ERemoteCommand.SampleAuto)
  64. {
  65. if (command.CommandParam == "open")
  66. _autoSample = true;
  67. else
  68. _autoSample = false;
  69. }
  70. else
  71. {
  72. throw new NotImplementedException(command.CommandType.ToString());
  73. }
  74. }
  75. }
  76. }