ConnectToEditor.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. using UnityEngine;
  2. using UnityEngine.Networking.PlayerConnection;
  3. using System.Text;
  4. using Utils;
  5. namespace UnityEngine.XR.iOS
  6. {
  7. public class ConnectToEditor : MonoBehaviour
  8. {
  9. PlayerConnection playerConnection;
  10. UnityARSessionNativeInterface m_session;
  11. int editorID;
  12. Texture2D frameBufferTex;
  13. // Use this for initialization
  14. void Start()
  15. {
  16. Debug.Log("STARTING ConnectToEditor");
  17. editorID = -1;
  18. playerConnection = PlayerConnection.instance;
  19. playerConnection.RegisterConnection(EditorConnected);
  20. playerConnection.RegisterDisconnection(EditorDisconnected);
  21. playerConnection.Register(ConnectionMessageIds.fromEditorARKitSessionMsgId, HandleEditorMessage);
  22. m_session = null;
  23. }
  24. void OnGUI()
  25. {
  26. if (m_session == null) {
  27. GUI.Box (new Rect ((Screen.width / 2) - 200, (Screen.height / 2), 400, 50), "Waiting for editor connection...");
  28. }
  29. }
  30. void HandleEditorMessage(MessageEventArgs mea)
  31. {
  32. serializableFromEditorMessage sfem = mea.data.Deserialize<serializableFromEditorMessage>();
  33. if (sfem != null && sfem.subMessageId == SubMessageIds.editorInitARKit) {
  34. InitializeARKit ( sfem.arkitConfigMsg );
  35. }
  36. }
  37. void InitializeARKit(serializableARKitInit sai)
  38. {
  39. #if !UNITY_EDITOR
  40. //get the config and runoption from editor and use them to initialize arkit on device
  41. Application.targetFrameRate = 60;
  42. m_session = UnityARSessionNativeInterface.GetARSessionNativeInterface();
  43. ARKitWorldTrackingSessionConfiguration config = sai.config;
  44. UnityARSessionRunOption runOptions = sai.runOption;
  45. m_session.RunWithConfigAndOptions(config, runOptions);
  46. UnityARSessionNativeInterface.ARFrameUpdatedEvent += ARFrameUpdated;
  47. UnityARSessionNativeInterface.ARAnchorAddedEvent += ARAnchorAdded;
  48. UnityARSessionNativeInterface.ARAnchorUpdatedEvent += ARAnchorUpdated;
  49. UnityARSessionNativeInterface.ARAnchorRemovedEvent += ARAnchorRemoved;
  50. #endif
  51. }
  52. public void ARFrameUpdated(UnityARCamera camera)
  53. {
  54. serializableUnityARCamera serARCamera = camera;
  55. SendToEditor(ConnectionMessageIds.updateCameraFrameMsgId, serARCamera);
  56. }
  57. public void ARAnchorAdded(ARPlaneAnchor planeAnchor)
  58. {
  59. serializableUnityARPlaneAnchor serPlaneAnchor = planeAnchor;
  60. SendToEditor (ConnectionMessageIds.addPlaneAnchorMsgeId, serPlaneAnchor);
  61. }
  62. public void ARAnchorUpdated(ARPlaneAnchor planeAnchor)
  63. {
  64. serializableUnityARPlaneAnchor serPlaneAnchor = planeAnchor;
  65. SendToEditor (ConnectionMessageIds.updatePlaneAnchorMsgeId, serPlaneAnchor);
  66. }
  67. public void ARAnchorRemoved(ARPlaneAnchor planeAnchor)
  68. {
  69. serializableUnityARPlaneAnchor serPlaneAnchor = planeAnchor;
  70. SendToEditor (ConnectionMessageIds.removePlaneAnchorMsgeId, serPlaneAnchor);
  71. }
  72. void EditorConnected(int playerID)
  73. {
  74. Debug.Log("connected");
  75. editorID = playerID;
  76. }
  77. void EditorDisconnected(int playerID)
  78. {
  79. if (editorID == playerID)
  80. {
  81. editorID = -1;
  82. }
  83. DisconnectFromEditor ();
  84. #if !UNITY_EDITOR
  85. if (m_session != null)
  86. {
  87. m_session.Pause();
  88. m_session = null;
  89. }
  90. #endif
  91. }
  92. public void SendToEditor(System.Guid msgId, object serializableObject)
  93. {
  94. byte[] arrayToSend = serializableObject.SerializeToByteArray ();
  95. SendToEditor (msgId, arrayToSend);
  96. }
  97. public void SendToEditor(System.Guid msgId, byte[] data)
  98. {
  99. if (playerConnection.isConnected)
  100. {
  101. playerConnection.Send(msgId, data);
  102. }
  103. }
  104. public void DisconnectFromEditor()
  105. {
  106. #if UNITY_2017_1_OR_NEWER
  107. playerConnection.DisconnectAll();
  108. #endif
  109. }
  110. }
  111. }