Files
webRtc/Assets/Script/MainPanel/MeetingChat.cs
2026-05-22 15:43:00 +08:00

141 lines
5.4 KiB
C#

using System;
using System.Collections.Generic;
using RenderStreaming;
using Script.Util;
using Stary.Evo;
using Unity.RenderStreaming;
using UnityEngine;
using UnityEngine.UI;
namespace Script
{
public class MeetingChat : IController
{
public GameObject PanelGo;
private MainPanel _mainPanel;
private Transform _content;
private GameObjectPool _objectRightPool;
private GameObjectPool _objectLeftPool;
private Text _connectionTimeText;
private Text _userCountText;
public Action<int> OnUserCountChangedEvent;
private List<ChatData> _chatDatas = new();
private InputField _messageInput;
private Button _sendButton;
public void Initialize(GameObject panelGo, MainPanel mainPanel)
{
PanelGo = panelGo;
_mainPanel = mainPanel;
_content = panelGo.transform.Find("MeetingGrid/Viewport/Content");
_objectLeftPool = panelGo.transform.Find("MeetingGrid/LeftPool").GetComponent<GameObjectPool>();
_objectRightPool = panelGo.transform.Find("MeetingGrid/RightPool").GetComponent<GameObjectPool>();
_connectionTimeText = panelGo.transform.Find("MeetingNum/Time").GetComponent<Text>();
_userCountText = panelGo.transform.Find("MeetingNum/Num").GetComponent<Text>();
_messageInput = panelGo.transform.Find("MettingSend/InputField").GetComponent<InputField>();
_sendButton = panelGo.transform.Find("MettingSend/SendBtn").GetComponent<Button>();
_sendButton.onClick.AddListener(OnSendButtonClick);
OnUserCountChangedEvent += OnUserCountChanged;
GameObject.FindObjectOfType<MessageChannel>().OnChatMessageReceived += OnChatMessageReceivedEvent;
}
public async void OnEnter()
{
_connectionTimeText.text = this.GetSystem<IGlobalConfigSystem>().GetConnectionStartTime();
_userCountText.text = this.GetSystem<IGlobalConfigSystem>().GetUserCount().ToString();
}
public async void OnDestroy()
{
OnUserCountChangedEvent -= OnUserCountChanged;
GameObject.FindObjectOfType<MessageChannel>().OnChatMessageReceived -= OnChatMessageReceivedEvent;
_objectRightPool.AllRelease();
_objectLeftPool.AllRelease();
}
private void OnSendButtonClick()
{
if (string.IsNullOrEmpty(_messageInput.text)) return;
var chatData = new ChatData
{
id = "msg_" + Guid.NewGuid().ToString(),
senderId = this.GetSystem<IGlobalConfigSystem>().GetUserId(),
senderName = this.GetSystem<IGlobalConfigSystem>().GetConnectionName(),
senderAvatar = $"{this.GetSystem<IGlobalConfigSystem>().IP}/images/head/" +
$"{this.GetSystem<IGlobalConfigSystem>().GetConnectionTexture()}.png",
type = "text",
content = _messageInput.text,
isSelf = "true",
timestamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
};
GameObject.FindObjectOfType<MessageChannel>().SendMessage<ChatData>(_messageInput.text, chatData);
_messageInput.text = "";
}
private void OnChatMessageReceivedEvent(string connectionId, ChatData data)
{
if (connectionId != this.GetSystem<IGlobalConfigSystem>().GetConnectionId()) return;
_chatDatas.Add(data);
if (bool.Parse(data.isSelf) == false)
{
var entry = _objectLeftPool.Get();
SetMessageEntry(data, entry);
}
}
private void SetMessageEntry(ChatData data, GameObject entry)
{
var head = entry.transform.Find("head/image");
var name = entry.transform.Find("name");
var message = entry.transform.Find("message");
var sprite = entry.transform.Find("sprite");
if (string.IsNullOrEmpty(data.senderAvatar))
{
var randomColor = WebRTCUtil.GetRandomColor();
head.GetComponent<Image>().color = randomColor;
head.transform.Find("Name").GetComponent<Text>().text = data.senderName.Substring(0, 1);
}
else
{
WebRTCUtil.DownloadAndSetAvatar(data.senderAvatar, head.GetComponent<Image>());
}
name.GetComponent<Text>().text = data.senderName;
if (data.type == "text")
{
message.GetComponent<Text>().text = data.content;
sprite.gameObject.SetActive(false);
}
else if (data.type == "file")
{
message.gameObject.SetActive(false);
sprite.gameObject.SetActive(true);
//TODO 图片传输功能
if (WebRTCUtil.TryDecodeDataUrlToTexture(data.content, out var texture))
sprite.GetComponent<Image>().sprite = Sprite.Create(texture,
new Rect(0, 0, texture.width, texture.height),
Vector2.one);
}
}
private void OnUserCountChanged(int obj)
{
_userCountText.text = obj.ToString();
}
public IArchitecture GetArchitecture()
{
return MainArchitecture.Interface;
}
}
}