この例では、TextChatService クラスを使用して自分のフロントエンドを設計する方法を示しています。デフォルトのテキストチャンネルを CreateDefaultTextChannels から再利用し、デフォルトの UI と比べて非常にシンプルです。
スタジオの TextChatService ウィンドウで ChatWindowConfiguration.Enabled および ChatInputBarConfiguration.Enabled プロパティを false に設定して、デフォルトの UI である **** を無効にします。
チャット入力バーの置換を作成します。これは、ユーザーが Enter を押すとメッセージを発信するテキストボックスです。
Create a ScreenGui と親にして StarterGui 。
Create a 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 を付与します。
Create a ScrollingFrame と親にし、ScreenGui に移動し、必要に応じて 再配置 と サイズ変更 を行います。
Create a UIListLayout と親を ScrollingFrame にしてください。
Create a 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)