GyroSample.cs 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using UnityEngine.InputSystem;
  4. using UnityEngine.InputSystem.Controls;
  5. using Gyroscope = UnityEngine.InputSystem.Gyroscope;
  6. namespace Unity.RenderStreaming.Samples
  7. {
  8. using InputSystem = UnityEngine.InputSystem.InputSystem;
  9. class GyroSample : MonoBehaviour
  10. {
  11. #pragma warning disable 0649
  12. [SerializeField] private SignalingManager renderStreaming;
  13. [SerializeField] private Button sendOfferButton;
  14. [SerializeField] private RawImage remoteVideoImage;
  15. [SerializeField] private VideoStreamReceiver receiveVideoViewer;
  16. [SerializeField] private SingleConnection connection;
  17. [SerializeField] private Text textVelocityX;
  18. [SerializeField] private Text textVelocityY;
  19. [SerializeField] private Text textVelocityZ;
  20. [SerializeField] private InputAction vector3Action;
  21. #pragma warning restore 0649
  22. private RenderStreamingSettings settings;
  23. void Awake()
  24. {
  25. if(Gyroscope.current != null)
  26. InputSystem.EnableDevice(Gyroscope.current);
  27. else
  28. Debug.LogError("Gyroscope is not supported on this device.");
  29. sendOfferButton.onClick.AddListener(SendOffer);
  30. receiveVideoViewer.OnUpdateReceiveTexture += texture => remoteVideoImage.texture = texture;
  31. settings = SampleManager.Instance.Settings;
  32. }
  33. void OnDestroy()
  34. {
  35. // Reset InputControl of Gyroscope surely.
  36. InputSystem.ResetDevice(Gyroscope.current, true);
  37. InputSystem.DisableDevice(Gyroscope.current);
  38. }
  39. void Start()
  40. {
  41. if (renderStreaming.runOnAwake)
  42. return;
  43. if (settings != null)
  44. renderStreaming.useDefaultSettings = settings.UseDefaultSettings;
  45. if (settings?.SignalingSettings != null)
  46. renderStreaming.SetSignalingSettings(settings.SignalingSettings);
  47. renderStreaming.Run();
  48. }
  49. void OnEnable()
  50. {
  51. vector3Action.Enable();
  52. vector3Action.performed += UpdateVector3;
  53. vector3Action.started += UpdateVector3;
  54. vector3Action.canceled += UpdateVector3;
  55. }
  56. void OnDisable()
  57. {
  58. vector3Action.Disable();
  59. vector3Action.performed -= UpdateVector3;
  60. vector3Action.started -= UpdateVector3;
  61. vector3Action.canceled -= UpdateVector3;
  62. }
  63. private void UpdateVector3(InputAction.CallbackContext context)
  64. {
  65. if (context.control is Vector3Control control)
  66. {
  67. Vector3 value = control.ReadValue();
  68. textVelocityX.text = value.x.ToString("f2");
  69. textVelocityY.text = value.y.ToString("f2");
  70. textVelocityZ.text = value.z.ToString("f2");
  71. }
  72. }
  73. void SendOffer()
  74. {
  75. if(settings != null)
  76. receiveVideoViewer.SetCodec(settings.ReceiverVideoCodec);
  77. var connectionId = System.Guid.NewGuid().ToString("N");
  78. connection.CreateConnection(connectionId);
  79. }
  80. }
  81. }