概要
方法
GetAssetFetchStatus(contentId: ContentId):Enum.AssetFetchStatus |
GetAssetFetchStatusChangedSignal(contentId: ContentId):RBXScriptSignal |
Preload(contentId: ContentId):() |
PreloadAsync(contentIdList: {any},callbackFunction: function):() |
RegisterDefaultEncryptionKey(encryptionKey: string):() |
RegisterDefaultSessionKey(sessionKey: string):() |
RegisterEncryptedAsset(assetId: ContentId,encryptionKey: string):() |
RegisterSessionEncryptedAsset(contentId: ContentId,sessionKey: string):() |
UnregisterEncryptedAsset(assetId: ContentId):() |
活動
AssetFetchFailed(assetId: ContentId):RBXScriptSignal |
範例程式碼
內容提供器
local ContentProvider = game:GetService("ContentProvider")
local LOGO_ID = "rbxassetid://658743164"
local PAGE_TURN_ID = "rbxassetid://12222076"
local decal = Instance.new("Decal")
decal.ColorMapContent = Content.fromUri(LOGO_ID)
local sound = Instance.new("Sound")
sound.SoundId = PAGE_TURN_ID
local assets = { decal, sound }
ContentProvider:PreloadAsync(assets)
print("所有資產已加載。")API 參考
屬性
RequestQueueSize
範例程式碼
內容提供者加載條
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
-- 創建一個基本的加載條
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 }) -- 一次一個,會暫停
local progress = i / #assets
bar.Size = UDim2.new(progress, 0, 1, 0)
end
print("加載完成")方法
GetAssetFetchStatus
參數
contentId:ContentId |
範例程式碼
監控資產獲取狀態
local ContentProvider = game:GetService("ContentProvider")
-- 一個示例 資產來加載
local ASSET_ID = "rbxassetid://9120386436"
local exampleAsset = Instance.new("Sound")
exampleAsset.SoundId = ASSET_ID
-- 輸出 資產當前的 AssetFetchStatus
local initialAssetFetchStatus = ContentProvider:GetAssetFetchStatus(ASSET_ID)
print("初始 AssetFetchStatus:", initialAssetFetchStatus)
-- 監聽 更新
local assetFetchStatusChangedSignal = ContentProvider:GetAssetFetchStatusChangedSignal(ASSET_ID)
local function onAssetFetchStatusChanged(newAssetFetchStatus: Enum.AssetFetchStatus)
print(`新的 AssetFetchStatus: {newAssetFetchStatus}`)
end
assetFetchStatusChangedSignal:Connect(onAssetFetchStatusChanged)
-- 觸發資產預加載
local function onAssetRequestComplete(contentId: string, assetFetchStatus: Enum.AssetFetchStatus)
print(`預加載狀態 {contentId}: {assetFetchStatus.Name}`)
end
ContentProvider:PreloadAsync({ exampleAsset }, onAssetRequestComplete)GetAssetFetchStatusChangedSignal
參數
contentId:ContentId |
範例程式碼
監控資產獲取狀態
local ContentProvider = game:GetService("ContentProvider")
-- 一個示例 資產來加載
local ASSET_ID = "rbxassetid://9120386436"
local exampleAsset = Instance.new("Sound")
exampleAsset.SoundId = ASSET_ID
-- 輸出 資產當前的 AssetFetchStatus
local initialAssetFetchStatus = ContentProvider:GetAssetFetchStatus(ASSET_ID)
print("初始 AssetFetchStatus:", initialAssetFetchStatus)
-- 監聽 更新
local assetFetchStatusChangedSignal = ContentProvider:GetAssetFetchStatusChangedSignal(ASSET_ID)
local function onAssetFetchStatusChanged(newAssetFetchStatus: Enum.AssetFetchStatus)
print(`新的 AssetFetchStatus: {newAssetFetchStatus}`)
end
assetFetchStatusChangedSignal:Connect(onAssetFetchStatusChanged)
-- 觸發資產預加載
local function onAssetRequestComplete(contentId: string, assetFetchStatus: Enum.AssetFetchStatus)
print(`預加載狀態 {contentId}: {assetFetchStatus.Name}`)
end
ContentProvider:PreloadAsync({ exampleAsset }, onAssetRequestComplete)Preload
PreloadAsync
返回
()
範例程式碼
預加載資源
local ContentProvider = game:GetService("ContentProvider")
local sound = Instance.new("Sound")
sound.SoundId = "rbxassetid://9120386436"
local decal = Instance.new("Decal")
decal.ColorMapContent = Content.fromUri("rbxassetid://5447528495")
local assets = {
decal,
sound,
}
-- 每當資產解析時將被觸發
local callback = function(assetId, assetFetchStatus)
print("PreloadAsync() 解析的資產 ID:", assetId)
print("PreloadAsync() 最終的 AssetFetchStatus:", assetFetchStatus)
end
-- 預加載內容並計時
local startTime = os.clock()
ContentProvider:PreloadAsync(assets, callback)
local deltaTime = os.clock() - startTime
print(("預加載完成,耗時 %.2f 秒"):format(deltaTime))RegisterDefaultEncryptionKey
RegisterDefaultSessionKey
RegisterEncryptedAsset
RegisterSessionEncryptedAsset
UnregisterDefaultEncryptionKey
ContentProvider:UnregisterDefaultEncryptionKey():()
返回
()
UnregisterEncryptedAsset
ContentProvider:UnregisterEncryptedAsset(assetId:ContentId):()
參數
assetId:ContentId |
返回
()
活動
AssetFetchFailed