この例では、TextChatServiceクラスを使用して独自のフロントエンドを設計する方法を示します。これは、CreateDefaultTextChannelsからデフォルトのテキストチャネルを再利用し、デフォルトのUIに比べて非常にシンプルです。
スタジオのプロパティウィンドウでChatWindowConfiguration.EnabledおよびChatInputBarConfiguration.Enabledプロパティをfalseに設定して、TextChatServiceに付属するデフォルトUIを無効にします。
チャット入力バーの代替品を作成します。これは、ユーザーがEnterを押すとメッセージを送信するテキストボックスです。
ScreenGuiを作成し、StarterGuiに親子付けします。
LocalScriptを作成し、新しいTextBoxに親子付けします。
次のコードをLocalScriptに追加します:
Clientlocal 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("メッセージの送信に失敗しました")end-- ユーザーはメッセージ送信後に入力ボックスがクリアされることを期待していますtextBox.Text = ""endend)
チャットウィンドウの代替品を作成します。これは、TextChatService.MessageReceivedから受信したメッセージを表示するScrollingFrameです。このステップでは、メッセージを自動的にレイアウトするためのUIListLayoutも作成します。
別の新しいScreenGuiを作成し、StarterGuiに親子付けします。
ScrollingFrameを作成し、ScreenGuiに親子付けし、次に再配置およびサイズ変更をします。
UIListLayoutを作成し、ScrollingFrameに親子付けします。
LocalScriptを作成し、ScrollingFrameに親子付けします。
次のコードをLocalScriptに追加します:
Clientlocal 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)