自訂聊天視窗

*此內容是使用 AI(Beta 測試版)翻譯,可能含有錯誤。若要以英文檢視此頁面,請按一下這裡

遊戲內文字聊天 系統,由 TextChatService 提供支持,使玩家能夠在實時遊戲中輕鬆地進行交流和社交。除了支持默認的文字聊天功能之外,您還可以自訂前端用戶界面。

聊天視窗配置

整體聊天視窗由以下組成:

  • 聊天視窗
  • 輸入欄
  • 頻道標籤(可選)
文字聊天視窗的核心組件。

頻道標籤默認是禁用的,您可以在 Studio 或通過腳本來切換每個組件的開關:

資源管理器視窗中,展開 TextChatService 分支,然後選擇 ChatWindowConfigurationChatInputBarConfigurationChannelTabsConfiguration。然後在屬性窗口中啟用或禁用組件。

ChannelTabsConfiguration 被啟用時,每個默認的 TextChannel 會顯示在一個標籤中,具體如下表所示。此外,每個自定義的 TextChannel 創建一個對應於頻道的 Name 屬性的標籤。

默認頻道標籤名稱
RBXGeneral一般
RBXSystem一般(與 RBXGeneral 合併到一個標籤中)
RBXTeam隊伍
RBXWhisper另一位玩家的用戶名

視窗外觀

整體聊天視窗的外觀可以通過 ChatWindowConfiguration 自訂。

Explorer 層級中的 ChatWindowConfiguration 實例。
屬性描述默認
BackgroundColor3Color3 聊天視窗的背景顏色。[25, 27, 29]
BackgroundTransparency聊天視窗背景的透明度。0.3
Enabled是否顯示默認聊天視窗。設置為 false 以隱藏。true
FontFace聊天視窗文本的 FontBuilderSansMedium
TextColor3聊天視窗文本的 Color3[255, 255, 255]
TextSize聊天視窗文本的大小。14
TextStrokeColor3聊天視窗文本的描邊顏色。[0, 0, 0]
TextStrokeTransparency聊天視窗文本的描邊透明度。0.5
HorizontalAlignment聊天視窗的橫向對齊方式。Left
VerticalAlignment聊天視窗的縱向對齊方式。Top
HeightScale聊天視窗相對於螢幕大小的高度比例。1
WidthScale聊天視窗相對於螢幕大小的寬度比例。1

輸入欄外觀

聊天輸入欄的外觀可以通過 ChatInputBarConfiguration 自訂。

Explorer 層級中的 ChatInputBarConfiguration 實例。
屬性描述默認
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 自訂。

Explorer 層級中的 ChannelTabsConfiguration 實例。
屬性描述默認
BackgroundColor3Color3 頻道標籤的背景顏色。[25, 27, 29]
BackgroundTransparency頻道標籤背景的透明度。0
HoverBackgroundColor3Color3 在懸停時頻道標籤的背景顏色。[125, 125, 125]
FontFaceFont 頻道標籤中文字的字體。BuilderSansBold
TextColor3Color3 未選中標籤中文字的顏色。[175, 175, 175]
SelectedTabTextColor3Color3 選中標籤中文字的顏色。[255, 255, 255]
TextSize頻道標籤中文字的大小。18
TextStrokeColor3Color3 頻道標籤中文字的描邊顏色。[0, 0, 0]
TextStrokeTransparency頻道標籤中文字的描邊透明度。1

自訂訊息

您可以使用 ChatWindowMessagePropertiesTextChatService.OnChatWindowAdded 回調來自訂聊天訊息主體和前綴的外觀,而不會覆蓋現有的 UI。自訂選項允許您修改聊天訊息的外觀以匹配您的遊戲主題,並且您還可以通過顏色前綴或新增聊天標籤來對不同用戶組的訊息進行排序或突出顯示。

顏色用戶名

當用戶發送聊天訊息時,其 DisplayName 顯示為訊息的前綴部分。默認情況下,每個用戶的名稱根據其 Player.TeamColor 著色,但您可以使用 ChatWindowMessagePropertiesOnChatWindowAdded 更改聊天名稱的顏色。以下的 LocalScriptStarterPlayerScripts 中為每個用戶分配一個預定的顏色,隨機選擇 RGB 顏色的表。

聊天視窗中的顏色名用戶名。
LocalScript - 隨機用戶名顏色

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 對訊息前綴應用顏色和透明度漸變。

聊天視窗中的漸變用戶名。
漸變用戶名顏色

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)移到訊息主體中,然後僅對名稱部分應用富文本標記。

聊天視窗中用戶名的富文本自訂。
富文本自訂

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 回調來自訂這些消息的外觀。

您可能希望自訂或更改聊天系統自動發出的系統消息。由於默認的系統消息是針對用戶本地化的,因此如果您希望自訂其外觀,則應在文字聊天回調中參考它們,通過 TextChatMessage.Metadata 來識別系統消息、錯誤消息或來自您遊戲中特定系統的消息。

系統

要將系統消息發送給本地玩家,例如來自公共地址系統的“講話”,請從默認的 RBXGeneral 頻道調用 DisplaySystemMessage(),並在玩家的顯示名稱之前加上前綴。

客戶端腳本

local Players = game:GetService("Players")
local TextChatService = game:GetService("TextChatService")
local player = Players.LocalPlayer
local generalChannel: TextChannel = TextChatService:WaitForChild("TextChannels").RBXGeneral
local PREFIX = "[指南] 歡迎 "
-- 將"系統消息"發送給玩家,後附其顯示名稱
generalChannel:DisplaySystemMessage(PREFIX .. player.DisplayName)
顯示聊天視窗中基本系統消息的圖片。

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 = "歡迎!我將是您的指導者。"
local speakerName = "古代騎士"
generalChannel:DisplaySystemMessage(message, speakerName)
顯示一個騎士雕像 NPC 向聊天視窗廣播聊天消息的圖片,並在其頭上有一個聊天泡泡。
©2026 Roblox Corporation、Roblox、Roblox 標誌及 Powering Imagination 是我們在美國及其他國家地區的部分註冊與未註冊商標。