主题
PublishService
资产发布服务,提供本地文件与 mesh 的上传、批量发布、进度查询与自定义资产管理。
Overview
PublishService 负责把本地文件路径对应的图片、FBX 等资源,以及以网格名标识的网格模型上传到平台资产库,并返回包含 AssetId 与 Uri 的发布结果供后续引用。服务同时提供异步回调版本(PublishAsync、PublishMesh)与同步阻塞版本(PublishSync、PublishMeshSync)两种入口,还能通过条目数组一次发起混合类型的批量发布;发布过程中可查询进度,失败后可读取最近一次错误信息用于定位问题。
Get by:
lua
-- @runtime client
local service = editor:GetService("PublishService")Public
PublishAsync
以异步方式上传指定路径的资产文件。可以传入可选的上传信息,并通过可选的回调函数在异步上传结束后收到结果;该方法本身没有返回值。
参数
| 参数 | 类型 | 说明 |
|---|---|---|
filePath | String | 要上传的本地文件路径 |
info | Table | 上传信息:Name(资源名称,可选)、Description(描述,可选)、Tags(标签数组,可选) |
callback | Function | 上传完成回调:callback(success, result),result 包含资源 ID、URI 等信息 |
调用 PublishAsync
lua
-- @runtime client
local publishService = editor:GetService("PublishService")
local filePath = "" -- String
local info = {} -- Table
local callback = function() end -- Function
publishService:PublishAsync(filePath, info, callback)
print("调用完成", publishService)PublishSync
以同步方式上传指定路径的资产文件,方法会等待上传完成后直接返回结果表;成功时返回资源标识与资源 URI,失败时返回错误码和错误信息。
参数
| 参数 | 类型 | 说明 |
|---|---|---|
filePath | String | 要上传的本地文件路径 |
info | Table | 上传信息:Name(资源名称,可选)、Description(描述,可选)、Tags(标签数组,可选) |
返回值
| 类型 | 说明 |
|---|---|
Table | 上传结果:成功返回资源 ID 与 URI;失败返回错误码与错误信息 |
调用 PublishSync
lua
-- @runtime client
local publishService = editor:GetService("PublishService")
local filePath = "" -- String
local info = {} -- Table
local result = publishService:PublishSync(filePath, info) -- 返回 Table
if result ~= nil then
for index, item in ipairs(result) do
print(index, item)
end
endGetPublishProgress
查询当前上传任务的进度信息,StepName 表示当前正在执行的步骤。
返回值
| 类型 | 说明 |
|---|---|
Table | 进度信息:{ Progress = number, StepName = string, IsPublishing = boolean } |
调用 GetPublishProgress
lua
-- @runtime client
local publishService = editor:GetService("PublishService")
local result = publishService:GetPublishProgress() -- 返回 Table
if result ~= nil then
for index, item in ipairs(result) do
print(index, item)
end
endGetLastPublishError
获取最近一次资产上传失败时的原因;若上一次上传没有出现失败,则返回空字符串。
返回值
| 类型 | 说明 |
|---|---|
String | 失败原因,无失败时返回空字符串 |
调用 GetLastPublishError
lua
-- @runtime client
local publishService = editor:GetService("PublishService")
local result = publishService:GetLastPublishError() -- 返回 String
if result ~= nil then
print(result)
if result ~= "" then
print("返回了非空字符串")
end
endPublishMesh
以异步方式上传指定的网格资源。传入网格名称 meshName 和上传信息 info,并可通过可选回调在完成时获得上传结果;该方法执行后立即返回,不等待上传结束。
参数
| 参数 | 类型 | 说明 |
|---|---|---|
meshName | String | 要上传的网格名称 |
info | Table | 网格信息:Name(名称,可选)、Description(描述,可选)、Tags(标签数组,可选) |
callback | Function | 上传完成回调:callback(success, result) |
调用 PublishMesh
lua
-- @runtime client
local publishService = editor:GetService("PublishService")
local meshName = "default" -- String
local info = {} -- Table
local callback = function() end -- Function
publishService:PublishMesh(meshName, info, callback)
print("调用完成", publishService)PublishMeshSync
以同步方式上传指定的网格资源。调用时需要提供网格名称 meshName,上传信息表可选;方法会等待上传结束后直接返回结果表,成功时返回资源标识与资源 URI,失败时返回错误码和错误信息。
参数
| 参数 | 类型 | 说明 |
|---|---|---|
meshName | String | 要上传的网格名称 |
info | Table | 网格信息:Name(名称,可选,默认使用网格名)、Description(描述,可选)、Tags(标签数组,可选) |
返回值
| 类型 | 说明 |
|---|---|
Table | 上传结果:成功返回资源 ID 与 URI;失败返回错误码与错误信息 |
调用 PublishMeshSync
lua
-- @runtime client
local publishService = editor:GetService("PublishService")
local meshName = "default" -- String
local info = {} -- Table
local result = publishService:PublishMeshSync(meshName, info) -- 返回 Table
if result ~= nil then
for index, item in ipairs(result) do
print(index, item)
end
endPublishAssetsBatch
批量上传多个资产。需要传入待上传资产的列表 items,方法会返回一个与输入顺序对应的结果数组,数组中的每一项包含对应资产是否上传成功以及成功后的详情或失败信息。
参数
| 参数 | 类型 | 说明 |
|---|---|---|
items | Array | 上传项数组,每项包含 Type(资源类型)与本地路径或网格名,以及可选 Info |
返回值
| 类型 | 说明 |
|---|---|
Array | 与输入同序的结果数组,每项包含是否成功与结果详情 |
调用 PublishAssetsBatch
lua
-- @runtime client
local publishService = editor:GetService("PublishService")
local items = {} -- Array
local result = publishService:PublishAssetsBatch(items) -- 返回 Array
if result ~= nil then
for index, item in ipairs(result) do
print(index, item)
end
end