1.3 一键 Play 的代价:选型与对标基准
本节用 helio 教学模型比较运行态隔离、实时编辑和场景规模对拓扑的影响。
本章与站内既有内容的分工
本节是对 1.2 矩阵的消费端:1.2 定义代价,本节定义判据;站内没有现成的编辑器拓扑选型模型。
| 主题 | 站内既有内容 | 本节做什么 |
|---|---|---|
| 拓扑与代价 | 1.2 进程模型:单进程双实例到多进程 PIE | 把「哪个拓扑」变成「需求 → 拓扑」的决策函数 |
| 隔离规则 | 1.1 编辑态与运行态:共享与隔离 | 硬隔离需求来自 1.1 的防线在跨进程时更彻底 |
| 引擎内核 | helio 引擎内核 | 运行态是 helio 内核的进程外化身 |
决策函数:四条判据,按优先级排
recommendTopology 只有四个分支,优先级从高到低:
- 需要硬隔离 → 独立进程。崩溃与副作用被进程边界关住,这是任何「怕运行态弄脏编辑器」的场景的第一选择(对标 Unreal PIE、Cocos 预览)。
- 需要真多进程多人 → 独立进程。多人 PIE 需要一个能独立运行的运行时进程,编辑器只是调试前端(对标 Unreal 多进程 PIE)。
- 需要 live edit → 再看规模:世界大 → Worker(隔离大世界内存,同时保留低延迟热改);场景小 → 单进程双实例(延迟最低,热改最顺)。
- 都没有 → 单进程双实例,最简单。
注意第三条的「再看规模」:live edit 本身并不要求拓扑,真正区分 Worker 和 in-process 的是场景内存规模。一个小关卡在编辑器堆里跑没有任何问题,一个大世界的物理与对象流式会压垮编辑器进程——这时才需要 Worker 来当「隔壁房间」。
对标四款编辑器的 Play 模式
| 引擎 | 文档可验证的行为 |
|---|---|
| Unity 6 | 进入 Play Mode 时可分别控制域重载和场景重载。 EnterPlayModeOptions |
| Unreal Engine 5 | PIE 多人设置提供 Run Under One Process,可将多个窗口置于一个 UE 实例。 PIE 设置 |
| Godot 4 | 游戏在独立进程运行,包括游戏画面嵌入主编辑器窗口时。 Game embedding |
| Cocos Creator 3.8 | 支持浏览器、编辑器内预览和模拟器三种入口,浏览器模式打开默认桌面浏览器。 项目预览 |
窗口位置、进程边界和实时编辑是三个独立维度。跨进程实时编辑可通过消息传递实现,需要处理版本、顺序和断连。helio 示例使用可替换的后端来验证接口边界。
下面 src/topology-choice.ts 层级 L1,完整可编译,命令 node --test test/01-3-topology-choice.test.ts:
// file: src/topology-choice.ts
// tier: L1
// verify: node --test test/01-3-topology-choice.test.ts
//
// 对标解析(第一章):Unity、Unreal、Godot、Cocos Creator 的编辑器拓扑,
// 加上一份把「需求」映射到「推荐拓扑」的决策函数,用于对照各引擎的选择
// 与取舍。真正的引擎内部实现远比本节模型复杂,这里只提取可复用的判据。
import type { Topology } from './process-models.ts';
export interface Requirements {
/** 是否需要硬隔离(运行态崩溃不带走编辑器、防恶意脚本污染)。 */
needsHardIsolation: boolean;
/** 是否需要编辑态改动实时流入运行态(live edit / 热调试)。 */
needsLiveEdit: boolean;
/** 是否需要真多进程多人游玩(PIE 联机)。 */
needsTrueMultiplayer: boolean;
/** 规模:single(单机关卡)/ small(小场景)/ large(大世界)。 */
scale: 'single' | 'small' | 'large';
}
export interface Recommendation {
topology: Topology;
reason: string;
/** 对标引擎实例。 */
example: string;
}
export function recommendTopology(req: Requirements): Recommendation {
if (req.needsHardIsolation) {
return {
topology: 'separate-process',
reason: '要求硬隔离,运行态必须与编辑器进程边界分开,崩溃与副作用都被关在独立进程里。',
example: 'Unreal Editor PIE(独立进程运行世界)、Cocos Creator 预览(独立浏览器进程)',
};
}
if (req.needsTrueMultiplayer) {
return {
topology: 'separate-process',
reason: '真多进程多人游玩需要一个可独立运行的运行时进程,编辑器只是它的调试前端。',
example: 'Unreal 的多进程 PIE、独立专用服务器 + 编辑器客户端',
};
}
if (req.needsLiveEdit) {
return {
topology: req.scale === 'large' ? 'worker' : 'in-process',
reason: req.scale === 'large'
? '需要实时改场景但世界很大,用 Worker 隔离大世界内存,同时保留低延迟 live edit。'
: '实时改场景、同步改数据,单进程双实例延迟最低,live edit 最顺。',
example: req.scale === 'large' ? 'helio Worker 教学模型' : 'Unity Edit/Play(同进程双实例)',
};
}
return {
topology: 'in-process',
reason: '无硬隔离需求、无真多进程需求,单进程双实例最简单,延迟最低。',
example: 'helio 单进程教学模型',
};
}
/** 各引擎的编辑器拓扑事实表(用于正文对标小节,取值有据)。 */
export const ENGINE_TOPOLOGIES = [
{ engine: 'Unity', mode: 'configurable-reload' },
{ engine: 'Unreal', mode: 'configurable-pie-processes' },
{ engine: 'Godot', mode: 'embedded-separate-process' },
{ engine: 'Cocos Creator', mode: 'browser-editor-simulator' },
] as const;
这组标签用于教学示例,产品行为以上方官方文档为准。
可运行产物:6 条用例的决策验证
下面是 test/01-3-topology-choice.test.ts 层级 L1,命令 node --test test/01-3-topology-choice.test.ts:
// file: test/01-3-topology-choice.test.ts
// tier: L1
// verify: node --test test/01-3-topology-choice.test.ts
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { ENGINE_TOPOLOGIES, recommendTopology } from '../src/topology-choice.ts';
test('需要硬隔离 → 独立进程(对标 UE PIE / Cocos 预览)', () => {
const r = recommendTopology({ needsHardIsolation: true, needsLiveEdit: false, needsTrueMultiplayer: false, scale: 'single' });
assert.equal(r.topology, 'separate-process');
assert.match(r.example, /Unreal|Cocos/);
});
test('需要真多进程多人游玩 → 独立进程(对标 UE 多客户端 PIE)', () => {
const r = recommendTopology({ needsHardIsolation: false, needsLiveEdit: true, needsTrueMultiplayer: true, scale: 'small' });
assert.equal(r.topology, 'separate-process');
});
test('需要 live edit 且场景小 → 单进程双实例(对标 Unity Edit/Play)', () => {
const r = recommendTopology({ needsHardIsolation: false, needsLiveEdit: true, needsTrueMultiplayer: false, scale: 'small' });
assert.equal(r.topology, 'in-process');
assert.match(r.example, /Unity/);
});
test('需要 live edit 且世界大 → Worker(helio 教学模型)', () => {
const r = recommendTopology({ needsHardIsolation: false, needsLiveEdit: true, needsTrueMultiplayer: false, scale: 'large' });
assert.equal(r.topology, 'worker');
});
test('无特殊需求 → 单进程双实例最简', () => {
const r = recommendTopology({ needsHardIsolation: false, needsLiveEdit: false, needsTrueMultiplayer: false, scale: 'single' });
assert.equal(r.topology, 'in-process');
});
test('对标事实表四引擎齐全,取值方向一致', () => {
const engines = ENGINE_TOPOLOGIES.map((e) => e.engine);
assert.deepEqual(engines, ['Unity', 'Unreal', 'Godot', 'Cocos Creator']);
assert.equal(ENGINE_TOPOLOGIES.find((e) => e.engine === 'Unreal')!.mode, 'configurable-pie-processes');
assert.equal(ENGINE_TOPOLOGIES.find((e) => e.engine === 'Unity')!.mode, 'configurable-reload');
assert.equal(ENGINE_TOPOLOGIES.find((e) => e.engine === 'Cocos Creator')!.mode, 'browser-editor-simulator');
});
本机实测(Node v26.5.0,node --test test/01-3-topology-choice.test.ts):tests 6、pass 6、fail 0,耗时约 61 ms。
单测覆盖决策分支及标签,官方文档用于核实引擎行为。
小结
- 选型是一串需求的交并,判据按优先级排:硬隔离 > 真多人 > live edit(再看规模)> 兜底单进程。
- 跨进程实时编辑需要消息协议;场景内存规模决定 live edit 下选 Worker 还是 in-process。
ENGINE_TOPOLOGIES保存本节的模式标签。- 第一章到此把「编辑 / 运行的分工、隔离、进程、选型」整条链路走通。下一章进入编辑器自身的骨架:插件化内核与模块系统——注册、依赖拓扑、启动序。
阅读导航:上一节:1.2 进程模型:单进程双实例到多进程 PIE · 下一节:2.1 模块注册与依赖拓扑 。
继续阅读
探索更多技术文章
浏览归档,发现更多关于系统设计、工具链和工程实践的内容。