add code ui chat

This commit is contained in:
CuongNV
2026-03-09 16:55:47 +07:00
parent ab012dc30b
commit 476b1b623f
6 changed files with 217 additions and 1 deletions
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using BrewMonster.Network;
using BrewMonster.Scripts.UI;
using BrewMonster.UI;
@@ -501,8 +502,60 @@ namespace BrewMonster.Scripts.Chat
p.emotion);*/
return true;
}
/*public static void OnTaskChatMessage(byte[] buffer, int size)
{
const int iNameLen = 20;
int expectedSize = 4 * 5 + iNameLen * 2;
if (size < expectedSize)
return;
int offset = 0;
int self_id = BitConverter.ToInt32(buffer, offset); offset += 4;
uint task_id = BitConverter.ToUInt32(buffer, offset); offset += 4;
int channel = BitConverter.ToInt32(buffer, offset); offset += 4;
int param = BitConverter.ToInt32(buffer, offset); offset += 4;
int map_id = BitConverter.ToInt32(buffer, offset); offset += 4;
// name (UTF16 20 chars)
string name = Encoding.Unicode.GetString(buffer, offset, iNameLen * 2);
name = name.TrimEnd('\0');
var pTempl = EC_Game.GetTaskTemplateMan()
.GetTaskTemplByID(task_id);
if (pTempl == null)
return;
var tribute = pTempl.GetTribute();
if (string.IsNullOrEmpty(tribute))
return;
// register player name
EC_Game.GetGameRun().AddPlayerName(self_id, name);
string strMsg = tribute;
// replace $name
strMsg = strMsg.Replace("$name", $"&{name}&");
// replace $map
string strMap = "";
var instance = EC_Game.GetGameRun().GetInstance(map_id);
if (instance != null)
strMap = instance.GetName();
strMsg = strMsg.Replace("$map", strMap);
EC_Game.GetGameRun().AddChatMessage(strMsg, channel);
}*/
public class AUICTranslate
{
+8
View File
@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 3d2c00e6fd97fce4c8061a0c54327a5f
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,15 @@
using TMPro;
using UnityEngine;
namespace BrewMonster
{
public class ChatMessageView : MonoBehaviour
{
public TextMeshProUGUI messageText;
public void Bind(string message)
{
messageText.text = message;
}
}
}
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 179d32c667fc2f641bdcb7afb18046b9
@@ -0,0 +1,136 @@
using System.Collections.Generic;
using CSNetwork;
using UnityEngine;
using UnityEngine.Pool;
using UnityEngine.UI;
namespace BrewMonster.Scripts.ChatUI
{
public class ChatPanelUI : MonoBehaviour
{
[Header("UI")] public ScrollRect scrollRect;
public RectTransform content;
public ChatMessageView messagePrefab;
//public GameObject newMessageIndicator;
[Header("Config")] public int maxVisibleMessages = 30;
public int maxStoredMessages = 2000;
private List<string> _messages = new();
private List<ChatMessageView> _visibleViews = new();
private ObjectPool<ChatMessageView> _pool;
private bool _userAtBottom = true;
void Awake()
{
EventBus.Subscribe<GameSession.ChatMessageEvent>(
(x) => AddMessage(x.context));
_pool = new ObjectPool<ChatMessageView>(
CreateItem,
OnGetItem,
OnReleaseItem,
OnDestroyItem,
false,
10,
100
);
scrollRect.onValueChanged.AddListener(OnScrollChanged);
}
ChatMessageView CreateItem()
{
var item = Instantiate(messagePrefab);
item.transform.SetParent(content, false);
return item;
}
void OnGetItem(ChatMessageView item)
{
item.gameObject.SetActive(true);
}
void OnReleaseItem(ChatMessageView item)
{
item.gameObject.SetActive(false);
}
void OnDestroyItem(ChatMessageView item)
{
Destroy(item.gameObject);
}
void OnScrollChanged(Vector2 pos)
{
_userAtBottom = scrollRect.verticalNormalizedPosition <= 0.001f;
if (_userAtBottom)
{
//newMessageIndicator.SetActive(false);
}
}
bool IsAtBottom()
{
return scrollRect.verticalNormalizedPosition <= 0.001f;
}
public void AddMessage(string msg)
{
_messages.Add(msg);
if (_messages.Count > maxStoredMessages)
_messages.RemoveAt(0);
RefreshVisible();
if (_userAtBottom)
{
ScrollToBottom();
}
else
{
//newMessageIndicator.SetActive(true);
}
}
void RefreshVisible()
{
foreach (var view in _visibleViews)
_pool.Release(view);
_visibleViews.Clear();
int start = Mathf.Max(0, _messages.Count - maxVisibleMessages);
for (int i = start; i < _messages.Count; i++)
{
var view = _pool.Get();
view.transform.SetParent(content, false);
view.Bind(_messages[i]);
_visibleViews.Add(view);
}
Canvas.ForceUpdateCanvases();
}
public void ScrollToBottom()
{
Canvas.ForceUpdateCanvases();
scrollRect.verticalNormalizedPosition = 0f;
//newMessageIndicator.SetActive(false);
}
public void ClearChat()
{
foreach (var view in _visibleViews)
_pool.Release(view);
_visibleViews.Clear();
_messages.Clear();
}
}
}
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 5790dcd71bafcec4697f10b5366bec2c