هذا المثال يوضح كيفية استخدام TextChatService لتصميم الواجهة الأمامية الخاصة بك. يعيد استخدام قنوات النص الافتراضية من CreateDefaultTextChannels وهو بسيط جدًا مقارنةً بالواجهة الافتراضية.
قم بتعطيل واجهة المستخدم الافتراضية التي تأتي مع TextChatService عن طريق تعيين الخصائص ChatWindowConfiguration.Enabled و ChatInputBarConfiguration.Enabled إلى false في نافذة الخصائص في الاستوديو.
أنشئ بديلاً لشريط إدخال الدردشة. هذه هي مربع النص الذي يرسل الرسائل عندما يضغط المستخدم على Enter.
أنشئ ScreenGui واربطه بـ StarterGui.
أنشئ TextBox واربطه بـ ScreenGui الجديدة، ثم قم بإعادة موضعه و تغيير حجمه كما ترغب.
أنشئ 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)
اختبر لعبتك بإرسال رسائل في شريط إدخال الدردشة. يجب أن ترى الرسائل تظهر في نافذة الدردشة. يمكنك بعد ذلك توسيع هذا المثال عن طريق إضافة ميزات مثل التركيز على TextBox عندما يتم الضغط على مفتاح أو إضافة علامات الدردشة.