使用 TextChatService,您可以使用泡泡聊天显示上述用户虚拟形象和 NPC 的可定制语音聊天泡泡。泡泡聊天可以让您的体验更具视觉沉浸感,并帮助用户轻松识别信息和发言者在上下文相关的方式。此功能特别适合那些需要让用户专注于内容的体验,同时通过较少显著的方式与其他人沟通。
启用泡泡聊天
要启用体验中的泡泡聊天:
在 资源管理器 窗口中,选择 BubbleChatConfiguration 下面的 TextChatService .
在 属性 窗口中,检查 BubbleChatConfiguration.Enabled 复选框。
泡泡定制
启用泡泡聊天后,您可以自定义聊天泡泡的外观和行为,使其与体验主题匹配。使用 属性 窗口的 基本 变化,例如文本颜色和间距,或实现 高级 自定义对泡泡背景图像和其他视觉调整的其他调整。

或者,在 StarterPlayerScripts 中添加一个 LocalScript 以及所有的自定义设置。这允许引擎在执行时间时应用您的自定义,覆盖 Studio 中的设置。当用户触发特定事件或条件时,添加特殊效果到聊天泡泡有用。
基本定制
下表显示了常见的泡泡聊天自定义属性。要查看完整的自定义属性列表,请参阅 BubbleChatConfiguration 。
属性 | 描述 | 默认 |
---|---|---|
BackgroundColor3 | Color3中泡泡的背景颜色。 | [250, 250, 250] |
FontFace | Font 泡泡文本。 | BuilderSansMedium |
TextColor3 | Color3中泡泡文本的颜色。 | [57, 59, 61] |
TextSize | 泡泡文本的大小。 | 16 |
高级自定义
为了进一步自定义您的泡泡,添加 UI 对象代表泡泡外观的某些方面作为子对象 BubbleChatConfiguration ,包括:
- ImageLabel 用于背景图像设置。
- UIGradient 用于背景渐变设置。
- UICorner 对泡泡的角形状。
- UIPadding 为文本和泡泡边缘之间的填充空间,相对于父父元素的正常尺寸。
添加这些对象后,您可以修改这些对象的属性,适用于聊天泡泡的高级泡泡自定义。以下示例 LocalScript 添加了背景图像和锋利角落到泡泡:
高级泡泡定制
local TextChatService = game:GetService("TextChatService")local bubbleChatConfiguration = TextChatService.BubbleChatConfigurationbubbleChatConfiguration.TailVisible = falsebubbleChatConfiguration.TextColor3 = Color3.fromRGB(220, 50, 50)bubbleChatConfiguration.FontFace = Font.fromEnum(Enum.Font.LuckiestGuy)local bubbleUICorner = bubbleChatConfiguration:FindFirstChildOfClass("UICorner")if not bubbleUICorner thenbubbleUICorner = Instance.new("UICorner")bubbleUICorner.Parent = bubbleChatConfigurationendbubbleUICorner.CornerRadius = UDim.new(0, 0)local bubbleUIPadding = bubbleChatConfiguration:FindFirstChildOfClass("UIPadding")if not bubbleUIPadding thenbubbleUIPadding = Instance.new("UIPadding")bubbleUIPadding.Parent = bubbleChatConfigurationendbubbleUIPadding.PaddingTop = UDim.new(0, 20)bubbleUIPadding.PaddingRight = UDim.new(0, 10)bubbleUIPadding.PaddingBottom = UDim.new(0, 15)bubbleUIPadding.PaddingLeft = UDim.new(0, 10)local bubbleImageLabel = bubbleChatConfiguration:FindFirstChildOfClass("ImageLabel")if not bubbleImageLabel thenbubbleImageLabel = Instance.new("ImageLabel")bubbleImageLabel.Parent = bubbleChatConfigurationendbubbleImageLabel.Image = "rbxassetid://109157529833093"bubbleImageLabel.ScaleType = Enum.ScaleType.SlicebubbleImageLabel.SliceCenter = Rect.new(40, 40, 320, 120)bubbleImageLabel.SliceScale = 0.5

以下表格概述了可用的 GuiObject 和 外观调整器 孩子以及其有效的自定义属性:
属性 | 描述 | 默认 |
---|---|---|
Image | 泡泡背景图像的资产ID。 | |
ImageColor3 | 泡泡背景图像的颜色淡化在 Color3 。 | [255, 255, 255] |
ImageRectOffset | 从左上角显示的图像区域的抵消在像素上。 | (0, 0) |
ImageRectSize | 图像区域的像素显示尺寸。要显示整个图像,将任何维度设置为 0 。 | (0, 0) |
ScaleType | 当图像尺寸与泡泡绝对尺寸不同时渲染图像的缩放类型。 | Stretch |
SliceCenter | 如果图像是 9 切片图像,切割图像边界。仅当你将 ScaleType 设置为 Slice 时才适用。 | (0, 0, 0, 0) |
SliceScale | 如果图像是 9 片分割图像,切割边的比例率。仅当你将 ScaleType 设置为 Slice 时才适用。 | 1 |
TileSize | 图像的砖块大小。仅当你将 ScaleType 设置为 Tile 时才适用。 | (1, 0, 1, 0) |
每个泡泡的定制
您可以根据特定条件单独设置和修改聊天泡泡行为,以覆盖您的一般设置。例如,你可以使用聊天泡泡来区分 NPC 和用户、突出重要的生命状态,并对预定义关键字的消息应用特殊效果。
要设置每个泡泡的自定义,请添加客户端 LocalScript 使用 BubbleChatMessageProperties 来覆盖 BubbleChatConfiguration 匹配的属性,并将 TextChatService.OnBubbleAdded 回调指定如何自定义每个泡泡。回调向你提供 TextChatMessage 属性以及装饰者,因此你可以根据与用户相关的属性、聊天文本内容、用户角色属性以及任何你想定义的特殊条件来应用自定义。
以下基本自定义属性可用于每个泡泡的自定义:
属性 | 描述 | 默认 |
---|---|---|
BackgroundColor3 | Color3中泡泡的背景颜色。 | (250, 250, 250) |
BackgroundTransparency | 泡泡的背景透明。 | 0.1 |
FontFace | Font 泡泡文本。 | BuilderSansMedium |
TextColor3 | Color3中泡泡文本的颜色。 | [57, 59, 61] |
TextSize | 泡泡文本的大小。 | 16 |
以下示例添加特殊外观到 VIP 用户的聊天泡泡,检查聊天消息发件人是否具有 IsVIP 属性:
VIP泡泡
local TextChatService = game:GetService("TextChatService")
local Players = game:GetService("Players")
-- 当新聊天泡泡添加到体验时的事件处理器
TextChatService.OnBubbleAdded = function(message: TextChatMessage, adornee: Instance)
-- 检查聊天消息是否具有与之相关的文本来源(发送者)
if message.TextSource then
-- 创建一个新的 BubbleChatMessageProperties 实例来自定义聊天泡泡
local bubbleProperties = Instance.new("BubbleChatMessageProperties")
-- 根据用户的用户ID获得发送聊天消息的用户
local player = Players:GetPlayerByUserId(message.TextSource.UserId)
if player:GetAttribute("IsVIP") then
-- 如果玩家是 VIP,自定义聊天泡泡属性
bubbleProperties.TextColor3 = Color3.fromHex("#F5CD30")
bubbleProperties.BackgroundColor3 = Color3.fromRGB(25, 27, 29)
bubbleProperties.FontFace = Font.fromEnum(Enum.Font.PermanentMarker)
end
return bubbleProperties
end
end
所有高级自定义选项都可用于每个泡泡的自定义。与通用泡泡的高级自定义类似,添加您想要自定义为 BubbleChatMessageProperties 子泡泡的实例。以下示例添加了特殊的渐变效果以及其他属性,用于检查聊天消息发送者角色的 Humanoid.Health 属性来与健康状态低的用户聊天泡泡:
低生命泡泡
local TextChatService = game:GetService("TextChatService")
local Players = game:GetService("Players")
-- 当新聊天泡泡添加到体验时的事件处理器
TextChatService.OnBubbleAdded = function(message: TextChatMessage, adornee: Instance)
-- 检查聊天消息是否具有与之相关的文本来源(发送者)
if message.TextSource then
-- 使用用户的用户ID来获取发送聊天消息的用户
local player = Players:GetPlayerByUserId(message.TextSource.UserId)
-- 在用户的角色中找到人形怪物
local humanoid = player.Character:FindFirstChildWhichIsA("Humanoid")
if humanoid and humanoid.Health < 25 then
-- 创建一个新的 BubbleChatMessageProperties 实例来自定义聊天泡泡
local bubbleProperties :BubbleChatMessageProperties = Instance.new("BubbleChatMessageProperties")
-- 为低生命值状态自定义聊天泡泡属性
bubbleProperties.BackgroundColor3 = Color3.fromRGB(245, 245, 245)
bubbleProperties.TextColor3 = Color3.fromRGB(234, 51, 96)
bubbleProperties.TextSize = 20
bubbleProperties.FontFace = Font.fromEnum(Enum.Font.DenkOne)
-- 将 UIGradient 作为子添加到 Gradient 来自定义渐变
local uiGradient : UIGradient = Instance.new("UIGradient")
uiGradient.Color = ColorSequence.new(Color3.fromRGB(110, 4, 0), Color3.fromRGB(0, 0, 0))
uiGradient.Rotation = 90
uiGradient.Parent = bubbleProperties
return bubbleProperties
end
end
end
手动显示泡泡
您可能想在玩家还没有发送消信息时显示聊天泡泡,例如与 NPC 交互。使用 TextChatService:DisplayBubble() 方法手动显示聊天泡泡。
这些泡泡的自定义与使用 TextChatService.OnBubbleAdded 回调 发送消息时自动显示的泡泡的自定义相同。
NPC泡泡
通过调用 TextChatService:DisplayBubble(character, message) 显示非玩家角色(NPC)的聊天泡泡,其中 NPC 角色和消息作为参数。这些泡泡可以使用 TextChatService.OnBubbleAdded 回调与任何其他聊天泡泡一样自定义。
TextChatService:DisplayBubble() 仅适用于客户端脚本,因此请确保使用 Script 与 RunContext 设置为 Enum.RunContext.Client 或 LocalScript 在适当的容器中 ,例如 StarterPlayerScripts 。如果您将 ProximityPrompt 附加到 NPC,显示聊天泡泡的脚本可能会像这样:
local TextChatService = game:GetService("TextChatService")
local Workspace = game:GetService("Workspace")
local prompt = Workspace.SomeNPC.ProximityPrompt
local head = prompt.Parent:WaitForChild("Head")
prompt.Triggered:Connect(function()
TextChatService:DisplayBubble(head, "Hello world!")
end)