这个例子显示了如何使用 TextChatService 类来设计自己的前端。它重用了默认文本通道 CreateDefaultTextChannels 并且与默认 UI 相比很简单。
通过在 Studio 的 TextChatService 窗口设置 ChatWindowConfiguration.Enabled 和 ChatInputBarConfiguration.Enabled 为 false 来禁用带有 属性 的默认用户界面。
创建一个替换聊天输入栏的替换。这是用户按下 Enter 时发出消息的文本框。
创建一个 ScreenGui 并将其作为父级 StarterGui 。
创建一个 LocalScript 并将其作为新的 TextBox 的父级。
将以下代码添加到 LocalScript :
客户local TextChatService = game:GetService("TextChatService")-- RBXGeneral 是默认公共频道local RBXGeneral = TextChatService:FindFirstChild("TextChannels"):WaitForChild("RBXGeneral")local textBox = script.ParenttextBox.FocusLost:Connect(function(enterPressed)local text = textBox.Textif enterPressed and #text > 0 thenlocal success, response = pcall(function()return RBXGeneral:SendAsync(textBox.Text)end)if not success thenRBXGeneral:DisplaySystemMessage("Failed to send message")end-- 用户期望在发送消信息后清除输入框textBox.Text = ""endend)
创建一个替换聊天窗口的替换。这是显示从 ScrollingFrame 收到的消息的 TextChatService.MessageReceived 。此步骤还创建了一个 UIListLayout 来自动布置消息。
创建另一个新的 ScreenGui 并将其作为父级 StarterGui .
创建一个 ScrollingFrame 并将其作为父级传给 ScreenGui ,然后 重新定位 并 调整尺寸 以满足需求。
创建一个 UIListLayout 并将其作为父亲传给 ScrollingFrame 。
创建一个 LocalScript 并将其作为父亲传给 ScrollingFrame 。
将以下代码添加到 LocalScript :
客户local TextChatService = game:GetService("TextChatService")-- 创建每个收到的消息的新文本标签的函数local function addMessageGui(textChatMessage: TextChatMessage)local isOutgoingMessage = textChatMessage.Status == Enum.TextChatMessageStatus.Sendinglocal parent = script.Parentlocal originalLabel = parent:FindFirstChild(textChatMessage.MessageId)if originalLabel thenoriginalLabel.Text = textChatMessage.TextoriginalLabel.BackgroundTransparency = if isOutgoingMessage then 0.5 else 0elselocal textLabel = Instance.new("TextLabel")textLabel.BorderSizePixel = 0textLabel.Font = Enum.Font.BuilderSanstextLabel.TextSize = 18textLabel.TextXAlignment = Enum.TextXAlignment.LefttextLabel.BackgroundTransparency = if isOutgoingMessage then 0.5 else 0textLabel.BackgroundColor3 = Color3.fromRGB(0, 0, 0)textLabel.TextColor3 = Color3.fromRGB(255, 255, 255)textLabel.Name = textChatMessage.MessageIdtextLabel.AutomaticSize = Enum.AutomaticSize.XYtextLabel.Text = textChatMessage.TexttextLabel.Parent = parentendend-- 开始收听收到的消息TextChatService.MessageReceived:Connect(addMessageGui)