ARRemoteEditor.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. using UnityEngine;
  2. #if UNITY_EDITOR
  3. using UnityEngine.Networking.PlayerConnection;
  4. using UnityEditor.Networking.PlayerConnection;
  5. #endif
  6. namespace UnityARInterface
  7. {
  8. public class ARRemoteEditor : ARController
  9. {
  10. [SerializeField]
  11. private bool m_SendVideo;
  12. #if UNITY_EDITOR
  13. public bool sendVideo
  14. {
  15. get { return m_SendVideo; }
  16. set
  17. {
  18. m_SendVideo = value;
  19. if (m_RemoteInterface != null)
  20. m_RemoteInterface.sendVideo = value;
  21. }
  22. }
  23. public override bool BackgroundRendering
  24. {
  25. get { return base.BackgroundRendering; }
  26. set
  27. {
  28. if (m_RemoteInterface != null)
  29. {
  30. m_RemoteInterface.BackgroundRendering = base.BackgroundRendering = value;
  31. }
  32. }
  33. }
  34. ARRemoteEditorInterface m_RemoteInterface;
  35. EditorConnection m_EditorConnection;
  36. void SetConnection(EditorConnection editorConnection)
  37. {
  38. m_EditorConnection = editorConnection;
  39. if (m_RemoteInterface != null)
  40. m_RemoteInterface.editorConnection = editorConnection;
  41. }
  42. private void DestroyConnection()
  43. {
  44. // Disconnect all players and destroy the connection object
  45. var connection = EditorConnection.instance;
  46. connection.DisconnectAll();
  47. connection.Unregister(ARMessageIds.frame, FrameMessageHandler);
  48. connection.Unregister(ARMessageIds.addPlane, PlaneAddedMessageHandler);
  49. connection.Unregister(ARMessageIds.updatePlane, PlaneUpdatedMessageHandler);
  50. connection.Unregister(ARMessageIds.removePlane, PlaneRemovedMessageHandler);
  51. connection.Unregister(ARMessageIds.screenCaptureY, ScreenCaptureYMessageHandler);
  52. connection.Unregister(ARMessageIds.screenCaptureUV, ScreenCaptureUVMessageHandler);
  53. connection.Unregister(ARMessageIds.screenCaptureParams, ScreenCaptureParamsMessageHandler);
  54. connection.Unregister(ARMessageIds.pointCloud, PointCloudMessageHandler);
  55. connection.Unregister(ARMessageIds.lightEstimate, LightEstimateMessageHandler);
  56. DestroyImmediate(connection);
  57. SetConnection(null);
  58. }
  59. private void SetupConnection()
  60. {
  61. // This forces a the connection to be reconstructed
  62. SetConnection(EditorConnection.instance);
  63. m_EditorConnection.Initialize();
  64. m_EditorConnection.RegisterConnection(PlayerConnectedEventHandler);
  65. m_EditorConnection.RegisterDisconnection(PlayerDisconnectedEventHandler);
  66. // These EditorConnection callbacks can only be on UnityEngine.Objects
  67. // so register them here and just forward all messages to the ARRemoteEditorInterface
  68. m_EditorConnection.Register(ARMessageIds.frame, FrameMessageHandler);
  69. m_EditorConnection.Register(ARMessageIds.addPlane, PlaneAddedMessageHandler);
  70. m_EditorConnection.Register(ARMessageIds.updatePlane, PlaneUpdatedMessageHandler);
  71. m_EditorConnection.Register(ARMessageIds.removePlane, PlaneRemovedMessageHandler);
  72. m_EditorConnection.Register(ARMessageIds.screenCaptureY, ScreenCaptureYMessageHandler);
  73. m_EditorConnection.Register(ARMessageIds.screenCaptureUV, ScreenCaptureUVMessageHandler);
  74. m_EditorConnection.Register(ARMessageIds.screenCaptureParams, ScreenCaptureParamsMessageHandler);
  75. m_EditorConnection.Register(ARMessageIds.pointCloud, PointCloudMessageHandler);
  76. m_EditorConnection.Register(ARMessageIds.lightEstimate, LightEstimateMessageHandler);
  77. }
  78. private void Start()
  79. {
  80. SetupConnection();
  81. }
  82. void ScreenCaptureParamsMessageHandler(MessageEventArgs message)
  83. {
  84. m_RemoteInterface.ScreenCaptureParamsMessageHandler(message);
  85. }
  86. void ScreenCaptureUVMessageHandler(MessageEventArgs message)
  87. {
  88. m_RemoteInterface.ScreenCaptureUVMessageHandler(message);
  89. }
  90. void ScreenCaptureYMessageHandler(MessageEventArgs message)
  91. {
  92. m_RemoteInterface.ScreenCaptureYMessageHandler(message);
  93. }
  94. void PlaneAddedMessageHandler(MessageEventArgs message)
  95. {
  96. m_RemoteInterface.PlaneAddedMessageHandler(message);
  97. }
  98. void PlaneUpdatedMessageHandler(MessageEventArgs message)
  99. {
  100. m_RemoteInterface.PlaneUpdatedMessageHandler(message);
  101. }
  102. void PlaneRemovedMessageHandler(MessageEventArgs message)
  103. {
  104. m_RemoteInterface.PlaneRemovedMessageHandler(message);
  105. }
  106. void FrameMessageHandler(MessageEventArgs message)
  107. {
  108. m_RemoteInterface.FrameMessageHandler(message);
  109. }
  110. void PointCloudMessageHandler(MessageEventArgs message)
  111. {
  112. m_RemoteInterface.PointCloudMessageHandler(message);
  113. }
  114. void LightEstimateMessageHandler(MessageEventArgs message)
  115. {
  116. m_RemoteInterface.LightEstimateMessageHandler(message);
  117. }
  118. void PlayerConnectedEventHandler(int playerId)
  119. {
  120. m_RemoteInterface.PlayerConnectedMessageHandler(m_EditorConnection, playerId);
  121. }
  122. void PlayerDisconnectedEventHandler(int playerId)
  123. {
  124. m_RemoteInterface.PlayerDisconnectedMessageHandler(playerId);
  125. }
  126. void OnGUI()
  127. {
  128. string message;
  129. if (m_RemoteInterface.connected)
  130. {
  131. message = string.Format("Connected to remote AR device: {0}", m_RemoteInterface.playerId);
  132. var buttonRect = new Rect((Screen.width / 2) - 200, (Screen.height / 2) - 200, 400, 100);
  133. if (m_RemoteInterface.IsRemoteServiceRunning)
  134. {
  135. if (GUI.Button(buttonRect, "Stop Remote AR Session"))
  136. m_RemoteInterface.StopRemoteService();
  137. }
  138. else
  139. {
  140. if (GUI.Button(buttonRect, "Start Remote AR Session"))
  141. m_RemoteInterface.StartRemoteService(GetSettings());
  142. }
  143. }
  144. else
  145. {
  146. message = "Please connect to remote Player in the console menu.";
  147. }
  148. var boxRect = new Rect((Screen.width / 2) - 200, (Screen.height / 2) + 100, 400, 50);
  149. GUI.Box(boxRect, message);
  150. if (GUI.Button(new Rect((Screen.width / 2) - 200, Screen.height - 50, 100, 50), "Disconnect"))
  151. {
  152. m_RemoteInterface.StopRemoteService();
  153. m_EditorConnection.DisconnectAll();
  154. }
  155. }
  156. protected override void SetupARInterface()
  157. {
  158. m_RemoteInterface = new ARRemoteEditorInterface();
  159. m_RemoteInterface.editorConnection = m_EditorConnection;
  160. m_ARInterface = m_RemoteInterface;
  161. ARInterface.SetInterface(m_RemoteInterface);
  162. m_RemoteInterface.sendVideo = sendVideo;
  163. m_RemoteInterface.BackgroundRendering = BackgroundRendering;
  164. }
  165. private void OnDisable()
  166. {
  167. m_EditorConnection.DisconnectAll();
  168. }
  169. #endif
  170. }
  171. }