Skip to content

AssetService

AssetService 资产服务,提供预加载资源与自定义资产管理。原预设管理能力已迁移至 PrefabService。

Overview

管理资源预加载与自定义资产流转:一方面维护一张预加载资源清单,可基于资源 URI 将资产加入或移出,并随时回查当前清单内容;另一方面支持将自定义资产导出为本地 JSON 文件,再通过该文件导入还原为新资产,并可按资产类型筛选枚举全部自定义资产。历史遗留的预设管理接口已标记迁出,仅作兼容保留,新逻辑应聚焦在预加载与自定义资产的流转上。

Get by:

lua
-- @runtime client
local service = editor:GetService("AssetService")

Public

AddPreloadAsset

将指定 URI 标识的资源添加至预加载清单,并返回是否添加成功。

参数

参数类型说明
uriString完整资源 URI(custom://xxx / official://mesh/9833)

返回值

类型说明
Bool是否添加成功

调用 AddPreloadAsset

lua
-- @runtime client
local assetService = editor:GetService("AssetService")
local uri = "default"  -- String
local result = assetService:AddPreloadAsset(uri)  -- 返回 Bool
if result then
    print("调用结果为 true")
else
    print("调用结果为 false")
end

RemovePreloadAsset

将指定 URI 标识的资源从预加载清单中移除,并返回是否移除成功。

参数

参数类型说明
uriString完整资源 URI(custom://xxx / official://mesh/9833)

返回值

类型说明
Bool是否移除成功

调用 RemovePreloadAsset

lua
-- @runtime client
local assetService = editor:GetService("AssetService")
local uri = "default"  -- String
local result = assetService:RemovePreloadAsset(uri)  -- 返回 Bool
if result then
    print("调用结果为 true")
else
    print("调用结果为 false")
end

GetPreloadAssetList

获取当前预加载资源清单,返回由资源 URI 组成的数组。

返回值

类型说明
Array当前预加载资源清单(资源 URI 数组)

调用 GetPreloadAssetList

lua
-- @runtime client
local assetService = editor:GetService("AssetService")
local result = assetService:GetPreloadAssetList()  -- 返回 Array
if result ~= nil then
    for index, item in ipairs(result) do
        print(index, item)
    end
end

ExportCustomAsset

将指定自定义资源导出到指定本地路径,并返回是否导出成功。

参数

参数类型说明
assetIdString要导出的自定义资产 ID
exportPathString导出文件路径

返回值

类型说明
Bool是否导出成功

调用 ExportCustomAsset

lua
-- @runtime client
local assetService = editor:GetService("AssetService")
local assetId = ""  -- String
local exportPath = ""  -- String
local result = assetService:ExportCustomAsset(assetId, exportPath)  -- 返回 Bool
if result then
    print("调用结果为 true")
else
    print("调用结果为 false")
end

ImportCustomAsset

从指定 JSON 描述文件导入自定义资源,返回导入成功的资源 ID;失败时返回空串。

参数

参数类型说明
jsonPathString要导入的 JSON 文件路径

返回值

类型说明
String资源ID(失败返回空串)

调用 ImportCustomAsset

lua
-- @runtime client
local assetService = editor:GetService("AssetService")
local jsonPath = ""  -- String
local result = assetService:ImportCustomAsset(jsonPath)  -- 返回 String
if result ~= nil then
    print(result)
    if result ~= "" then
        print("返回了非空字符串")
    end
end

ListCustomAssets

列出自定义资源列表,可按资源类型进行过滤;返回的每项包含资产 ID、类型、名称等信息。

参数

参数类型说明
assetTypeString资产类型过滤(如 picture/audio/texture);nil 时返回全部

返回值

类型说明
Array自定义资产列表,每项包含资产 ID、类型、名称等信息

调用 ListCustomAssets

lua
-- @runtime client
local assetService = editor:GetService("AssetService")
local assetType = ""  -- String
local result = assetService:ListCustomAssets(assetType)  -- 返回 Array
if result ~= nil then
    for index, item in ipairs(result) do
        print(index, item)
    end
end