ARFoundationSample.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #if URS_USE_AR_FOUNDATION
  2. using System.Collections;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using UnityEngine.InputSystem;
  6. using UnityEngine.InputSystem.Controls;
  7. using UnityEngine.XR.ARFoundation;
  8. namespace Unity.RenderStreaming.Samples
  9. {
  10. internal class ARFoundationSample : MonoBehaviour
  11. {
  12. #pragma warning disable 0649
  13. [SerializeField] private SignalingManager renderStreaming;
  14. [SerializeField] private Button startButton;
  15. [SerializeField] private Button stopButton;
  16. [SerializeField] private RawImage remoteVideoImage;
  17. [SerializeField] private VideoStreamReceiver receiveVideoViewer;
  18. [SerializeField] private SingleConnection connection;
  19. [SerializeField] private ARSession session;
  20. [SerializeField] private Text textPositionX;
  21. [SerializeField] private Text textPositionY;
  22. [SerializeField] private Text textPositionZ;
  23. [SerializeField] private Text textQuaternionX;
  24. [SerializeField] private Text textQuaternionY;
  25. [SerializeField] private Text textQuaternionZ;
  26. [SerializeField] private InputAction positionAction;
  27. [SerializeField] private InputAction quaternionAction;
  28. #pragma warning restore 0649
  29. private string _connectionId;
  30. private RenderStreamingSettings settings;
  31. void Awake()
  32. {
  33. startButton.onClick.AddListener(CreateConnection);
  34. stopButton.onClick.AddListener(DeleteConnection);
  35. startButton.gameObject.SetActive(true);
  36. stopButton.gameObject.SetActive(false);
  37. receiveVideoViewer.OnUpdateReceiveTexture += texture => remoteVideoImage.texture = texture;
  38. settings = SampleManager.Instance.Settings;
  39. }
  40. IEnumerator Start()
  41. {
  42. if (!renderStreaming.runOnAwake)
  43. {
  44. if (settings != null)
  45. renderStreaming.useDefaultSettings = settings.UseDefaultSettings;
  46. if (settings?.SignalingSettings != null)
  47. renderStreaming.SetSignalingSettings(settings.SignalingSettings);
  48. renderStreaming.Run();
  49. }
  50. if ((ARSession.state == ARSessionState.None ) ||
  51. (ARSession.state == ARSessionState.CheckingAvailability))
  52. {
  53. yield return ARSession.CheckAvailability();
  54. }
  55. if (ARSession.state == ARSessionState.Unsupported)
  56. {
  57. // Start some fallback experience for unsupported devices
  58. Debug.LogError("AR foundation is not supported on this device.");
  59. }
  60. else
  61. {
  62. // Start the AR session
  63. session.enabled = true;
  64. }
  65. }
  66. void OnEnable()
  67. {
  68. positionAction.Enable();
  69. positionAction.performed += UpdatePosition;
  70. positionAction.started += UpdatePosition;
  71. positionAction.canceled += UpdatePosition;
  72. quaternionAction.Enable();
  73. quaternionAction.performed += UpdateQuaternion;
  74. quaternionAction.started += UpdateQuaternion;
  75. quaternionAction.canceled += UpdateQuaternion;
  76. }
  77. void OnDisable()
  78. {
  79. positionAction.Disable();
  80. positionAction.performed -= UpdatePosition;
  81. positionAction.started -= UpdatePosition;
  82. positionAction.canceled -= UpdatePosition;
  83. quaternionAction.Disable();
  84. quaternionAction.performed -= UpdateQuaternion;
  85. quaternionAction.started -= UpdateQuaternion;
  86. quaternionAction.canceled -= UpdateQuaternion;
  87. }
  88. private void UpdatePosition(InputAction.CallbackContext context)
  89. {
  90. if (context.control is Vector3Control control)
  91. {
  92. Vector3 value = control.ReadValue();
  93. textPositionX.text = value.x.ToString("f2");
  94. textPositionY.text = value.y.ToString("f2");
  95. textPositionZ.text = value.z.ToString("f2");
  96. }
  97. }
  98. private void UpdateQuaternion(InputAction.CallbackContext context)
  99. {
  100. if (context.control is QuaternionControl control)
  101. {
  102. Quaternion value = control.ReadValue();
  103. textQuaternionX.text = value.eulerAngles.x.ToString("f2");
  104. textQuaternionY.text = value.eulerAngles.y.ToString("f2");
  105. textQuaternionZ.text = value.eulerAngles.z.ToString("f2");
  106. }
  107. }
  108. void CreateConnection()
  109. {
  110. if(settings != null)
  111. receiveVideoViewer.SetCodec(settings.ReceiverVideoCodec);
  112. _connectionId = System.Guid.NewGuid().ToString("N");
  113. connection.CreateConnection(_connectionId);
  114. startButton.gameObject.SetActive(false);
  115. stopButton.gameObject.SetActive(true);
  116. }
  117. void DeleteConnection()
  118. {
  119. connection.DeleteConnection(_connectionId);
  120. _connectionId = null;
  121. startButton.gameObject.SetActive(true);
  122. stopButton.gameObject.SetActive(false);
  123. }
  124. }
  125. }
  126. #endif