using System.Collections.Generic; using RenderStreaming; using Script.Util; using Stary.Evo; using Unity.RenderStreaming; using UnityEngine; using UnityEngine.UI; namespace Script { public class MeetingInfoList : IController { private MainPanel _mainPanel; public GameObject PanelGo; /// /// 联系人列表项预制体 /// private GameObject _contactEntryPrefab; /// /// 全部联系人列表容器 /// private Transform _content; private Text _meetingNum; private Dictionary _meetingList = new(); private GameObjectPool _objectPool; public void Initialize(GameObject panelGo, MainPanel mainPanel) { PanelGo = panelGo; _mainPanel = mainPanel; _contactEntryPrefab = Resources.Load("ParticipantEntry"); _content = panelGo.transform.Find("MeetingGrid/Viewport/Content"); _meetingNum = panelGo.transform.Find("MeetingNum/Num").GetComponent(); _objectPool = PanelGo.transform.Find("MeetingGrid/Pool").GetComponent(); } public void OnEnter() { LoadUsers(); GameObject.FindObjectOfType().OnMediaStateChangeReceived += OnMediaStateChangeReceived; GameObject.FindObjectOfType().OnUserInfoMessageReceived += OnUserInfoMessageReceived; GameObject.FindObjectOfType().OnParticipantDisconnected += OnParticipantDisconnected; } /// /// 当有用户断开连接时调用 /// /// private void OnParticipantDisconnected(string id) { var userId = ""; foreach (var value in _meetingList.Values) if (value.item.participantId == id) { _objectPool.Release(value.participant); userId = value.item.userId; break; } _meetingList.Remove(userId); } /// /// 当有用户加入会议时调用 /// /// /// private void OnUserInfoMessageReceived(string arg1, UserInfo arg2) { foreach (var info in _meetingList.Values) _objectPool.Release(info.participant); _meetingList.Clear(); LoadUsers(); } /// /// 当有用户媒体状态改变时调用 /// /// /// private async void OnMediaStateChangeReceived(string connectionId, MediaStateChange message) { Debug.Log($"OnMediaStateChangeReceived: {message}"); if (_meetingList.ContainsKey(message.userId)) { var info = _meetingList[message.userId]; SetMediaStateMic(info.participant, message.audio); SetMediaStateVideo(info.participant, message.video); } } private async void LoadUsers() { var response = await WebRequestSystem.Get(this.GetSystem().IP, $"/signaling/users?connectionId={this.GetSystem().GetConnectionId()}"); if (response != null && response.totalCount > 0) { this.GetSystem().SetUserCount(response.totalCount); _mainPanel.OnUsersChangedEvnent?.Invoke(response.users); _meetingNum.text = response.totalCount.ToString(); for (var i = 0; i < response.totalCount; i++) { var item = response.users[i]; if (string.IsNullOrEmpty(item.name) || string.IsNullOrEmpty(item.userId)) continue; var entry = _objectPool.Get(); SetData(entry, item); } } } public void SetData(GameObject entry, MainPanel.UsersItem item) { var transform = entry.transform; ///头像赋值 var background = transform.Find("headBackground"); var backgroundName = background.transform.Find("Name"); if (string.IsNullOrEmpty(item.avatar)) { var randomColor = WebRTCUtil.GetRandomColor(); if (background != null) { var image = background.GetComponent(); if (image != null) image.color = randomColor; } if (backgroundName != null) { var textComponent = backgroundName.GetComponent(); if (textComponent != null && !string.IsNullOrEmpty(item.name)) textComponent.text = item.name.Substring(0, 1); } } else { background = transform.Find("headBackground"); if (background != null) { var imageComponent = background.GetComponent(); if (imageComponent != null) WebRTCUtil.DownloadAndSetAvatar(item.avatar, imageComponent); } if (backgroundName != null) backgroundName.GetComponent().text = ""; } if (!string.IsNullOrEmpty(item.name)) { var nameText = transform.Find("Name"); if (nameText != null) { var textComponent = nameText.GetComponent(); if (textComponent != null) textComponent.text = item.name; } } if (!string.IsNullOrEmpty(item.userId)) { var messageText = transform.Find("Message"); if (messageText != null) { var textComponent = messageText.GetComponent(); if (textComponent != null) textComponent.text = item.userId; } } if (!string.IsNullOrEmpty(item.role)) { var role = item.role; var participant = transform.Find("Name/state/participant"); var host = transform.Find("Name/state/host"); if (role.Equals("host")) { host.gameObject.SetActive(true); participant.gameObject.SetActive(false); SetMediaStateMic(entry, true); SetMediaStateVideo(entry, true); } else if (role.Equals("participant")) { participant.gameObject.SetActive(true); host.gameObject.SetActive(false); SetMediaStateMic(entry, false); SetMediaStateVideo(entry, true); } } _meetingList.Add(item.userId, new MessageRecordInfo(item, entry)); } private void SetMediaStateMic(GameObject entry, bool isAudio) { var mic = entry.transform.Find("mic"); var micBackground = mic.transform.Find("Background"); var micCheckmark = mic.transform.Find("Checkmark"); micCheckmark.gameObject.SetActive(isAudio); micBackground.gameObject.SetActive(!isAudio); } private void SetMediaStateVideo(GameObject entry, bool isVideo) { var video = entry.transform.Find("video"); var videoBackground = video.transform.Find("Background"); var videoCheckmark = video.transform.Find("Checkmark"); videoCheckmark.gameObject.SetActive(isVideo); videoBackground.gameObject.SetActive(!isVideo); } public async void OnExit() { GameObject.FindObjectOfType().OnMediaStateChangeReceived -= OnMediaStateChangeReceived; GameObject.FindObjectOfType().OnUserInfoMessageReceived -= OnUserInfoMessageReceived; GameObject.FindObjectOfType().OnParticipantDisconnected -= OnParticipantDisconnected; } public class MessageRecordInfo { public MainPanel.UsersItem item; public GameObject participant; public MessageRecordInfo(MainPanel.UsersItem item, GameObject participant) { this.item = item; this.participant = participant; } } public IArchitecture GetArchitecture() { return MainArchitecture.Interface; } } }