이 예제는 TextChatService 클래스를 사용하여 자신만의 프런트엔드를 디자인하는 방법을 보여줍니다. 이는 CreateDefaultTextChannels의 기본 텍스트 채널을 재사용하며 기본 UI에 비해 매우 간단합니다.
Studio의 속성 창에서 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)
채팅 창의 대체품을 만듭니다. 이는 ScrollingFrame으로, TextChatService.MessageReceived로부터 수신된 메시지를 표시합니다. 이 단계에서는 메시지를 자동으로 배치하는 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)