Roblox在使用者連接到體驗時顯示預設的載入畫面,但您可以使用自訂載入畫面來個性化您的體驗,該畫面可以包含靜態或動畫內容。
顯示自訂載入畫面
要顯示自訂載入畫面,您可以直接在 LocalScript 中設計 ScreenGui 實例,或者可以引用您工作區中的 ScreenGui 物件。兩種選擇都利用 ReplicatedFirst,因為這個服務會在任何其他物件被複製之前先將實例複製到客戶端。這樣可以確保載入畫面是使用者進入您的體驗時看到的第一樣東西。
在LocalScripts中設計
要設計和顯示自訂載入畫面:
- 在 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 = "載入中"textLabel.TextSize = 28textLabel.Parent = screenGui-- 移除預設載入畫面ReplicatedFirst:RemoveDefaultLoadingScreen()task.wait(5) -- 強制螢幕至少顯示幾秒鐘if not game:IsLoaded() thengame.Loaded:Wait()endscreenGui:Destroy()
引用ScreenGuis
您也可以直接在工作區中引用現有的 ScreenGui,而不是通過 LocalScript 創建它。請確保您的體驗中包含 ReplicatedFirst 中的 ScreenGui,並且該 ScreenGui 包含 UI 元素,如 TextLabels 和 ImageLabels,然後通過在 LocalScript 中引用它來將其設置為載入畫面。這種方法可以讓您在創建它時輕鬆查看您的載入畫面。
為了演示此過程,以下的 LocalScript 引用 ReplicatedFirst 中名為 LoadingScreen 的 ScreenGui,然後移除預設的載入畫面,以便使用者只能看到您自己的自訂載入畫面:
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 = "載入中"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()