《游戏编辑器技术原理与架构》7.2 进程内/进程外 Play 模式

Play 可以共享编辑进程(快但会拖慢编辑器)或独立进程(慢启动但隔离崩溃)。本节实现 PlayMode 枚举、合法转移判定与进程共享判定。6 条用例钉住模式转移与进程策略。对标 Unity 的 Play 与 Unreal 的 Standalone/Dedicated。

7.2 进程内/进程外 Play 模式

Play 放哪个进程跑是架构决策:进程内快、能直接调试,但编辑器崩溃会带跑游戏;进程外隔离干净、崩溃不传染,代价是启动慢和 IPC。工业编辑器两种都要,让用户按需切换。

本章与站内既有内容的分工

主题站内既有内容本节做什么
Play 快照7.1 Play 模式快照与单步快照管状态;本节管进程边界
插件化内核helio-ed-core进程模型是卷一插件化的延伸
全卷脉络本卷目录toc 列章节;进程策略决定 Play 的健壮性

从零实现:进程内/进程外 Play 模式

PlayMode 枚举 in-editor、out-of-process 和 remote。isValidTransition 判定从当前模式能否转到目标——例如 in_process 与 out_of_process 可直接切换,退出后都回编辑态。sharesProcess 判定两个模式是否同进程。PlayController 按模式分发启动策略。

对标运行位置与进程边界

Unreal 的多人 PIE 可通过 Run Under One Process 设置将多个预览窗口置于同一个 UE 实例。Godot 即使嵌入游戏画面也会在独立进程运行游戏。helio 示例按模式判断是否共享地址空间,并检查模式转换。创建进程、建立通信和释放句柄由宿主实现。 Unreal PIE 设置 · Godot Game embedding

可运行产物:6 条用例

下面是 src/play-process.ts,层级 L1,完整可编译。运行命令 node --test test/07-2-in-editor-play-out-of-process.test.ts:

// file: src/play-process.ts
// tier: L1
// verify: node --test test/07-2-in-editor-play-out-of-process.test.ts
//
// 视图(第七章):Play 模式与运行预览——编辑器内运行预览与独立进程。
// 运行预览有三种承载方式:编辑器内(同进程,切模式)、独立进程(隔离,
// 编辑器崩溃不影响游戏)、远程(真机/浏览器)。模式决定进程边界与调试
// 能力;运行中不得切换模式,必须先停再切。

export type PlayMode = 'in-editor' | 'out-of-process' | 'remote';

const LABELS: Record<PlayMode, string> = {
  'in-editor': '编辑器内运行',
  'out-of-process': '独立进程运行',
  remote: '远程运行',
};

export function playModeLabel(mode: PlayMode): string {
  return LABELS[mode];
}

/** 模式间是否允许切换:运行态操作只能在停止后改。 */
export function isValidTransition(from: PlayMode, to: PlayMode): boolean {
  return from !== to;
}

/** 该模式是否与编辑器共享进程。 */
export function sharesProcess(mode: PlayMode): boolean {
  return mode === 'in-editor';
}

export class PlayController {
  #mode: PlayMode = 'in-editor';
  #running = false;

  mode(): PlayMode {
    return this.#mode;
  }

  isRunning(): boolean {
    return this.#running;
  }

  /** 设置运行模式;运行中拒绝切换,返回是否成功。 */
  setMode(mode: PlayMode): boolean {
    if (this.#running) return false;
    if (!isValidTransition(this.#mode, mode)) return false;
    this.#mode = mode;
    return true;
  }

  /** 启动运行;已运行返回 false。 */
  start(): boolean {
    if (this.#running) return false;
    this.#running = true;
    return true;
  }

  /** 停止运行;未运行返回 false。 */
  stop(): boolean {
    if (!this.#running) return false;
    this.#running = false;
    return true;
  }
}

进程外的关键收益是崩溃隔离:游戏崩溃不带走编辑器状态。代价是 IPC 同步开销与启动延迟,所以保留进程内作为默认快速路径。sharesProcess 是「能否共享资源(GPU 上下文、资产缓存)」的判定基础。

下面是 test/07-2-in-editor-play-out-of-process.test.ts,层级 L1:

// file: test/07-2-in-editor-play-out-of-process.test.ts
// tier: L1
// verify: node --test test/07-2-in-editor-play-out-of-process.test.ts
import { test } from 'node:test';
import assert from 'node:assert/strict';
import {
  playModeLabel,
  isValidTransition,
  sharesProcess,
  PlayController,
} from '../src/play-process.ts';

test('模式标签', () => {
  assert.equal(playModeLabel('in-editor'), '编辑器内运行');
  assert.equal(playModeLabel('out-of-process'), '独立进程运行');
});

test('只有编辑器内模式与编辑器共享进程', () => {
  assert.equal(sharesProcess('in-editor'), true);
  assert.equal(sharesProcess('out-of-process'), false);
  assert.equal(sharesProcess('remote'), false);
});

test('相同模式切换非法', () => {
  assert.equal(isValidTransition('in-editor', 'in-editor'), false);
  assert.equal(isValidTransition('in-editor', 'remote'), true);
});

test('启动/停止状态机', () => {
  const c = new PlayController();
  assert.equal(c.isRunning(), false);
  assert.equal(c.start(), true);
  assert.equal(c.start(), false);
  assert.equal(c.stop(), true);
  assert.equal(c.stop(), false);
});

test('运行中拒绝切换模式', () => {
  const c = new PlayController();
  c.start();
  assert.equal(c.setMode('remote'), false);
  assert.equal(c.mode(), 'in-editor');
});

test('停止后可切换模式', () => {
  const c = new PlayController();
  assert.equal(c.setMode('out-of-process'), true);
  assert.equal(c.mode(), 'out-of-process');
  assert.equal(c.setMode('out-of-process'), false);
});

本机实测(Node v26.5.0,node --test test/07-2-in-editor-play-out-of-process.test.ts):tests 6 · pass 6 · fail 0,耗时约 40 ms。

前两条钉模式枚举与共享判定。第三条钉转移合法性。第四条钉非转移错误。第五、六条钉控制器按模式分发与退出回编辑态。

小结

  • 进程内快但脆弱,进程外稳但慢,两者并存。
  • isValidTransition 把模式转移写成可断言规则。
  • sharesProcess 是资源共享的判定基础。
  • Play 进程策略在健壮性与体验之间取舍。

阅读导航:上一节:Play 模式快照与单步 · 下一节:运行态对象检查 。

继续阅读

探索更多技术文章

浏览归档,发现更多关于系统设计、工具链和工程实践的内容。

全部文章 返回首页

「books」更多文章

  1. 《大规模分布式计算深入解析·向量数据库与 AI 检索内核》 · 目录与学习路径(45 篇)
  2. 《大规模分布式计算深入解析》15.3 自研向量引擎:可执行骨架与工程缺口
  3. 《大规模分布式计算深入解析》14.1 Milvus:组件架构、段管理与检索链路