이 예제는 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)