using System.Linq; using System.Collections.Generic; using UnityEngine; namespace Unity.RenderStreaming.Samples { class Multiplay : SignalingHandlerBase, IOfferHandler, IAddChannelHandler, IDisconnectHandler, IDeletedConnectionHandler { [SerializeField] GameObject prefab; private List connectionIds = new List(); private List streams = new List(); private Dictionary dictObj = new Dictionary(); private RenderStreamingSettings settings; void Awake() { settings = SampleManager.Instance.Settings; } public override IEnumerable Streams => streams; public void OnDeletedConnection(SignalingEventData eventData) { Disconnect(eventData.connectionId); } public void OnDisconnect(SignalingEventData eventData) { Disconnect(eventData.connectionId); } private void Disconnect(string connectionId) { if (!connectionIds.Contains(connectionId)) return; connectionIds.Remove(connectionId); var obj = dictObj[connectionId]; var sender = obj.GetComponentInChildren(); var inputChannel = obj.GetComponentInChildren(); var multiplayChannel = obj.GetComponentInChildren(); dictObj.Remove(connectionId); Object.Destroy(obj); RemoveSender(connectionId, sender); RemoveChannel(connectionId, inputChannel); RemoveChannel(connectionId, multiplayChannel); streams.Remove(sender); streams.Remove(inputChannel); streams.Remove(multiplayChannel); if (ExistConnection(connectionId)) DeleteConnection(connectionId); } public void OnOffer(SignalingEventData data) { if (connectionIds.Contains(data.connectionId)) { Debug.Log($"Already answered this connectionId : {data.connectionId}"); return; } connectionIds.Add(data.connectionId); var initialPosition = new Vector3(0, 3, 0); var newObj = Instantiate(prefab, initialPosition, Quaternion.identity); dictObj.Add(data.connectionId, newObj); var sender = newObj.GetComponentInChildren(); if (sender is VideoStreamSender videoStreamSender && settings != null) { videoStreamSender.width = (uint)settings.StreamSize.x; videoStreamSender.height = (uint)settings.StreamSize.y; videoStreamSender.SetCodec(settings.SenderVideoCodec); } var inputChannel = newObj.GetComponentInChildren(); var multiplayChannel = newObj.GetComponentInChildren(); var playerController = newObj.GetComponentInChildren(); if (multiplayChannel.OnChangeLabel == null) multiplayChannel.OnChangeLabel = new ChangeLabelEvent(); multiplayChannel.OnChangeLabel.AddListener(playerController.SetLabel); streams.Add(sender); streams.Add(inputChannel); streams.Add(multiplayChannel); AddSender(data.connectionId, sender); AddChannel(data.connectionId, inputChannel); AddChannel(data.connectionId, multiplayChannel); SendAnswer(data.connectionId); } /// todo(kazuki):: public void OnAddChannel(SignalingEventData data) { var obj = dictObj[data.connectionId]; var channels = obj.GetComponentsInChildren(); var channel = channels.FirstOrDefault(_ => !_.IsLocal && !_.IsConnected); channel?.SetChannel(data); } } }