2026-05-22 10:59:18 +08:00
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2026-05-24 17:52:30 +08:00
|
|
|
using Newtonsoft.Json;
|
2026-05-19 22:40:52 +08:00
|
|
|
using RenderStreaming;
|
2026-05-22 10:59:18 +08:00
|
|
|
using Script.Util;
|
2026-05-19 22:40:52 +08:00
|
|
|
using Stary.Evo;
|
2026-05-22 10:59:18 +08:00
|
|
|
using Unity.RenderStreaming;
|
2026-05-19 22:40:52 +08:00
|
|
|
using UnityEngine;
|
2026-05-22 10:59:18 +08:00
|
|
|
using UnityEngine.UI;
|
2026-05-19 22:40:52 +08:00
|
|
|
|
|
|
|
|
namespace Script
|
|
|
|
|
{
|
|
|
|
|
public class MeetingChat : IController
|
|
|
|
|
{
|
|
|
|
|
public GameObject PanelGo;
|
2026-05-22 10:59:18 +08:00
|
|
|
private MainPanel _mainPanel;
|
2026-05-19 22:40:52 +08:00
|
|
|
|
2026-05-22 10:59:18 +08:00
|
|
|
private Transform _content;
|
|
|
|
|
private GameObjectPool _objectRightPool;
|
|
|
|
|
private GameObjectPool _objectLeftPool;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private Text _connectionTimeText;
|
|
|
|
|
private Text _userCountText;
|
|
|
|
|
|
|
|
|
|
public Action<int> OnUserCountChangedEvent;
|
|
|
|
|
|
|
|
|
|
private List<ChatData> _chatDatas = new();
|
|
|
|
|
|
2026-05-22 15:43:00 +08:00
|
|
|
private InputField _messageInput;
|
|
|
|
|
private Button _sendButton;
|
|
|
|
|
|
2026-05-22 10:59:18 +08:00
|
|
|
public void Initialize(GameObject panelGo, MainPanel mainPanel)
|
2026-05-19 22:40:52 +08:00
|
|
|
{
|
|
|
|
|
PanelGo = panelGo;
|
2026-05-22 10:59:18 +08:00
|
|
|
_mainPanel = mainPanel;
|
|
|
|
|
_content = panelGo.transform.Find("MeetingGrid/Viewport/Content");
|
2026-05-22 15:43:00 +08:00
|
|
|
_objectLeftPool = panelGo.transform.Find("MeetingGrid/LeftPool").GetComponent<GameObjectPool>();
|
|
|
|
|
_objectRightPool = panelGo.transform.Find("MeetingGrid/RightPool").GetComponent<GameObjectPool>();
|
2026-05-22 10:59:18 +08:00
|
|
|
_connectionTimeText = panelGo.transform.Find("MeetingNum/Time").GetComponent<Text>();
|
|
|
|
|
_userCountText = panelGo.transform.Find("MeetingNum/Num").GetComponent<Text>();
|
2026-05-22 15:43:00 +08:00
|
|
|
|
|
|
|
|
_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;
|
2026-05-19 22:40:52 +08:00
|
|
|
}
|
|
|
|
|
|
2026-05-22 15:43:00 +08:00
|
|
|
|
2026-05-19 22:40:52 +08:00
|
|
|
public async void OnEnter()
|
|
|
|
|
{
|
2026-05-22 15:43:00 +08:00
|
|
|
_connectionTimeText.text = this.GetSystem<IGlobalConfigSystem>().GetConnectionStartTime();
|
|
|
|
|
_userCountText.text = this.GetSystem<IGlobalConfigSystem>().GetUserCount().ToString();
|
2026-05-19 22:40:52 +08:00
|
|
|
}
|
|
|
|
|
|
2026-05-22 15:43:00 +08:00
|
|
|
public async void OnDestroy()
|
2026-05-19 22:40:52 +08:00
|
|
|
{
|
2026-05-22 10:59:18 +08:00
|
|
|
OnUserCountChangedEvent -= OnUserCountChanged;
|
|
|
|
|
GameObject.FindObjectOfType<MessageChannel>().OnChatMessageReceived -= OnChatMessageReceivedEvent;
|
2026-05-22 15:43:00 +08:00
|
|
|
_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,
|
2026-05-24 17:52:30 +08:00
|
|
|
isSelf = false,
|
2026-05-22 15:43:00 +08:00
|
|
|
timestamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
|
|
|
|
|
};
|
2026-05-24 17:52:30 +08:00
|
|
|
var message = new
|
|
|
|
|
{
|
|
|
|
|
type = "chat-message",
|
|
|
|
|
data = chatData
|
|
|
|
|
};
|
|
|
|
|
var data = new
|
|
|
|
|
{
|
|
|
|
|
message = message,
|
|
|
|
|
connectionId = this.GetSystem<IGlobalConfigSystem>().GetConnectionId()
|
|
|
|
|
};
|
|
|
|
|
var send = new
|
|
|
|
|
{
|
|
|
|
|
form = this.GetSystem<IGlobalConfigSystem>().GetConnectionId(),
|
|
|
|
|
to = "",
|
|
|
|
|
type = "on-message",
|
|
|
|
|
data = data
|
|
|
|
|
};
|
|
|
|
|
SignalingMessageHelper.SendMessage(JsonConvert.SerializeObject(send));
|
2026-05-22 15:43:00 +08:00
|
|
|
_messageInput.text = "";
|
2026-05-24 17:52:30 +08:00
|
|
|
chatData.isSelf = !chatData.isSelf;
|
|
|
|
|
_chatDatas.Add(chatData);
|
|
|
|
|
var entry = _objectRightPool.Get();
|
|
|
|
|
SetMessageEntry(chatData, entry);
|
2026-05-22 10:59:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnChatMessageReceivedEvent(string connectionId, ChatData data)
|
|
|
|
|
{
|
|
|
|
|
if (connectionId != this.GetSystem<IGlobalConfigSystem>().GetConnectionId()) return;
|
|
|
|
|
_chatDatas.Add(data);
|
2026-05-24 17:52:30 +08:00
|
|
|
if (data.isSelf == false)
|
2026-05-22 10:59:18 +08:00
|
|
|
{
|
|
|
|
|
var entry = _objectLeftPool.Get();
|
|
|
|
|
SetMessageEntry(data, entry);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SetMessageEntry(ChatData data, GameObject entry)
|
|
|
|
|
{
|
2026-05-22 15:43:00 +08:00
|
|
|
var head = entry.transform.Find("head/image");
|
2026-05-22 10:59:18 +08:00
|
|
|
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);
|
|
|
|
|
}
|
2026-05-22 15:43:00 +08:00
|
|
|
else if (data.type == "file")
|
2026-05-22 10:59:18 +08:00
|
|
|
{
|
|
|
|
|
message.gameObject.SetActive(false);
|
|
|
|
|
sprite.gameObject.SetActive(true);
|
|
|
|
|
//TODO 图片传输功能
|
2026-05-22 15:43:00 +08:00
|
|
|
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);
|
2026-05-22 10:59:18 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnUserCountChanged(int obj)
|
|
|
|
|
{
|
|
|
|
|
_userCountText.text = obj.ToString();
|
2026-05-19 22:40:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IArchitecture GetArchitecture()
|
|
|
|
|
{
|
|
|
|
|
return MainArchitecture.Interface;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|