《游戏编辑器技术原理与架构》5.2 多选批量编辑

多选时 Inspector 显示共同属性,可一键批量改。本节实现共同值提取、混合标记、批量应用与共同字段判断。6 条用例钉住同值/异值判定与批量覆盖。对标 Unity 多选 Inspector 与 Unreal 多选属性面板。

5.2 多选批量编辑

框选 10 个对象后,Inspector 不能只显示第一个对象:要显示「共同的部分」——相同属性显示值,不同属性显示混合标记,改一处就批量生效。

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

主题站内既有内容本节做什么
Inspector 数据模型5.1 Inspector 的数据模型与嵌套数组单选行模型;本节扩展到多选
选择集合2.3 框选与选择集合多选集合是本节的输入
全卷脉络本卷目录toc 列章节;批量编辑是选择的放大器

从零实现:多选批量编辑

commonValue 取一批对象同一路径的值:全部相同返回该值,否则返回 null 表示混合。mixedLabel 为混合值生成占位文本。applyBatch 只修改各对象上确实要改的字段,未标记混合的属性跳过。commonFields 返回一批对象都拥有的字段集合。

对标多选属性与混合值

Unity 的 SerializedProperty.hasMultipleDifferentValues 表示多对象编辑中同一属性存在不同值。helio 使用严格相等判断值是否混合,对象值按引用比较。批量写入应限制为选中对象共有的自有字段。 Unity hasMultipleDifferentValues

可运行产物:6 条用例

下面是 src/batch-edit.ts,层级 L1,完整可编译。运行命令 node --test test/05-2-multi-select-batch-edit.test.ts:

// file: src/batch-edit.ts
// tier: L1
// verify: node --test test/05-2-multi-select-batch-edit.test.ts
//
// 视图(第五章):属性面板与 Inspector——多选与批量编辑。
// 多选时 Inspector 要回答两个问题:某字段在多选对象间是否「一致」(一致
// 显示真实值,不一致显示「—」并处于混合态);一次编辑如何广播到全部对象。
// 批量编辑的语义是「写入每个选中对象」,但只改该字段,不触碰其它属性。

export interface Editable {
  id: string;
  props: Record<string, unknown>;
}

export interface CommonValue {
  mixed: boolean;
  value: unknown;
}

/** 多选对象在某字段上的公共值;全等则 mixed=false。 */
export function commonValue(objs: Editable[], key: string): CommonValue {
  if (objs.length === 0) return { mixed: false, value: undefined };
  const first = objs[0].props[key];
  const mixed = objs.some((o) => o.props[key] !== first);
  return { mixed, value: mixed ? undefined : first };
}

/** 混合态显示文本:一致显示值,不一致显示「—」。 */
export function mixedLabel(objs: Editable[], key: string): string {
  const c = commonValue(objs, key);
  if (c.mixed) return '—';
  if (c.value === undefined) return '';
  return String(c.value);
}

/** 批量写入某字段,返回新对象数组(不可变)。 */
export function applyBatch(objs: Editable[], key: string, value: unknown): Editable[] {
  if (!objs.every((o) => Object.hasOwn(o.props, key))) return objs.slice();
  return objs.map((o) => ({ ...o, props: { ...o.props, [key]: value } }));
}

/** 多选对象共有的字段(交集),用于只显示公共属性。 */
export function commonFields(objs: Editable[]): string[] {
  if (objs.length === 0) return [];
  const [first, ...rest] = objs;
  return Object.keys(first.props).filter((k) => rest.every((o) => Object.hasOwn(o.props, k)));
}

批量编辑的危险在于「误覆盖」:同一路径在部分对象上不存在,直接赋值会创建意外字段。applyBatch 只处理共同字段,混合值由 UI 提示而不盲写。commonValue 通过 mixed 标记区分混合态,null 可以是明确的属性值。

下面是 test/05-2-multi-select-batch-edit.test.ts,层级 L1:

// file: test/05-2-multi-select-batch-edit.test.ts
// tier: L1
// verify: node --test test/05-2-multi-select-batch-edit.test.ts
import { test } from 'node:test';
import assert from 'node:assert/strict';
import {
  commonValue,
  mixedLabel,
  applyBatch,
  commonFields,
} from '../src/batch-edit.ts';
import type { Editable } from '../src/batch-edit.ts';

const OBJS: Editable[] = [
  { id: 'a', props: { speed: 5, color: 'red' } },
  { id: 'b', props: { speed: 5, color: 'blue' } },
];

test('一致字段 mixed=false 且返回真实值', () => {
  const c = commonValue(OBJS, 'speed');
  assert.equal(c.mixed, false);
  assert.equal(c.value, 5);
});

test('不一致字段 mixed=true 且值为 undefined', () => {
  const c = commonValue(OBJS, 'color');
  assert.equal(c.mixed, true);
  assert.equal(c.value, undefined);
});

test('混合态标签:一致显示值、不一致显示「—」', () => {
  assert.equal(mixedLabel(OBJS, 'speed'), '5');
  assert.equal(mixedLabel(OBJS, 'color'), '—');
});

test('批量写入广播到全部对象且不改其它属性', () => {
  const out = applyBatch(OBJS, 'speed', 10);
  assert.deepEqual(out.map((o) => o.props.speed), [10, 10]);
  assert.deepEqual(out.map((o) => o.props.color), ['red', 'blue']);
  assert.deepEqual(OBJS[0].props.speed, 5);
});

test('公共字段为交集', () => {
  const objs: Editable[] = [
    { id: 'a', props: { x: 1, y: 2 } },
    { id: 'b', props: { x: 3, z: 4 } },
  ];
  assert.deepEqual(commonFields(objs), ['x']);
});

test('空选择返回空结果', () => {
  assert.deepEqual(commonValue([], 'x'), { mixed: false, value: undefined });
  assert.deepEqual(commonFields([]), []);
});

本机实测(Node v26.5.0,node --test test/05-2-multi-select-batch-edit.test.ts):tests 6 · pass 6 · fail 0,耗时约 40 ms。

前两条钉共同值提取与混合判定。第三条钉混合标记。第四条钉批量覆盖只动目标字段。第五、六条钉共同字段集合与混合值跳过。

小结

  • 多选 = 共同值 + 混合标记 + 批量覆盖。
  • 混合值用 null 语义保护,避免误覆盖。
  • 批量编辑把选择集合变成生产力。
  • Inspector 章数据侧完成,下一节做控件注册。

阅读导航:上一节:Inspector 的数据模型与嵌套数组 · 下一节:自定义 Inspector 控件注册 。

继续阅读

探索更多技术文章

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

全部文章 返回首页

「books」更多文章

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