110 lines
3.7 KiB
C#
110 lines
3.7 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();
|
|
|
|
public void Initialize(GameObject panelGo, MainPanel mainPanel)
|
|
{
|
|
PanelGo = panelGo;
|
|
_mainPanel = mainPanel;
|
|
_content = panelGo.transform.Find("MeetingGrid/Viewport/Content");
|
|
_objectRightPool = panelGo.transform.Find("MeetingGrid/LeftPool").GetComponent<GameObjectPool>();
|
|
_objectLeftPool = panelGo.transform.Find("MeetingGrid/RightPool").GetComponent<GameObjectPool>();
|
|
|
|
_connectionTimeText = panelGo.transform.Find("MeetingNum/Time").GetComponent<Text>();
|
|
_connectionTimeText.text = this.GetSystem<IGlobalConfigSystem>().GetConnectionStartTime();
|
|
|
|
_userCountText = panelGo.transform.Find("MeetingNum/Num").GetComponent<Text>();
|
|
_userCountText.text = this.GetSystem<IGlobalConfigSystem>().GetUserCount().ToString();
|
|
}
|
|
|
|
public async void OnEnter()
|
|
{
|
|
OnUserCountChangedEvent += OnUserCountChanged;
|
|
GameObject.FindObjectOfType<MessageChannel>().OnChatMessageReceived += OnChatMessageReceivedEvent;
|
|
}
|
|
|
|
public async void OnExit()
|
|
{
|
|
OnUserCountChangedEvent -= OnUserCountChanged;
|
|
GameObject.FindObjectOfType<MessageChannel>().OnChatMessageReceived -= OnChatMessageReceivedEvent;
|
|
}
|
|
|
|
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");
|
|
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 == "image")
|
|
{
|
|
message.gameObject.SetActive(false);
|
|
sprite.gameObject.SetActive(true);
|
|
//TODO 图片传输功能
|
|
}
|
|
}
|
|
|
|
private void OnUserCountChanged(int obj)
|
|
{
|
|
_userCountText.text = obj.ToString();
|
|
}
|
|
|
|
public IArchitecture GetArchitecture()
|
|
{
|
|
return MainArchitecture.Interface;
|
|
}
|
|
}
|
|
} |