Files
Fishing2/Assets/Scripts/UI/Chat/ChatTestPanel.cs
2026-03-11 10:15:59 +08:00

115 lines
3.5 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 本脚本只在不存在时会生成一次。已存在不会再次生成覆盖
using System;
using System.Collections.Generic;
using System.Text;
using FairyGUI;
using Fantasy;
using Fantasy.Async;
using Fantasy.Network;
using NBC;
using UnityEngine;
using Log = NBC.Log;
using UIPanel = NBC.UIPanel;
namespace NBF
{
public partial class ChatTestPanel : UIPanel
{
private List<string> _messages = new List<string>();
protected override void OnInit()
{
this.AutoAddClick(OnClick);
}
protected override void OnShow()
{
}
private void RefreshList()
{
ChatList.RemoveChildrenToPool();
foreach (var item in _messages)
{
if (ChatList.AddItemFromPool() is ChatItem chatItem)
{
chatItem.InitData(item);
}
}
}
private void OnClick(GComponent btn)
{
if (btn == BtnSendWorld)
{
OnSendMessage(InputMessage.title).Coroutine();
}
else if (btn == BtnSendFriend)
{
OnSendFriendMessage().Coroutine();
}
}
#region
private async FTask OnSendMessage(string message)
{
var tree = ChatTreeFactory.Broadcast(Game.Main);
tree = tree.AddendPositionNode(message, "勇者大陆", 121, 131, 111);
var response = (Game2C_SendMessageResponse)await Net.Call(new C2Game_SendMessageRequest()
{
ChatInfoTree = tree
});
if (response.ErrorCode != 0)
{
Log.Error($"发送聊天消息失败 ErrorCode:{response.ErrorCode}");
}
else
{
Log.Info("发送消息成功");
}
}
private async FTask OnSendFriendMessage()
{
var tree = ChatTreeFactory.Private();
tree.Target.Add(Convert.ToInt64(InputFriendId.text));
tree.AddendTextNode("你好欢迎来到Fantasy Chat")
.AddendLinkNode("点击这里http://www.fantasy.com.cn", "http://www.fantasy.com.cn");
var response = (Game2C_SendMessageResponse)await Net.Call(new C2Game_SendMessageRequest()
{
ChatInfoTree = tree
});
if (response.ErrorCode != 0)
{
Log.Error($"发送私聊消息失败 ErrorCode:{response.ErrorCode}");
}
}
public void Message(ChatInfoTree tree)
{
// _messages.Add(message);
var sb = new StringBuilder();
foreach (var chatInfoNode in tree.Node)
{
// 这里只是演示一下处理事件的效果,实际使用时,需要根据实际情况处理事件
// 明显我现在这样做的方式不是对的,应该是自己拼接一个聊天信息,然后调用这个接口来处理事件
// entryComponent.Entry.ChatNodeEventButton.onClick.RemoveAllListeners();
// entryComponent.Entry.ChatNodeEventButton.onClick.AddListener(() =>
// {
// ChatNodeEventHelper.Handler(scene, chatInfoNode);
// });
sb.Append(chatInfoNode.Content);
}
_messages.Add(sb.ToString());
RefreshList();
}
#endregion
}
}