遊戲內文字聊天 系統,由 TextChatService 提供支持,使玩家能夠在實時遊戲中輕鬆地進行交流和社交。除了支持默認的文字聊天功能之外,您還可以自訂前端用戶界面。
聊天視窗配置
整體聊天視窗由以下組成:
- 聊天視窗
- 輸入欄
- 頻道標籤(可選)

頻道標籤默認是禁用的,您可以在 Studio 或通過腳本來切換每個組件的開關:
在資源管理器視窗中,展開 TextChatService 分支,然後選擇 ChatWindowConfiguration、ChatInputBarConfiguration 或 ChannelTabsConfiguration。然後在屬性窗口中啟用或禁用組件。

當 ChannelTabsConfiguration 被啟用時,每個默認的 TextChannel 會顯示在一個標籤中,具體如下表所示。此外,每個自定義的 TextChannel 創建一個對應於頻道的 Name 屬性的標籤。
| 默認頻道 | 標籤名稱 |
|---|---|
| RBXGeneral | 一般 |
| RBXSystem | 一般(與 RBXGeneral 合併到一個標籤中) |
| RBXTeam | 隊伍 |
| RBXWhisper | 另一位玩家的用戶名 |
視窗外觀
整體聊天視窗的外觀可以通過 ChatWindowConfiguration 自訂。

| 屬性 | 描述 | 默認 |
|---|---|---|
| BackgroundColor3 | Color3 聊天視窗的背景顏色。 | [25, 27, 29] |
| BackgroundTransparency | 聊天視窗背景的透明度。 | 0.3 |
| Enabled | 是否顯示默認聊天視窗。設置為 false 以隱藏。 | true |
| FontFace | 聊天視窗文本的 Font。 | BuilderSansMedium |
| TextColor3 | 聊天視窗文本的 Color3。 | [255, 255, 255] |
| TextSize | 聊天視窗文本的大小。 | 14 |
| TextStrokeColor3 | 聊天視窗文本的描邊顏色。 | [0, 0, 0] |
| TextStrokeTransparency | 聊天視窗文本的描邊透明度。 | 0.5 |
| HorizontalAlignment | 聊天視窗的橫向對齊方式。 | Left |
| VerticalAlignment | 聊天視窗的縱向對齊方式。 | Top |
| HeightScale | 聊天視窗相對於螢幕大小的高度比例。 | 1 |
| WidthScale | 聊天視窗相對於螢幕大小的寬度比例。 | 1 |
輸入欄外觀
聊天輸入欄的外觀可以通過 ChatInputBarConfiguration 自訂。

| 屬性 | 描述 | 默認 |
|---|---|---|
| BackgroundColor3 | Color3 聊天輸入欄的背景顏色。 | [25, 27, 29] |
| BackgroundTransparency | 聊天輸入欄背景的透明度。 | 0.2 |
| FontFace | Font 聊天輸入文本的字體。 | BuilderSansMedium |
| PlaceholderColor3 | Color3 占位符聊天輸入文本的顏色。 | [178, 178, 178] |
| TextColor3 | Color3 玩家輸入的聊天文本的顏色。 | [255, 255, 255] |
| TextSize | 聊天輸入文本的大小。 | 14 |
| TextStrokeColor3 | Color3 聊天輸入文本的描邊顏色。 | [0, 0, 0] |
| TextStrokeTransparency | 聊天輸入文本所用描邊的透明度。 | 0.5 |
| AutocompleteEnabled | 文字聊天系統是否顯示表情符號和指令的自動補全選項。通過輸入 : 後跟非空白字符來自動補全表情符號,通過輸入 / 來自動補全指令。 | true |
| KeyboardKeyCode | 玩家可以按下的其他按鍵,以觸發聚焦到默認的聊天輸入欄。 | Slash |
頻道標籤外觀
頻道標籤的外觀可以通過 ChannelTabsConfiguration 自訂。

| 屬性 | 描述 | 默認 |
|---|---|---|
| BackgroundColor3 | Color3 頻道標籤的背景顏色。 | [25, 27, 29] |
| BackgroundTransparency | 頻道標籤背景的透明度。 | 0 |
| HoverBackgroundColor3 | Color3 在懸停時頻道標籤的背景顏色。 | [125, 125, 125] |
| FontFace | Font 頻道標籤中文字的字體。 | BuilderSansBold |
| TextColor3 | Color3 未選中標籤中文字的顏色。 | [175, 175, 175] |
| SelectedTabTextColor3 | Color3 選中標籤中文字的顏色。 | [255, 255, 255] |
| TextSize | 頻道標籤中文字的大小。 | 18 |
| TextStrokeColor3 | Color3 頻道標籤中文字的描邊顏色。 | [0, 0, 0] |
| TextStrokeTransparency | 頻道標籤中文字的描邊透明度。 | 1 |
自訂訊息
您可以使用 ChatWindowMessageProperties 和 TextChatService.OnChatWindowAdded 回調來自訂聊天訊息主體和前綴的外觀,而不會覆蓋現有的 UI。自訂選項允許您修改聊天訊息的外觀以匹配您的遊戲主題,並且您還可以通過顏色前綴或新增聊天標籤來對不同用戶組的訊息進行排序或突出顯示。
顏色用戶名
當用戶發送聊天訊息時,其 DisplayName 顯示為訊息的前綴部分。默認情況下,每個用戶的名稱根據其 Player.TeamColor 著色,但您可以使用 ChatWindowMessageProperties 和 OnChatWindowAdded 更改聊天名稱的顏色。以下的 LocalScript 在 StarterPlayerScripts 中為每個用戶分配一個預定的顏色,隨機選擇 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.CreateDefaultTextChannels 為 true 時,默認的文字頻道之一是 RBXSystem 頻道。默認的聊天腳本自動在此頻道中顯示系統消息。您可以通過 TextChannel.OnIncomingMessage 回調來自訂這些消息的外觀。
您可能希望自訂或更改聊天系統自動發出的系統消息。由於默認的系統消息是針對用戶本地化的,因此如果您希望自訂其外觀,則應在文字聊天回調中參考它們,通過 TextChatMessage.Metadata 來識別系統消息、錯誤消息或來自您遊戲中特定系統的消息。
系統
要將系統消息發送給本地玩家,例如來自公共地址系統的“講話”,請從默認的 RBXGeneral 頻道調用 DisplaySystemMessage(),並在玩家的顯示名稱之前加上前綴。
客戶端腳本local Players = game:GetService("Players")local TextChatService = game:GetService("TextChatService")local player = Players.LocalPlayerlocal generalChannel: TextChannel = TextChatService:WaitForChild("TextChannels").RBXGenerallocal 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)
