ContentProvider

显示已弃用

*此内容使用人工智能(Beta)翻译,可能包含错误。若要查看英文页面,请点按 此处

无法创建
服务
未复制

将内容(资产)加载到游戏的服务。

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.

ContentProvider

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.")

概要

属性

方法

属性

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.

ContentProvider Loading Bar

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.AssetFetchStatuscontentId 。使用 GetAssetFetchStatusChangedSignal() 来监听此值的更改。

参数

contentId: ContentId

用于获取状态的内容的ID。

默认值:""

返回

代码示例

Retrieves the initial Enum.AssetFetchStatus of an asset and listens for future updates.

Monitoring AssetFetchStatus

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 的回调连接到这个信号。这对那些可能自动更新的资产特别有用,例如用户更换衣服时的缩略图。

参数

contentId: ContentId
默认值:""

返回

代码示例

Retrieves the initial Enum.AssetFetchStatus of an asset and listens for future updates.

Monitoring AssetFetchStatus

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)

ListEncryptedAssets


返回

RegisterDefaultEncryptionKey

()

参数

encryptionKey: string
默认值:""

返回

()

RegisterDefaultSessionKey

()

参数

sessionKey: string
默认值:""

返回

()

RegisterEncryptedAsset

()

参数

assetId: ContentId
默认值:""
encryptionKey: string
默认值:""

返回

()

RegisterSessionEncryptedAsset

()

参数

contentId: ContentId
默认值:""
sessionKey: string
默认值:""

返回

()

UnregisterDefaultEncryptionKey

()

返回

()

UnregisterEncryptedAsset

()

参数

assetId: ContentId
默认值:""

返回

()

PreloadAsync

()
暂停

直到所有与给定 Instances 相关的资产加载完毕。这可用于暂停脚本并在确定内容已加载到体验中之前不使用内容的情况下。

调用时,引擎识别列表中每个项目的内容链接。对于任何具有定义内容链接的属性的 Instances ,例如 DecalSound ,引擎尝试从 Roblox 加载这些资产。对于每个请求的素材,调用函数运行,表示素材的最后 Enum.AssetFetchStatus

如果任何资产无法加载,输出中将出现错误消息。方法本身不会出错,直到它处理了每个请求的实例后继续执行。

限制

SurfaceAppearanceMaterialVariant 不支持 PreloadAsync() ,因为这些对象依赖处理的纹理包资产而不是直接加载单个纹理。在 SurfaceAppearance 实例上调用它不会做任何事情,但相关的纹理仍然会在执行时间行时流传。

参数

contentIdList: Array

要加载的一组实例。

默认值:""
callbackFunction: function

每当资产请求完成时调用的函数。返回 content 字符串和素材的最后 Enum.AssetFetchStatus

默认值:"nil"

返回

()

代码示例

In this code sample, a sound and a texture are preloaded using Sound and Decal instances.

Preloading Assets

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))

活动

AssetFetchFailed

参数

assetId: ContentId