《游戏编辑器技术原理与架构》9.2 真机预览与远程控制

一个预览会话可以挂多台设备。控制指令只有三种:播放、暂停、单步;单步只在暂停态有效——运行中单步没有语义,回执里直接拒绝。每条回执带递增序号,远端据此对齐请求与响应。本节从零实现并真跑 6 条用例,对标 Unity / Unreal / Godot / Cocos Creator 的真机预览控制。

9.2 真机预览与远程控制

一个预览会话可以挂多台设备。控制指令只有三种:播放、暂停、单步;单步只在暂停态有效——运行中单步没有语义,回执里直接拒绝。每条回执带递增序号,远端据此对齐请求与响应。

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

主题站内既有内容本节做什么
Play 模式《视图与子编辑器》 第 7 章卷三做编辑器内快照与单步;本节把控制搬到真机
远程协议本卷 9.19.1 传输消息;本节定义预览会话的指令语义
全卷脉络本卷目录本节承接远程调试章

从零实现:真机预览与远程控制

会话维护设备集合、预览状态与回执序号。指令先校验设备存在,再递增序号;play 与 pause 无条件切换状态,step 只在 paused 时接受,否则返回 accepted=false 但状态不变。连接与断开返回布尔值,便于界面做幂等处理。

协议与宿主的职责

Cocos Creator 3.8 的浏览器预览可通过二维码在同一网络中的移动设备打开。helio 的 PreviewSession 则用状态和序号演示控制请求与回执。实际设备连接、传输和运行时响应需要宿主实现。 Cocos Creator 项目预览

可运行产物:6 条用例

下面是 src/preview-session.ts,层级 L1,完整可编译。运行命令 node --test test/09-2-device-preview-remote-control.test.ts:

// file: src/preview-session.ts
// tier: L1
// verify: node --test test/09-2-device-preview-remote-control.test.ts
//
// 卷四(第九章):真机预览与远程控制。
// 一个预览会话可以挂多台设备。控制指令只有三种:播放、暂停、单步;
// 单步只在暂停态有效——运行中单步没有语义,回执里直接拒绝。每条回执带
// 递增序号,远端据此对齐请求与响应。

export type PreviewState = 'idle' | 'playing' | 'paused';
export type PreviewCommand = 'play' | 'pause' | 'step';

export interface Receipt {
  seq: number;
  accepted: boolean;
  state: PreviewState;
}

export class PreviewSession {
  #devices = new Set<string>();
  #state: PreviewState = 'idle';
  #seq = 0;

  attach(deviceId: string): boolean {
    if (this.#devices.has(deviceId)) return false;
    this.#devices.add(deviceId);
    return true;
  }

  detach(deviceId: string): boolean {
    return this.#devices.delete(deviceId);
  }

  devices(): string[] {
    return [...this.#devices];
  }

  state(): PreviewState {
    return this.#state;
  }

  command(deviceId: string, command: PreviewCommand): Receipt {
    if (!this.#devices.has(deviceId)) {
      throw new Error(`未知设备 ${deviceId}`);
    }
    this.#seq += 1;
    let accepted = true;
    if (command === 'play') this.#state = 'playing';
    else if (command === 'pause') this.#state = 'paused';
    else if (this.#state !== 'paused') accepted = false;
    return { seq: this.#seq, accepted, state: this.#state };
  }
}

单步被拒绝时不改状态,这一点很重要:否则「运行中点单步」会把预览偷偷暂停,用户以为还在跑。回执带序号,是为了让远端能丢弃迟到响应,避免旧响应覆盖新操作的结果。

下面是 test/09-2-device-preview-remote-control.test.ts,层级 L1:

// file: test/09-2-device-preview-remote-control.test.ts
// tier: L1
// verify: node --test test/09-2-device-preview-remote-control.test.ts
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { PreviewSession } from '../src/preview-session.ts';

test('连接与断开设备', () => {
  const s = new PreviewSession();
  assert.equal(s.attach('pixel-8'), true);
  assert.equal(s.attach('pixel-8'), false);
  assert.deepEqual(s.devices(), ['pixel-8']);
  assert.equal(s.detach('pixel-8'), true);
  assert.deepEqual(s.devices(), []);
});

test('play 进入播放态', () => {
  const s = new PreviewSession();
  s.attach('d1');
  const r = s.command('d1', 'play');
  assert.equal(r.accepted, true);
  assert.equal(r.state, 'playing');
});

test('pause 进入暂停态', () => {
  const s = new PreviewSession();
  s.attach('d1');
  s.command('d1', 'play');
  assert.equal(s.command('d1', 'pause').state, 'paused');
});

test('单步仅在暂停态被接受', () => {
  const s = new PreviewSession();
  s.attach('d1');
  assert.equal(s.command('d1', 'step').accepted, false);
  s.command('d1', 'pause');
  assert.equal(s.command('d1', 'step').accepted, true);
});

test('未知设备指令报错', () => {
  const s = new PreviewSession();
  assert.throws(() => s.command('ghost', 'play'), /未知设备 ghost/);
});

test('回执序号单调递增', () => {
  const s = new PreviewSession();
  s.attach('d1');
  assert.equal(s.command('d1', 'play').seq, 1);
  assert.equal(s.command('d1', 'pause').seq, 2);
  assert.equal(s.command('d1', 'step').seq, 3);
});

本机实测(Node v26.5.0,node --test test/09-2-device-preview-remote-control.test.ts):tests 6 · pass 6 · fail 0,耗时约 40 ms。

前两条钉设备连接与播放;第三条钉暂停;第四条钉单步前置条件;后两条钉未知设备与回执序号。

小结

  • 预览状态是显式状态机:idle、playing、paused。
  • 单步只在 paused 态接受,拒绝时不改状态。
  • 回执序号递增,远端据此丢弃迟到响应。
  • 设备连接与断开幂等,界面无需做额外判断。

阅读导航:上一节:9.1 远程连接与协议 · 下一节:9.3 多机与线上诊断 。

继续阅读

探索更多技术文章

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

全部文章 返回首页

「books」更多文章

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