Roblox 在用户连接到游戏时会显示默认的加载屏幕,但您可以使用包含静态或动画内容的自定义加载屏幕来个性化您的游戏。
实现
要显示自定义加载屏幕,您可以直接在 LocalScript 中设计一个 ScreenGui 实例,或者您可以引用放置层次结构中的 ScreenGui 对象。两种选项都使用 ReplicatedFirst,因为该服务在任何其他内容被复制之前将实例复制到客户端。这确保您的加载屏幕是用户进入游戏时看到的第一件事。
从脚本
要完全通过 LocalScript 设计和显示自定义加载屏幕:
- 在 ReplicatedFirst 中创建一个 LocalScript。
- 粘贴以下代码示例以创建和自定义 ScreenGui 对象。您可以使用自己的值修改以下代码以创建您的设计。
脚本创建的加载屏幕local Players = game:GetService("Players")local ReplicatedFirst = game:GetService("ReplicatedFirst")local player = Players.LocalPlayerlocal playerGui = player:WaitForChild("PlayerGui")local loadingScreen = Instance.new("ScreenGui")loadingScreen.IgnoreGuiInset = trueloadingScreen.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.DenkOnetextLabel.TextColor3 = Color3.new(0.8, 0.8, 0.8)textLabel.Text = "加载中"textLabel.TextSize = 32textLabel.Parent = loadingScreen-- 移除默认加载屏幕ReplicatedFirst:RemoveDefaultLoadingScreen()task.wait(5) -- 强制自定义加载屏幕至少显示一段时间if not game:IsLoaded() thengame.Loaded:Wait()endloadingScreen:Destroy()
从现有 GUI
您还可以通过引用放置层次结构中的现有 ScreenGui 而不是完全通过 LocalScript 创建加载屏幕。确保您的游戏在 ReplicatedFirst 内包含引用的 ScreenGui,并且 ScreenGui 包含 TextLabels 和 ImageLabels 等 UI 元素。
为了演示这个过程,以下 LocalScript 引用 ReplicatedFirst 中名为 LoadingScreen 的 ScreenGui,然后它移除默认的加载屏幕,以便玩家可以看到的唯一加载屏幕是您自己的自定义加载屏幕。
引用现有加载屏幕local Players = game:GetService("Players")local ReplicatedFirst = game:GetService("ReplicatedFirst")local player = Players.LocalPlayerlocal playerGui = player:WaitForChild("PlayerGui")local loadingScreen = ReplicatedFirst:FindFirstChild("LoadingScreen")if loadingScreen thenloadingScreen.IgnoreGuiInset = trueloadingScreen.Parent = playerGui-- 移除默认加载屏幕ReplicatedFirst:RemoveDefaultLoadingScreen()task.wait(5) -- 强制屏幕至少显示几秒钟if not game:IsLoaded() thengame.Loaded:Wait()endloadingScreen:Destroy()end
添加动画
除了静态自定义加载屏幕外,您还可以添加动画来增强加载屏幕并指示加载进度。最简单的方法是创建一个 UI 元素,例如 TextLabel 或 ImageLabel,然后使用 TweenService 对其进行动画处理。例如,以下代码示例创建一个新的 ScreenGui,其子元素为 ImageLabel,移除默认加载屏幕,然后 TweenService 使中心元素淡入并旋转,直到游戏加载完成。
脚本创建的加载屏幕(动画)local Players = game:GetService("Players")local ReplicatedFirst = game:GetService("ReplicatedFirst")local TweenService = game:GetService("TweenService")local player = Players.LocalPlayerlocal playerGui = player:WaitForChild("PlayerGui")local loadingScreen = Instance.new("ScreenGui")loadingScreen.IgnoreGuiInset = trueloadingScreen.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.DenkOnetextLabel.TextColor3 = Color3.new(0.8, 0.8, 0.8)textLabel.Text = "加载中"textLabel.TextSize = 32textLabel.TextTransparency = 1textLabel.Parent = loadingScreenlocal 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.ImageTransparency = 1loadingRing.Parent = loadingScreen-- 移除默认加载屏幕ReplicatedFirst:RemoveDefaultLoadingScreen()-- 启动并开始淡入效果local fadeTweenInfo = TweenInfo.new(1, Enum.EasingStyle.Quad, Enum.EasingDirection.Out, 0, false, 2)local fadeTween1 = TweenService:Create(textLabel, fadeTweenInfo, {TextTransparency = 0})local fadeTween2 = TweenService:Create(loadingRing, fadeTweenInfo, {ImageTransparency = 0})fadeTween1:Play()fadeTween2:Play()-- 启动并开始旋转动画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()endloadingScreen:Destroy()