unity 聊天界面

unity 聊天界面

这段代码是用于实现一个简单的聊天界面的。它包括了三个主要的部分:WorldChatManager.csWorldOtherPeopleChatItem.csWorldOtherPeopleChatItem.cs。下面是对每个部分的详细解释:

1. WorldChatManager.cs

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class WorldChatManager : MonoBehaviour { public GameObject chatPanel; // 聊天面板 public ScrollRect scrollRect; // 滚动条 public InputField inputField; // 输入框 private List<GameObject> otherPeopleChatItems = new List<GameObject>(); // 存储其他人的聊天项 void Start() { // 初始化输入框的监听器 inputField.onEndEdit.AddListener(SendMessage); } void Update() { // 当有新消息时,自动滚动到最底部 if (scrollRect.verticalNormalizedPosition == 0) { scrollRect.verticalNormalizedPosition = 1; } } void SendMessage(string message) { if (!string.IsNullOrEmpty(message)) { GameObject chatItem = Instantiate(chatPanel, scrollRect.content); Text msgText = chatItem.GetComponentInChildren<Text>(); msgText.text = "You: " + message; // 清空输入框 inputField.text = ""; inputField.ActivateInputField(); } } public void AddOtherPeopleChat(string message) { GameObject chatItem = Instantiate(chatPanel, scrollRect.content); Text msgText = chatItem.GetComponentInChildren<Text>(); msgText.text = "Other: " + message; otherPeopleChatItems.Add(chatItem); } } 
  • chatPanel: 这是一个预制体,用于创建聊天项。
  • scrollRect: 滚动条组件,用于控制聊天内容的滚动。
  • inputField: 输入框组件,用户输入消息的地方。

SendMessage 方法会在用户按下回车键或点击发送按钮时被调用,将用户输入的消息显示在聊天面板中,并清空输入框。

2. WorldOtherPeopleChatItem.cs

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class WorldOtherPeopleChatItem : MonoBehaviour { public Text m_PlayerName_Txt; // 玩家名称文本 public Image m_HeadIcon_Img; // 头像图片 public Button m_HeadIcon_Btn; // 头像按钮 public Image m_DialogBg_Img; // 对话框背景图片 public Text m_Msg_Txt; // 消息文本 public void Init(string name, string message) { gameObject.SetActive(true); m_PlayerName_Txt.text = name; m_Msg_Txt.text = "Other: " + message; width = m_DialogBg_Img.GetComponent<RectTransform>().rect.width; textGenerator = m_Msg_Txt.cachedTextGeneratorForLayout; OnText(); } void OnText() { TextGenerationSettings settings = m_Msg_Txt.GetGenerationSettings(Vector2.zero); Canvas.ForceUpdateCanvases(); float textHeight = m_Msg_Txt.GetComponent<RectTransform>().rect.height; float textWidth = textGenerator.GetPreferredWidth(m_Msg_Txt.text, settings) / m_Msg_Txt.pixelsPerUnit; if (textWidth <= width) { m_DialogBg_Img.GetComponent<RectTransform>().sizeDelta = new Vector2(textWidth + 80, textHeight + 30); m_DialogBg_Img.GetComponent<RectTransform>().anchoredPosition = new Vector2((textWidth / 2) + 200, -60); } else { m_DialogBg_Img.GetComponent<RectTransform>().sizeDelta = new Vector2(width + 80, textHeight + 30); m_DialogBg_Img.GetComponent<RectTransform>().anchoredPosition = new Vector2((width / 2) + 200, -60); } } public void LateUpdate() { if (isShow) { gameObject.GetComponent<RectTransform>().sizeDelta = new Vector2(gameObject.GetComponent<RectTransform>().rect.width, m_Msg_Txt.GetComponent<RectTransform>().rect.height + 100); isShow = false; } } } 
  • m_PlayerName_Txt: 玩家名称文本。
  • m_HeadIcon_Img: 头像图片。
  • m_DialogBg_Img: 对话框背景图片。
  • m_Msg_Txt: 消息文本。

Init 方法用于初始化聊天项,设置玩家名称和消息内容。OnText 方法根据消息的长度调整对话框的大小和位置。

3. WorldOtherPeopleChatItem.cs

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class WorldOtherPeopleChatItem : MonoBehaviour { public Text m_PlayerName_Txt; // 玩家名称文本 public Image m_HeadIcon_Img; // 头像图片 public Button m_HeadIcon_Btn; // 头像按钮 public Image m_DialogBg_Img; // 对话框背景图片 public Text m_Msg_Txt; // 消息文本 public void Init(string name, string message) { gameObject.SetActive(true); m_PlayerName_Txt.text = name; m_Msg_Txt.text = "Other: " + message; width = m_DialogBg_Img.GetComponent<RectTransform>().rect.width; textGenerator = m_Msg_Txt.cachedTextGeneratorForLayout; OnText(); } void OnText() { TextGenerationSettings settings = m_Msg_Txt.GetGenerationSettings(Vector2.zero); Canvas.ForceUpdateCanvases(); float textHeight = m_Msg_Txt.GetComponent<RectTransform>().rect.height; float textWidth = textGenerator.GetPreferredWidth(m_Msg_Txt.text, settings) / m_Msg_Txt.pixelsPerUnit; if (textWidth <= width) { m_DialogBg_Img.GetComponent<RectTransform>().sizeDelta = new Vector2(textWidth + 80, textHeight + 30); m_DialogBg_Img.GetComponent<RectTransform>().anchoredPosition = new Vector2((textWidth / 2) + 200, -60); } else { m_DialogBg_Img.GetComponent<RectTransform>().sizeDelta = new Vector2(width + 80, textHeight + 30); m_DialogBg_Img.GetComponent<RectTransform>().anchoredPosition = new Vector2((width / 2) + 200, -60); } } public void LateUpdate() { if (isShow) { gameObject.GetComponent<RectTransform>().sizeDelta = new Vector2(gameObject.GetComponent<RectTransform>().rect.width, m_Msg_Txt.GetComponent<RectTransform>().rect.height + 100); isShow = false; } } } 

这个部分与 WorldOtherPeopleChatItem.cs 完全相同,可能是复制粘贴错误。

总结

这段代码实现了一个简单的聊天界面,包括发送消息和显示其他人的消息。用户在输入框中输入消息并按下回车键时,消息会显示在聊天面板中。其他人的消息通过调用 AddOtherPeopleChat 方法添加到聊天面板中。每个聊天项根据消息的长度调整大小和位置。

Read more

前端防范 XSS(跨站脚本攻击)

目录 一、防范措施 1.layui util  核心转义的特殊字符 示例 2.js-xss.js库 安装 1. Node.js 环境(npm/yarn) 2. 浏览器环境 核心 API 基础使用 1. 基础过滤(默认规则) 2. 自定义过滤规则 (1)允许特定标签 (2)允许特定属性 (3)自定义标签处理 (4)自定义属性处理 (5)转义特定字符 常见场景示例 1. 过滤用户输入的评论内容 2. 允许特定富文本标签(如富文本编辑器内容) 注意事项 更多配置 XSS(跨站脚本攻击)是一种常见的网络攻击手段,它允许攻击者将恶意脚本注入到其他用户的浏览器中。

详细教程:如何从前端查看调用接口、传参及返回结果(附带图片案例)

详细教程:如何从前端查看调用接口、传参及返回结果(附带图片案例)

目录 1. 打开浏览器开发者工具 2. 使用 Network 面板 3. 查看具体的API请求 a. Headers b. Payload c. Response d. Preview e. Timing 4. 实际操作步骤 5. 常见问题及解决方法 a. 无法看到API请求 b. 请求失败 c. 跨域问题(CORS) 作为一名后端工程师,理解前端如何调用接口、传递参数以及接收返回值是非常重要的。下面将详细介绍如何通过浏览器开发者工具(F12)查看和分析这些信息,并附带图片案例帮助你更好地理解。 1. 打开浏览器开发者工具 按下 F12 或右键点击页面选择“检查”可以打开浏览器的开发者工具。常用的浏览器如Chrome、Firefox等都内置了开发者工具。下面是我选择我的一篇文章,打开开发者工具进行演示。 2. 使用

Cursor+Codex隐藏技巧:用截图秒修前端Bug的保姆级教程(React/Chakra UI案例)

Cursor+Codex隐藏技巧:用截图秒修前端Bug的保姆级教程(React/Chakra UI案例) 前端开发中最令人头疼的莫过于那些难以定位的UI问题——元素错位、样式冲突、响应式失效...传统调试方式往往需要反复修改代码、刷新页面、检查元素。现在,通过Cursor编辑器集成的Codex功能,你可以直接用截图交互快速定位和修复这些问题。本文将带你从零开始,掌握这套革命性的调试工作流。 1. 环境准备与基础配置 在开始之前,确保你已经具备以下环境: * Cursor编辑器最新版(v2.5+) * Node.js 18.x及以上版本 * React 18项目(本文以Chakra UI 2.x为例) 首先在Cursor中安装Codex插件: 1. 点击左侧扩展图标 2. 搜索"Codex"并安装 3. 登录你的OpenAI账户(需要ChatGPT Plus订阅) 关键配置项: // 在项目根目录创建.