InputSender.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using System;
  2. using Unity.WebRTC;
  3. using UnityEngine;
  4. using Unity.RenderStreaming.InputSystem;
  5. using System.Collections;
  6. using static Unity.RenderStreaming.InputSystem.InputRemoting;
  7. using System.Text;
  8. namespace Unity.RenderStreaming
  9. {
  10. /// <summary>
  11. ///
  12. /// </summary>
  13. [AddComponentMenu("Render Streaming/Input Sender")]
  14. public class InputSender : DataChannelBase
  15. {
  16. private Sender sender;
  17. private InputRemoting senderInput;
  18. private IDisposable suscriberDisposer;
  19. /// <summary>
  20. ///
  21. /// </summary>
  22. /// <param name="track"></param>
  23. public override void SetChannel(string connectionId, RTCDataChannel channel)
  24. {
  25. if (channel == null)
  26. {
  27. Dispose();
  28. }
  29. else
  30. {
  31. sender = new Sender();
  32. senderInput = new InputRemoting(sender);
  33. suscriberDisposer = senderInput.Subscribe(new Observer(channel));
  34. channel.OnOpen += OnOpen;
  35. channel.OnClose += OnClose;
  36. }
  37. base.SetChannel(connectionId, channel);
  38. }
  39. /// <summary>
  40. ///
  41. /// </summary>
  42. /// <param name="size">Texture Size.</param>
  43. /// <param name="region">Region of the texture in world coordinate system.</param>
  44. public void CalculateInputResion(Rect region, Vector2Int size)
  45. {
  46. sender.CalculateInputRegion(region, new Rect(Vector2.zero, size));
  47. }
  48. /// <summary>
  49. ///
  50. /// </summary>
  51. /// <param name="enabled"></param>
  52. public void EnableInputPositionCorrection(bool enabled)
  53. {
  54. sender.EnableInputPositionCorrection = enabled;
  55. }
  56. void OnOpen()
  57. {
  58. senderInput.StartSending();
  59. }
  60. public void SendMessage(Message message)
  61. {
  62. if(senderInput!=null)
  63. senderInput.Send(message);
  64. }
  65. void OnClose()
  66. {
  67. senderInput.StopSending();
  68. }
  69. protected virtual void OnDestroy()
  70. {
  71. this.Dispose();
  72. }
  73. protected void Dispose()
  74. {
  75. senderInput?.StopSending();
  76. suscriberDisposer?.Dispose();
  77. sender?.Dispose();
  78. sender = null;
  79. }
  80. }
  81. }