using System; using Unity.RenderStreaming.InputSystem; using Unity.WebRTC; using UnityEngine; namespace Unity.RenderStreaming { /// /// The InputSender component is responsible for sending input data over a data channel in a Unity Render Streaming context. /// /// [AddComponentMenu("Render Streaming/Input Sender")] public class InputSender : DataChannelBase { private Sender sender; private InputRemoting senderInput; private IDisposable suscriberDisposer; /// /// Sets the RTCDataChannel for the sender. /// /// /// /// (); /// var channel = channels.FirstOrDefault(_ => !_.IsLocal && !_.IsConnected); /// channel?.SetChannel(data); /// } /// ]]> /// /// /// The connection ID. /// The RTCDataChannel to set. public override void SetChannel(string connectionId, RTCDataChannel channel) { if (channel == null) { Dispose(); } else { sender = new Sender(); senderInput = new InputRemoting(sender); suscriberDisposer = senderInput.Subscribe(new Observer(channel)); channel.OnOpen += OnOpen; channel.OnClose += OnClose; } base.SetChannel(connectionId, channel); } /// /// Calculates the input region based on the given texture size and region in world coordinates. /// /// /// /// /// /// /// The region of the texture in world coordinate system. /// The size of the texture. public void CalculateInputResion(Rect region, Vector2Int size) { sender.CalculateInputRegion(region, new Rect(Vector2.zero, size)); } /// /// Enables or disables input position correction. /// /// /// /// /// /// /// True to enable input position correction, false to disable. public void EnableInputPositionCorrection(bool enabled) { sender.EnableInputPositionCorrection = enabled; } void OnOpen() { senderInput.StartSending(); } void OnClose() { senderInput.StopSending(); } protected virtual void OnDestroy() { this.Dispose(); } protected void Dispose() { senderInput?.StopSending(); suscriberDisposer?.Dispose(); sender?.Dispose(); sender = null; } } }