using System;
using Unity.WebRTC;
using UnityEngine;
using UnityEngine.InputSystem;
namespace Unity.RenderStreaming.Samples
{
///
///
///
[Serializable]
class ButtonClickEvent : UnityEngine.Events.UnityEvent { }
///
///
///
[Serializable]
class ButtonClickElement
{
[Tooltip("Specifies the ID on the HTML")]
public int elementId;
public ButtonClickEvent click;
}
///
///
///
public class WebBrowserInputChannelReceiver : InputChannelReceiverBase
{
///
///
///
[SerializeField, Tooltip("Array to set your own click event")]
private ButtonClickElement[] arrayButtonClickEvent;
///
///
///
public override event Action onDeviceChange;
private RemoteInput remoteInput;
///
///
///
///
public override void SetChannel(string connectionId, RTCDataChannel channel)
{
if (channel == null)
{
if (remoteInput != null)
{
onDeviceChange?.Invoke(remoteInput.RemoteGamepad, InputDeviceChange.Removed);
onDeviceChange?.Invoke(remoteInput.RemoteKeyboard, InputDeviceChange.Removed);
onDeviceChange?.Invoke(remoteInput.RemoteMouse, InputDeviceChange.Removed);
onDeviceChange?.Invoke(remoteInput.RemoteTouchscreen, InputDeviceChange.Removed);
remoteInput.Dispose();
remoteInput = null;
}
}
else
{
remoteInput = RemoteInputReceiver.Create();
remoteInput.ActionButtonClick = OnButtonClick;
channel.OnMessage += remoteInput.ProcessInput;
onDeviceChange?.Invoke(remoteInput.RemoteGamepad, InputDeviceChange.Added);
onDeviceChange?.Invoke(remoteInput.RemoteKeyboard, InputDeviceChange.Added);
onDeviceChange?.Invoke(remoteInput.RemoteMouse, InputDeviceChange.Added);
onDeviceChange?.Invoke(remoteInput.RemoteTouchscreen, InputDeviceChange.Added);
}
base.SetChannel(connectionId, channel);
}
///
///
///
///
public virtual void OnButtonClick(int elementId)
{
foreach (var element in arrayButtonClickEvent)
{
if (element.elementId == elementId)
{
element.click.Invoke(elementId);
}
}
}
///
///
///
public virtual void OnDestroy()
{
remoteInput?.Dispose();
}
}
}