自定义聊天窗口

*此内容使用人工智能(Beta)翻译,可能包含错误。若要查看英文页面,请点按 此处

体验文本聊天系统 驱动的,由 TextChatService 提供技术支持,玩家可以在实时体验中轻松交流和社交。除了支持默认文字聊天外,你还可以自定义前端用户界面。

聊天窗口配置

整个聊天窗口由以下组成:

  • 聊天窗口
  • 输入栏
  • 通道选项卡(可选)
Core components of the text chat window.

默认情况下,通道选项卡被禁用,每个组件可以在 Studio 或通过脚本开启和关闭:

资源管理器 窗口中,扩展 TextChatService 分支,然后选择 ChatWindowConfiguration , ChatInputBarConfigurationChannelTabsConfiguration .然后在 属性 窗口中启用或禁用组件。

ChannelTabsConfiguration 启用时,每个默认 TextChannel 在下表中列出的选项卡中出现。此外,每个自定义 TextChannel 创建一个与通道的 Name 属性相对应的选项卡。

默认通道标签名称
RBXGeneral 一般
RBXSystem 一般 (结合成单个标签与 RBXGeneral )
RBXTeam 团队
RBXWhisper其他玩家的用户名

窗口外观

整个聊天窗口的外观可通过 ChatWindowConfiguration 进行自定义。

ChatWindowConfiguration instance in Explorer hierarchy.
属性描述默认
BackgroundColor3Color3 聊天窗口的背景颜色。[25, 27, 29]
BackgroundTransparency聊天窗口的背景透明。0.3
FontFaceFont 聊天窗口文本。BuilderSansMedium
TextColor3Color3 聊天窗口文本。[255, 255, 255]
TextSize聊天窗口文本的大小。14
TextStrokeColor3Color3 杆的聊天窗口文本的画笔。[0, 0, 0]
TextStrokeTransparency聊天窗口文本的画笔透明度。0.5
HorizontalAlignment聊天窗口的横向对齐。Left
VerticalAlignment聊天窗口的垂直对齐。Top
HeightScale聊天窗口与屏幕尺寸相对的高度缩放。1
WidthScale聊天窗口对屏幕尺寸的宽度比例。1

输入栏显示

聊天输入栏的外观可通过 ChatInputBarConfiguration 进行自定义。

ChatInputBarConfiguration instance in Explorer hierarchy.
属性描述默认
BackgroundColor3Color3 聊天输入栏的背景颜色。[25, 27, 29]
BackgroundTransparency聊天输入栏的背景透明。0.2
FontFaceFont 聊天输入文本。BuilderSansMedium
PlaceholderColor3Color3 占位符聊天输入文本。[178, 178, 178]
TextColor3Color3 玩家输入的聊天输入文本。[255, 255, 255]
TextSize聊天输入文本的大小。14
TextStrokeColor3Color3 聊天输入文本的边框颜色。[0, 0, 0]
TextStrokeTransparency聊天输入文本的画笔透明度。0.5
AutocompleteEnabled文字聊天系统是否显示了对表情符号和 命令 的自动完成选项。表情符号通过输入 : 以及非空白字符自动完成,而命令通过输入 / 自动完成。true
KeyboardKeyCode额外的关键玩家可以按下以触发关注默认聊天输入栏的操作。Slash

通道选项卡外观

通道选项卡 的外观可通过 ChannelTabsConfiguration 进行自定义。

ChannelTabsConfiguration instance in Explorer hierarchy.
属性描述默认
BackgroundColor3Color3 通道选项卡的背景颜色。[25, 27, 29]
BackgroundTransparency通道选项卡的背景透明。0
HoverBackgroundColor3Color3 选项卡在悬停时的背景颜色。[125, 125, 125]
FontFaceFont 对于通道选项卡中的文本。BuilderSansBold
TextColor3Color3 在未选择标签的文本。[175, 175, 175]
SelectedTabTextColor3Color3 选定标签中的文本。[255, 255, 255]
TextSize通道选项卡中文本的大小。18
TextStrokeColor3Color3 通道选项卡中文本的stroke颜色。[0, 0, 0]
TextStrokeTransparency通道选项卡中文本的画笔透明度。1

自定义消息

您可以使用 ChatWindowMessagePropertiesTextChatService.OnChatWindowAdded 回调来自定义聊天消息体和前缀的外观,而不会覆盖现有的用户界面。自定义选项可让您修改聊天消息的外观,以匹配您的体验主题,您还可以通过着色前缀或添加 聊天标签 来排序或突出不同用户群的聊天消息。

颜色用户名称

当用户发送聊天消息时,其 DisplayName 显示为消息的前缀部分。默认情况下,每个用户的名称都会根据其 Player.TeamColor 而被颜色化,但您可以使用 ChatWindowMessagePropertiesOnChatWindowAdded 来更改聊天名称的颜色。以下 LocalScriptStarterPlayerScripts 为每个用户分配预定义颜色,从 RGB 颜色表中随机选择。

Colored user name in the chat window.
本地脚本 - 随机用户名颜色

local TextChatService = game:GetService("TextChatService")
local chatWindowConfiguration = TextChatService.ChatWindowConfiguration
local nameColors = {
Color3.fromRGB(255, 0, 0),
Color3.fromRGB(0, 255, 0),
Color3.fromRGB(0, 0, 255),
Color3.fromRGB(255, 255, 0),
}
TextChatService.OnChatWindowAdded = function(message: TextChatMessage)
local properties = chatWindowConfiguration:DeriveNewMessageProperties()
local textSource = message.TextSource
if textSource then
local index: number = (textSource.UserId % #nameColors) + 1
local randomColor: Color3 = nameColors[index]
properties.PrefixTextProperties = chatWindowConfiguration:DeriveNewMessageProperties()
properties.PrefixTextProperties.TextColor3 = randomColor
end
return properties
end

您还可以使用 UIGradient 将颜色和透明度渐变应用到颜色消息前缀上。

Gradient user name in the chat window.
渐变用户名颜色

local TextChatService = game:GetService("TextChatService")
local chatWindowConfiguration = TextChatService.ChatWindowConfiguration
local gradient = Instance.new("UIGradient")
gradient.Color = ColorSequence.new{
ColorSequenceKeypoint.new(0, Color3.fromRGB(255, 0, 0)),
ColorSequenceKeypoint.new(0.5, Color3.fromRGB(255, 255, 0)),
ColorSequenceKeypoint.new(1, Color3.fromRGB(255, 0, 255))
}
TextChatService.OnChatWindowAdded = function(message: TextChatMessage)
local properties = chatWindowConfiguration:DeriveNewMessageProperties()
local textSource = message.TextSource
if textSource then
properties.PrefixTextProperties = chatWindowConfiguration:DeriveNewMessageProperties()
gradient:Clone().Parent = properties.PrefixTextProperties
end
return properties
end

富文本自定义

富文本 字体颜色标签 可用于格式化聊天消息,如果你想对消息的特定部分进行格式化,这很有用。请注意,富文本不支持渐变,但以下代码示例显示了您如何将用户名(存储在 TextChatMessage.PrefixText )移至消息体并应用富文本标记仅于名称部分。

Rich text customization of user name in the chat window.
富文本自定义

local TextChatService = game:GetService("TextChatService")
local Players = game:GetService("Players")
local chatWindowConfiguration = TextChatService.ChatWindowConfiguration
local gradient = Instance.new("UIGradient")
gradient.Color = ColorSequence.new{
ColorSequenceKeypoint.new(0, Color3.fromRGB(255, 0, 0)),
ColorSequenceKeypoint.new(0.5, Color3.fromRGB(255, 255, 0)),
ColorSequenceKeypoint.new(1, Color3.fromRGB(255, 0, 255))
}
TextChatService.OnChatWindowAdded = function(message: TextChatMessage)
local properties = chatWindowConfiguration:DeriveNewMessageProperties()
if message.TextSource then
properties.PrefixText = "[VIP]"
properties.Text = string.format("<font color='#00ffff'>%s</font>", message.PrefixText) .. " " .. message.Text
properties.PrefixTextProperties = chatWindowConfiguration:DeriveNewMessageProperties()
gradient:Clone().Parent = properties.PrefixTextProperties
end
return properties
end

来自非玩家来源的消息

TextChatService.CreateDefaultTextChannelstrue 时,默认文本通道之一是 RBXSystem 通道。默认聊天脚本会自动在此通道中显示系统消息。您可以使用 TextChannel.OnIncomingMessage 回调来自定义这些消息的外观。

您可能想自定义或更改聊天系统自动发出的系统消息。由于默认系统消息已为用户本地化,因此如果您想自定义其外观,您应该在 文本聊天回调中引用它们 如果您想自定义其外观。例如,你可以使用 Metadata 来识别体验中的系统消息、错误消息或特定系统的消息。

系统

要将系统消息发送到本地玩家,例如公共地址系统的“演讲”,请从默认的 DisplaySystemMessage() 通道使用前缀显示玩家显示名称前调用 RBXGeneral

客户端脚本

local Players = game:GetService("Players")
local TextChatService = game:GetService("TextChatService")
local player = Players.LocalPlayer
local generalChannel: TextChannel = TextChatService:WaitForChild("TextChannels").RBXGeneral
local PREFIX = "[Guide] Welcome "
-- 向带有显示名称的玩家发送“系统消息”
generalChannel:DisplaySystemMessage(PREFIX .. player.DisplayName)
Image showing a basic system message in the chat window.

NPC/对象

您还可以为非玩家对话添加样式,并添加 聊天泡泡 以使其看起来像是来自 3D 世界内的 NPC 或对象。

客户端脚本

local TextChatService = game:GetService("TextChatService")
local Workspace = game:GetService("Workspace")
local generalChannel: TextChannel = TextChatService:WaitForChild("TextChannels").RBXGeneral
TextChatService.OnIncomingMessage = function(textChatMessage: TextChatMessage)
local properties = Instance.new("TextChatMessageProperties")
-- 检查包含元数据的系统消息
if not textChatMessage.TextSource and textChatMessage.Metadata ~= "" then
-- 添加前缀以使消息看起来像是由玩家发送
properties.PrefixText = string.format("<font color='#%s'>%s: </font>", "#50C999", textChatMessage.Metadata)
-- 添加对话泡泡
TextChatService:DisplayBubble(Workspace.Statue, textChatMessage.Text)
end
return properties
end
local message = "Welcome! I will be your guide."
local speakerName = "Ancient Knight"
generalChannel:DisplaySystemMessage(message, speakerName)
Image showing a knight statue NPC broadcasting a chat message to the chat window, along with a chat bubble above its head.