将内容(资产)加载到游戏的服务。
Roblox 服务器在执行时间行时向客户端传输所有资产:工作区中的对象、网格资产、纹理资产等等无论是否启用传输,网格视觉数据、纹理、贴花和声音等资产都会按需流传,无需进行编解码。
在一些情况下,这种行为是不可取的,因为它可能会导致内容在体验中加载前的延迟。
ContentProvider 允许您使用 ContentProvider:PreloadAsync() 方法预加载资产到体验中。您可能想显示加载屏幕、预加载关键资产,然后才允许玩家进入体验。
预加载最佳实践
- 仅预加载必要资产, 不 整个Workspace。你可能会遇到偶尔的弹出,但它会减少加载时间,一般不会干扰玩家体验。预加载的候选资产包括需要用于加载屏幕、用户界面或起始区域的资产。
- 让玩家跳过加载屏幕,或在一定时间后自动跳过。
代码示例
In this example a Decal and Sound are preloaded into a game. Once they have finished loading the script will print a message to the output.
local ContentProvider = game:GetService("ContentProvider")
local LOGO_ID = "rbxassetid://658743164"
local PAGE_TURN_ID = "rbxassetid://12222076"
local decal = Instance.new("Decal")
decal.Texture = LOGO_ID
local sound = Instance.new("Sound")
sound.SoundId = PAGE_TURN_ID
local assets = { decal, sound }
ContentProvider:PreloadAsync(assets)
print("All assets loaded.")
概要
属性
由 ContentProvider 用于从 Roblox 网站下载资产。
提供需要下载的 ContentProvider 请求队列中的项目数量。
方法
获取提供的当前 Enum.AssetFetchStatus 的 contentId 。
当提供的内容的 Enum.AssetFetchStatus 发生变化时触发的信号。
直到所有与给定 Instances 相关的资产加载完毕。
属性
BaseUrl
由 ContentProvider 用于从 Roblox 网站下载资产。
该 URL 指向一个 Roblox 托管的网站,从其中下载资产并从 AppSettings.xml 文件中拉取,位于版本哈希文件夹中。
使用命令栏中的 ContentProvider:SetBaseUrl() 函数可以覆盖此属性,但不建议这样做,可能会导致资产加载问题。
RequestQueueSize
提供需要下载的 ContentProvider 请求队列中的项目数量。
当资产首次使用或 ContentProvider:PreloadAsync() 被调用时,项目将添加到客户端的请求队列中。
建议开发人员不要使用 RequestQueueSize 来创建加载条。这是因为随着新资产的添加和下载,队列大小可以在一定时间内增加和减少。希望显示加载进度的开发者应该一次加载资产(见下面的示例)。
代码示例
This code sample demonstrates how ContentProvider:PreloadAsync() can be used to create a simple loading bar in a game, by loading the assets one at a time.
local ContentProvider = game:GetService("ContentProvider")
local Players = game:GetService("Players")
local localPlayer = Players.LocalPlayer
local playerGui = localPlayer:WaitForChild("PlayerGui")
local screenGui = Instance.new("ScreenGui")
screenGui.Parent = playerGui
-- create a basic loading bar
local frame = Instance.new("Frame")
frame.Size = UDim2.new(0.5, 0, 0.1, 0)
frame.Position = UDim2.new(0.5, 0, 0.5, 0)
frame.AnchorPoint = Vector2.new(0.5, 0.5)
frame.Parent = screenGui
local bar = Instance.new("Frame")
bar.Size = UDim2.new(0, 0, 1, 0)
bar.Position = UDim2.new(0, 0, 0, 0)
bar.BackgroundColor3 = Color3.new(0, 0, 1)
bar.Parent = frame
local sound = Instance.new("Sound")
sound.SoundId = "rbxassetid://9120386436"
local sound2 = Instance.new("Sound")
sound2.SoundId = "rbxassetid://9120385974"
local assets = {
sound,
sound2,
}
task.wait(3)
for i = 1, #assets do
local asset = assets[i]
ContentProvider:PreloadAsync({ asset }) -- 1 at a time, yields
local progress = i / #assets
bar.Size = UDim2.new(progress, 0, 1, 0)
end
print("loading done")
方法
GetAssetFetchStatus
获取提供的当前 Enum.AssetFetchStatus 的 contentId 。使用 GetAssetFetchStatusChangedSignal() 来监听此值的更改。
参数
用于获取状态的内容的ID。
返回
代码示例
Retrieves the initial Enum.AssetFetchStatus of an asset and listens for future updates.
local ContentProvider = game:GetService("ContentProvider")
-- An example asset to load
local ASSET_ID = "rbxassetid://9120386436"
local exampleAsset = Instance.new("Sound")
exampleAsset.SoundId = ASSET_ID
-- Output the current AssetFetchStatus of the asset
local initialAssetFetchStatus = ContentProvider:GetAssetFetchStatus(ASSET_ID)
print("Initial AssetFetchStatus:", initialAssetFetchStatus)
-- Listen for updates
local assetFetchStatusChangedSignal = ContentProvider:GetAssetFetchStatusChangedSignal(ASSET_ID)
local function onAssetFetchStatusChanged(newAssetFetchStatus: Enum.AssetFetchStatus)
print(`New AssetFetchStatus: {newAssetFetchStatus}`)
end
assetFetchStatusChangedSignal:Connect(onAssetFetchStatusChanged)
-- Trigger the asset to preload
local function onAssetRequestComplete(contentId: string, assetFetchStatus: Enum.AssetFetchStatus)
print(`Preload status {contentId}: {assetFetchStatus.Name}`)
end
ContentProvider:PreloadAsync({ exampleAsset }, onAssetRequestComplete)
GetAssetFetchStatusChangedSignal
当提供的内容的 Enum.AssetFetchStatus 发生变化时触发的信号。使用一个类型为 Enum.AssetFetchStatus 的回调连接到这个信号。这对那些可能自动更新的资产特别有用,例如用户更换衣服时的缩略图。
参数
返回
代码示例
Retrieves the initial Enum.AssetFetchStatus of an asset and listens for future updates.
local ContentProvider = game:GetService("ContentProvider")
-- An example asset to load
local ASSET_ID = "rbxassetid://9120386436"
local exampleAsset = Instance.new("Sound")
exampleAsset.SoundId = ASSET_ID
-- Output the current AssetFetchStatus of the asset
local initialAssetFetchStatus = ContentProvider:GetAssetFetchStatus(ASSET_ID)
print("Initial AssetFetchStatus:", initialAssetFetchStatus)
-- Listen for updates
local assetFetchStatusChangedSignal = ContentProvider:GetAssetFetchStatusChangedSignal(ASSET_ID)
local function onAssetFetchStatusChanged(newAssetFetchStatus: Enum.AssetFetchStatus)
print(`New AssetFetchStatus: {newAssetFetchStatus}`)
end
assetFetchStatusChangedSignal:Connect(onAssetFetchStatusChanged)
-- Trigger the asset to preload
local function onAssetRequestComplete(contentId: string, assetFetchStatus: Enum.AssetFetchStatus)
print(`Preload status {contentId}: {assetFetchStatus.Name}`)
end
ContentProvider:PreloadAsync({ exampleAsset }, onAssetRequestComplete)
UnregisterDefaultEncryptionKey
返回
UnregisterEncryptedAsset
参数
返回
PreloadAsync
直到所有与给定 Instances 相关的资产加载完毕。这可用于暂停脚本并在确定内容已加载到体验中之前不使用内容的情况下。
调用时,引擎识别列表中每个项目的内容链接。对于任何具有定义内容链接的属性的 Instances ,例如 Decal 或 Sound ,引擎尝试从 Roblox 加载这些资产。对于每个请求的素材,调用函数运行,表示素材的最后 Enum.AssetFetchStatus 。
如果任何资产无法加载,输出中将出现错误消息。方法本身不会出错,直到它处理了每个请求的实例后继续执行。
限制
SurfaceAppearance 和 MaterialVariant 不支持 PreloadAsync() ,因为这些对象依赖处理的纹理包资产而不是直接加载单个纹理。在 SurfaceAppearance 实例上调用它不会做任何事情,但相关的纹理仍然会在执行时间行时流传。
参数
要加载的一组实例。
每当资产请求完成时调用的函数。返回 content 字符串和素材的最后 Enum.AssetFetchStatus 。
返回
代码示例
In this code sample, a sound and a texture are preloaded using Sound and Decal instances.
local ContentProvider = game:GetService("ContentProvider")
local sound = Instance.new("Sound")
sound.SoundId = "rbxassetid://9120386436"
local decal = Instance.new("Decal")
decal.Texture = "rbxassetid://5447528495"
local assets = {
decal,
sound,
}
-- This will be hit as each asset resolves
local callback = function(assetId, assetFetchStatus)
print("PreloadAsync() resolved asset ID:", assetId)
print("PreloadAsync() final AssetFetchStatus:", assetFetchStatus)
end
-- Preload the content and time it
local startTime = os.clock()
ContentProvider:PreloadAsync(assets, callback)
local deltaTime = os.clock() - startTime
print(("Preloading complete, took %.2f seconds"):format(deltaTime))