Roblox 在用户连接到体验时显示默认加载屏幕,但您可以使用包含静态或动画内容的自定义加载屏幕来个性化您的体验。
显示自定义加载屏幕
要显示自定义加载屏幕,您可以直接在 中设计一个实例,或者可以在工作区中引用一个 对象。两个选项都使用 ReplicatedFirst , 因为这个服务在复制其他任何东西之前复制实例到客户。这可确保您的加载屏幕是用户进入体验时第一看到的内容。
在本地脚本中设计
要设计并显示自定义加载屏幕:
- 在 复制第一个 中,创建一个 LocalScript。
- 使用以下代码示例来创建和自定义一个 ScreenGui 对象。您可以使用自己的值修改以下代码来创建设计:
local Players = game:GetService("Players")local ReplicatedFirst = game:GetService("ReplicatedFirst")local player = Players.LocalPlayerlocal playerGui = player:WaitForChild("PlayerGui")local screenGui = Instance.new("ScreenGui")screenGui.IgnoreGuiInset = truescreenGui.Parent = playerGui-- 用自拥有的值替换 ScreenGui 值local textLabel = Instance.new("TextLabel")textLabel.Size = UDim2.new(1, 0, 1, 0)textLabel.BackgroundColor3 = Color3.fromRGB(0, 20, 40)textLabel.Font = Enum.Font.GothamMediumtextLabel.TextColor3 = Color3.new(0.8, 0.8, 0.8)textLabel.Text = "Loading"textLabel.TextSize = 28textLabel.Parent = screenGui-- 移除默认加载屏幕ReplicatedFirst:RemoveDefaultLoadingScreen()task.wait(5) -- 强制屏幕在最少数秒内出现if not game:IsLoaded() thengame.Loaded:Wait()endscreenGui:Destroy()
参考屏幕指南
而不是通过创建 ScreenGui 来创建一个 LocalScript ,您也可以直接在工作区中引用现有的 ScreenGui 。确保您的体验包含 ScreenGui 内的 ReplicatedFirst 以及 ScreenGui 包含 UI 元素,如 TextLabels 和 ImageLabels ,然后将其设置为加载屏幕,通过在 LocalScript 中引用它来实现。这种方法可以让你轻松地查看你正在创建的加载屏幕。
为了展示此过程,以下 LocalScript 引用了 ScreenGui 命名为 加载屏幕 的 ReplicatedFirst 内的默认加载屏幕,然后移除了默认加载屏幕,因此用户只能看到自己的自定义加载屏幕:
local Players = game:GetService("Players")local ReplicatedFirst = game:GetService("ReplicatedFirst")local player = Players.LocalPlayerlocal playerGui = player:WaitForChild("PlayerGui")local screenGui = ReplicatedFirst.LoadingScreenscreenGui.IgnoreGuiInset = truescreenGui.Parent = playerGui-- 移除默认加载屏幕ReplicatedFirst:RemoveDefaultLoadingScreen()task.wait(5) -- 强制屏幕在最少数秒内出现if not game:IsLoaded() thengame.Loaded:Wait()endscreenGui:Destroy()
添加动画
除了静态自定义加载屏幕外,您还可以添加动画来增强加载屏幕并显示加载进度。最简单的方法是创建一个 UI 元素,例如 TextLabel 或 ImageLabel ,然后使用 TweenService 来动画它。例如,以下代码示例创建了一个新的 ScreenGui 与子 ImageLabel ,移除了默认加载屏幕,然后 TweenService 持续旋转 ImageLabel 直到体验加载:
local Players = game:GetService("Players")local ReplicatedFirst = game:GetService("ReplicatedFirst")local TweenService = game:GetService("TweenService")local player = Players.LocalPlayerlocal playerGui = player:WaitForChild("PlayerGui")local screenGui = Instance.new("ScreenGui")screenGui.IgnoreGuiInset = truescreenGui.Parent = playerGuilocal textLabel = Instance.new("TextLabel")textLabel.Size = UDim2.new(1, 0, 1, 0)textLabel.BackgroundColor3 = Color3.fromRGB(0, 20, 40)textLabel.Font = Enum.Font.GothamMediumtextLabel.TextColor3 = Color3.new(0.8, 0.8, 0.8)textLabel.Text = "Loading"textLabel.TextSize = 28textLabel.Parent = screenGuilocal loadingRing = Instance.new("ImageLabel")loadingRing.Size = UDim2.new(0, 256, 0, 256)loadingRing.BackgroundTransparency = 1loadingRing.Image = "rbxassetid://4965945816"loadingRing.AnchorPoint = Vector2.new(0.5, 0.5)loadingRing.Position = UDim2.new(0.5, 0, 0.5, 0)loadingRing.Parent = screenGui-- 移除默认加载屏幕ReplicatedFirst:RemoveDefaultLoadingScreen()local tweenInfo = TweenInfo.new(4, Enum.EasingStyle.Linear, Enum.EasingDirection.In, -1)local tween = TweenService:Create(loadingRing, tweenInfo, {Rotation = 360})tween:Play()task.wait(5) -- 强制屏幕在最少数秒内出现if not game:IsLoaded() thengame.Loaded:Wait()endscreenGui:Destroy()