Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

Question Remote Input

Discussion in 'Unity Render Streaming' started by Piflik, May 6, 2021.

  1. Piflik

    Piflik

    Joined:
    Sep 11, 2011
    Posts:
    293
    I need some help with the Remote Input. I thought I understood it, but the documentation is a bit sparse, and in any case, what I implemented doesn't work.

    I have two Unity Applications, currently they both run on the same machine, in Editor. I want to send input from app A to app B. I already have a video stream from app B to app A running.

    I added an
    InputSystemChannelReceiver 
    component on app B's stream controller object and added it to the
    SingleConnection
    's list of streams.
    I added an
    InputSystemChannelSender 
    component on app A's stream controller object and also added it to the
    SingleConnection
    's list of stream. (
    Local 
    is set to
    true
    )

    In app B I create a
    RemoteInput 
    object via
    RemoteInputReceiver.Create()
    . And in the
    InputSystemChannelReceiver 
    I subscribe to
    OnStartedChannel 
    and in the callback I subscribe to that channel's
    OnMessage 
    event in order to call
    RemoteInput.ProcessInput
    .

    To test it I just call
    RemoteInput.RemoteKeyboard.wKey.wasPressedThisFrame
    in
    Update
    and write to the console.

    However, I get an exception, stating that I cannot queue events for the keyboard, because it hasn't been added to the system yet. I tried calling
    InputSystem.AddDevice(RemoteInput.RemoteKeyboard)
    , both directly after creating the
    RemoteInput 
    and in the
    OnChannelStarted 
    callback, but that didn't change anything.

    Is there some essential step I am missing?

    Edit: I tried different combinations of "
    Local
    ", but this way (Sender is Local = true, Receiver is Local = false) is the only one where anything happens on the receiving side. (if Local is false for both, I get a null reference exception on the receiver when the sender disconnects, which makes sense, looking at the code, because the single connection doesn't create a channel if no
    DataChannel
    is
    Local
    . It should be handled, though.)
     
    Last edited: May 6, 2021
  2. Piflik

    Piflik

    Joined:
    Sep 11, 2011
    Posts:
    293
    This is the controlling code on app B (the one theat throws the exception):

    Code (CSharp):
    1. using Unity.RenderStreaming;
    2. using UnityEngine;
    3. using UnityEngine.InputSystem;
    4.  
    5. public class StreamController : MonoBehaviour {
    6.  
    7.  
    8.     [SerializeField] private SingleConnection _connection;
    9.     [SerializeField] private RenderStreaming _streaming;
    10.     [SerializeField] private InputSystemChannelReceiver _inputReceiver;
    11.     private RemoteInput _remoteInput;
    12.  
    13.     private string _channelID;
    14.  
    15.     public void Init(string channelID) {
    16.         _channelID = channelID;
    17.         StreamManager.Register(_channelID, this);
    18.         _streaming.Run();
    19.  
    20.         _remoteInput = RemoteInputReceiver.Create();
    21.         InputSystem.AddDevice(_remoteInput.RemoteKeyboard);
    22.  
    23.         _inputReceiver.OnStartedChannel += id => {
    24.             _inputReceiver.Channel.OnMessage += _remoteInput.ProcessInput;
    25.         };
    26.  
    27.     }
    28.  
    29.     public void Deinit(bool deregister = true) {
    30.         if (deregister) {
    31.             StreamManager.Deregister(_channelID);
    32.         }
    33.         _streaming.Stop();
    34.     }
    35.  
    36.     public void Connect() {
    37.         _connection.CreateConnection(_channelID);
    38.     }
    39.  
    40.     public void Disconnect() {
    41.         if (_channelID != null && _connection.IsConnected(_channelID)) {
    42.             _connection.DeleteConnection(_channelID);
    43.         }
    44.     }
    45.  
    46.     private void OnDestroy() {
    47.         Deinit();
    48.     }
    49.  
    50.  
    51.     void Update() {
    52.         if (_remoteInput.RemoteKeyboard.wKey.wasPressedThisFrame) {
    53.             Debug.Log("W has been pressed");
    54.         }
    55.     }
    56. }
    57.