主题
World
World 是编辑器场景单位管理服务,通过 editor:GetService("World") 获取;可查找、遍历、创建场景单位与资产,返回的单位可继续使用 Unit 的公开成员。
概览
World 是编辑器场景单位管理服务,通过 editor:GetService("World") 获取;可查找、遍历、创建场景单位与资产,返回的单位可继续使用 Unit 的公开成员。
Get by:
lua
-- @runtime editor
local service = editor:GetService("World")函数
CaptureScreenshot
视口截图:按可选参数 options 抓取当前视口画面并保存为 PNG 文件,返回该文件的绝对路径。当相机不可用或目标画布不存在时返回 nil。
参数
| 参数 | 类型 | 说明 |
|---|---|---|
options | Table | 相机参数缺省时沿用当前视口视角 |
返回值
| 类型 | 说明 |
|---|---|
String | 保存的 PNG 文件绝对路径;相机不可用或目标画布不存在时返回 nil |
视口截图示例
lua
-- @runtime editor
local world = editor:GetService("World")
if world.CurrentCamera == nil then
print("当前没有可用相机,无法截图")
return
end
local imagePath = world:CaptureScreenshot()
if imagePath == nil then
print("截图失败:相机不可用或目标画布不存在")
else
print("截图已保存到:", imagePath)
endFindFirstChild
World 服务的 FindFirstChild 用于按名称查找单位,接收一个字符串类型的名称参数,返回与之对应的 Unit 实例;当没有查找到匹配单位时,返回 nil,因此返回值是可空类型。
参数
| 参数 | 类型 | 说明 |
|---|---|---|
name | String | 要查找的单位名称 |
返回值
| 类型 | 说明 |
|---|---|
Unit | 找到的单位;未找到时返回 nil |
查找本示例创建的临时单位
lua
-- @runtime editor
local world = editor:GetService("World")
local created = world:CreateUnit("WorldUnit", { Name = "DocumentationFindExample" })
if created == nil then return end
local ok, err = pcall(function()
local found = world:FindFirstChild("DocumentationFindExample")
print("按名称查找结果:", found)
end)
created:Destroy()
assert(ok, err)GetDescendants
获取场景中的所有单位,并以数组形式返回,数组元素为 Unit 类型。该函数无入参,调用后一次性返回当前场景内的全部单位集合。
返回值
| 类型 | 说明 |
|---|---|
Array | 场景中的所有单位数组 |
遍历场景并清理本示例的临时单位
lua
-- @runtime editor
local world = editor:GetService("World")
local created = world:CreateUnit("WorldUnit", { Name = "DocumentationDescendantsExample" })
if created == nil then return end
local ok, err = pcall(function()
local units = world:GetDescendants()
print("场景单位数量:", #units)
for index, unit in ipairs(units) do print(index, unit:GetName()) end
end)
created:Destroy()
assert(ok, err)GetChildren
获取世界中的直接子单位,返回由 Unit 组成的数组。该函数不接收任何参数;当世界中不存在顶层单位时,返回空表,而不会返回 nil。
返回值
| 类型 | 说明 |
|---|---|
Array | 世界直接子单位数组;无顶层单位时返回空表 |
获取世界直接子单位示例
lua
-- @runtime editor
local world = editor:GetService("World")
local children = world:GetChildren()
print("直接子单位数量:", #children)
for index, child in ipairs(children) do
print(index, child)
endCreateAsset
在世界中按指定的内容 ID 与三维坐标创建一个资产单位,并返回本次创建得到的单位数组。contentId 为必填的字符串内容标识,pos 为必填的 Vector3 三维坐标,options 为可选的表参数;返回值为 Unit 类型的数组,对应创建出的单位。
参数
| 参数 | 类型 | 说明 |
|---|---|---|
contentId | String | 预设资产 ID,可通过 PrefabService:CreatePrefabFromUnit 创建后获取。 |
pos | Vector3 | 创建位置(世界坐标) |
options | Table | 创建选项:rot(旋转)、scale(缩放),均为可选 |
返回值
| 类型 | 说明 |
|---|---|
Array | 创建的单位数组 |
按真实预设 ID 创建并清理资产
lua
-- @runtime editor
local world = editor:GetService("World")
local function inspectAsset(assetId)
if assetId == nil or assetId == "" then return end
local units = world:CreateAsset(assetId, Vector3.New(0, 0, 0))
for _, unit in ipairs(units or {}) do
print("已创建:", unit)
unit:Destroy()
end
end
-- 把项目中的真实预设资源 ID 传给 inspectAssetCreateUnit
World 服务的 CreateUnit 方法用于按单位类型创建一个新单位:第一个参数传入单位类型标识(String),第二个参数 values 为可选的 Table,用于在创建时一并提供该单位的初始数据;返回值为新创建的单位对象(主单位),创建失败时返回 nil。
参数
| 参数 | 类型 | 说明 |
|---|---|---|
unitType | String | 要创建的单位类型名称(如 WorldUnit、ModelUnit) |
values | Table | 创建参数:Name(名称)、Position(位置)、Rotation(旋转)、Scale(缩放)、AssetId(直接指定预设资产 ID),均为可选 |
返回值
| 类型 | 说明 |
|---|---|
Unit | 新创建的单位对象(主单位);创建失败时返回 nil |
创建并清理临时世界单位
lua
-- @runtime editor
local world = editor:GetService('World')
local unit = world:CreateUnit('WorldUnit', { Name = 'TemporaryUnit' })
if unit ~= nil then
print('已创建:', unit.Name)
unit:Destroy()
endGetUnitsByUnitType
按单位类型在世界中筛选并返回单位列表。调用时传入表示单位类型的字符串,并可通过可选参数控制匹配是否为精确匹配;返回值为单位数组,若没有任何单位符合条件则返回空数组。
参数
| 参数 | 类型 | 说明 |
|---|---|---|
unitType | String | 要查询的单位类型名称 |
exact | Bool | 是否精确匹配;false 时同时返回继承子类型 |
返回值
| 类型 | 说明 |
|---|---|
Array | 匹配到的单位数组,无匹配时返回空数组 |
按类型查询并清理本示例的两个单位
lua
-- @runtime editor
local world = editor:GetService("World")
local a = world:CreateUnit("WorldUnit", { Name = "DocumentationTypeExampleA" })
local b = world:CreateUnit("WorldUnit", { Name = "DocumentationTypeExampleB" })
if a == nil or b == nil then
if a ~= nil then a:Destroy() end
if b ~= nil then b:Destroy() end
return
end
local ok, err = pcall(function()
local units = world:GetUnitsByUnitType("WorldUnit", true)
print("WorldUnit 数量:", #units)
for index, unit in ipairs(units) do print(index, unit:GetName()) end
end)
b:Destroy()
a:Destroy()
assert(ok, err)QueryUnitSummaries
按关键字、名称、单位类型与父级等条件筛选单位,并以分页方式返回单位摘要列表以及按类型汇总的统计结果。所有筛选形参均为可选,省略某个形参即表示该维度不参与过滤;limit 与 offset 共同决定本次取回的分页区间。查询失败时抛出错误,错误消息中可能包含 EDITOR_UNIT_INVALID_ARGUMENT、EDITOR_UNIT_QUERY_FAILED 或 EDITOR_UNIT_DUPLICATE_ID。
参数
| 参数 | 类型 | 说明 |
|---|---|---|
query | String | 大小写不敏感,匹配 Name/UnitType/Path;非 String 抛出错误(消息含 EDITOR_UNIT_INVALID_ARGUMENT) |
name | String | 名称子串,大小写不敏感,空串不过滤;非 String 抛出错误(消息含 EDITOR_UNIT_INVALID_ARGUMENT) |
unitType | String | 单位类型精确匹配(单位类型名),大小写不敏感,空串不过滤;非 String 抛出错误(消息含 EDITOR_UNIT_INVALID_ARGUMENT) |
parent | Unit | 仅返回该单位的直接子单位;nil 不过滤,传入 World 服务本身返回场景顶层单位;非 Unit 对象抛出错误(消息含 EDITOR_UNIT_INVALID_ARGUMENT) |
limit | Int | 1-200,默认 50;非整数或越界抛出错误(消息含 EDITOR_UNIT_INVALID_ARGUMENT) |
offset | Int | 零基偏移量,需 >=0;默认 0,负数抛出错误(消息含 EDITOR_UNIT_INVALID_ARGUMENT) |
返回值
| 类型 | 说明 |
|---|---|
UnitSummaryQueryResult | 分页单位摘要与类型统计;失败抛出错误(消息含 EDITOR_UNIT_INVALID_ARGUMENT / EDITOR_UNIT_QUERY_FAILED / EDITOR_UNIT_DUPLICATE_ID)。 |
分页查询单位摘要并验证结果
lua
-- @runtime editor
local world = editor:GetService("World")
local editableProperties = world:GetEditableProperties()
local children = world:GetChildren()
print("可编辑属性数量:", #editableProperties)
print("当前子单位数量:", #children)
local summaryResult = world:QueryUnitSummaries()
assert(summaryResult ~= nil, "单位摘要查询失败")
print("单位摘要查询结果:", summaryResult)GetEditableProperties
获取 World 的可编辑属性名列表,返回一个字符串数组,数组中的每个元素对应一个可编辑属性的名称。该函数不接收参数。
注意: 当前官方 Editor 运行包(editor-cli 0.20.17 / sdk 9.0.315)调用该接口时报
attempt to index a nil value (global 'UnitAttrMeta'),属于运行包实现缺口(Meta 合同已外放);本轮不提供可运行示例,待上游修复运行包后补充。参数与返回值以 Meta 合同为准。
返回值
| 类型 | 说明 |
|---|---|
Array | 可编辑属性名数组 |
SetReconnectMode
设置地图的掉线重连模式。调用时传入一个整型的模式值,接口会把该模式写入当前地图配置,并返回一个布尔值表示写入结果:返回 true 表示已写入,返回 false 表示传入的模式不合法。
参数
| 参数 | 类型 | 说明 |
|---|---|---|
mode | Int | 0=回到出生点重连;1=原地重连(需作者自行响应 OnRelay) |
返回值
| 类型 | 说明 |
|---|---|
Bool | true 表示已写入,需保存地图后生效;mode 非法时返回 false |
读取并写回掉线重连模式
lua
-- @runtime editor
local world = editor:GetService("World")
assert(world, "无法获取 World 服务")
-- 读取当前掉线重连模式,作为写入的合法取值来源
local currentMode = world:GetReconnectMode()
print("当前掉线重连模式:", currentMode)
-- 将同一模式值写回,确认写入接口可用
local ok = world:SetReconnectMode(currentMode)
print("SetReconnectMode 返回:", ok)
print("提示:写入结果需保存地图后生效")
-- 再次查询确认当前生效的重连模式
local modeAfter = world:GetReconnectMode()
print("写入后的掉线重连模式:", modeAfter)GetReconnectMode
获取当前地图的掉线重连模式,以整数形式返回该地图对玩家断线后重新进入时的定位规则。
返回值
| 类型 | 说明 |
|---|---|
Int | 0=回到出生点重连;1=原地重连 |
获取地图掉线重连模式示例
lua
-- @runtime editor
local world = editor:GetService("World")
local mode = world:GetReconnectMode()
local modeText = "回到出生点重连"
if mode == 1 then
modeText = "原地重连"
end
print("当前掉线重连模式:", mode, modeText)属性
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
CurrentCamera | EditorCamera | - | 编辑态视口相机对象,只读。相机不可用(未打开地图、camera_ctrl 缺失或未激活)时返回 nil;设置相机属性前建议先把 CameraType 写成 Enums.CameraType.Scriptable。注意它与 CameraService:GetCurrentCamera() 的地图默认相机不是同一对象;仅状态同步(SE)编辑器可用,FS 端返回 nil。 |
