2.2 自定义资产 Inspector
资产类型决定 Inspector 的版式。注册表按类型解析,高优先级覆盖低优先级;多选时先按类型分组,再求共同字段的交集,最后逐字段判断值是否相同——不同就是混合值,界面显示破折号,编辑后统一写回。
本章与站内既有内容的分工
| 主题 | 站内既有内容 | 本节做什么 |
|---|---|---|
| 资产类型 | 《资产管线》 | 卷二定义资产模型;本节按资产类型挂 Inspector |
| 属性面板 | 《视图与子编辑器》 第 5 章 | 卷三做属性多选;本节把同一套思路用于资产 |
| 全卷脉络 | 本卷目录 | 本节补齐自定义 Inspector 的资产分支 |
从零实现:自定义资产 Inspector
注册表用类型作键,优先级更高的注册直接替换已有项,因此每个类型只保留一个 Inspector。多选共同字段以第一个资产的键顺序为准,逐键检查其余资产是否拥有;字段值用 Object.is 比较,全部相同才返回该值,否则返回 MIXED 符号。
对标控件接口
Unity 的 CustomEditor 声明目标对象类型,CanEditMultipleObjects 标记支持多对象编辑。helio 示例先解析资产类型,再计算公共字段和混合值。真正的批量修改还需通过文档事务校验和提交。 Unity CustomEditor · Unity CanEditMultipleObjects
可运行产物:6 条用例
下面是 src/asset-inspector.ts,层级 L1,完整可编译。运行命令 node --test test/02-2-custom-asset-inspector.test.ts:
// file: src/asset-inspector.ts
// tier: L1
// verify: node --test test/02-2-custom-asset-inspector.test.ts
//
// 卷四(第二章):自定义资产 Inspector。
// 资产类型决定 Inspector 的版式。注册表按类型解析;多选时先把资产按类型
// 分组,再求共同字段的交集,最后逐字段判断「值是否相同」——不同就是混合值
// MIXED,界面显示为破折号,编辑后统一写回。
export interface AssetLike {
path: string;
type: string;
props: Record<string, unknown>;
}
export const MIXED = Symbol('mixed');
export class AssetInspectorRegistry {
#map = new Map<string, { inspectorId: string; priority: number }>();
register(assetType: string, inspectorId: string, priority = 0): void {
const current = this.#map.get(assetType);
if (!current || priority > current.priority) {
this.#map.set(assetType, { inspectorId, priority });
}
}
resolve(assetType: string): string | undefined {
return this.#map.get(assetType)?.inspectorId;
}
count(): number {
return this.#map.size;
}
}
/** 多选共同字段:所有资产都拥有的属性键,顺序取第一个资产的属性顺序。 */
export function sharedFields(items: AssetLike[]): string[] {
if (items.length === 0) return [];
const first = items[0];
return Object.keys(first.props).filter((key) =>
items.every((item) => Object.prototype.hasOwnProperty.call(item.props, key)),
);
}
/** 字段值:全部严格相同则返回该值,否则返回 MIXED。 */
export function fieldValue(items: AssetLike[], key: string): unknown {
if (items.length === 0) return undefined;
const first = items[0].props[key];
return items.every((item) => Object.is(item.props[key], first)) ? first : MIXED;
}
export function groupByType(items: AssetLike[]): Map<string, string[]> {
const groups = new Map<string, string[]>();
for (const item of items) {
const bucket = groups.get(item.type) ?? [];
bucket.push(item.path);
groups.set(item.type, bucket);
}
return groups;
}
MIXED 用 Symbol 而不是字符串,避免某个资产的真实值恰好等于 'MIXED' 时产生歧义;界面层只判断 value === MIXED,不需要做类型检查。分组函数保留选择顺序,让多选面板按用户点选先后展示。
下面是 test/02-2-custom-asset-inspector.test.ts,层级 L1:
// file: test/02-2-custom-asset-inspector.test.ts
// tier: L1
// verify: node --test test/02-2-custom-asset-inspector.test.ts
import { test } from 'node:test';
import assert from 'node:assert/strict';
import {
AssetInspectorRegistry,
MIXED,
sharedFields,
fieldValue,
groupByType,
} from '../src/asset-inspector.ts';
test('按资产类型解析 Inspector', () => {
const r = new AssetInspectorRegistry();
r.register('texture', 'texture-inspector');
assert.equal(r.resolve('texture'), 'texture-inspector');
assert.equal(r.resolve('mesh'), undefined);
});
test('高优先级注册覆盖低优先级', () => {
const r = new AssetInspectorRegistry();
r.register('texture', 'default-texture', 0);
r.register('texture', 'custom-texture', 5);
assert.equal(r.resolve('texture'), 'custom-texture');
assert.equal(r.count(), 1);
});
test('多选共同字段取交集并保留首个顺序', () => {
const items = [
{ path: 'a', type: 'texture', props: { size: 64, format: 'rgba', mip: true } },
{ path: 'b', type: 'texture', props: { format: 'rgba', mip: false, size: 128 } },
];
assert.deepEqual(sharedFields(items), ['size', 'format', 'mip']);
});
test('字段值全同返回值、不同返回 MIXED', () => {
const items = [
{ path: 'a', type: 'texture', props: { format: 'rgba', mip: true } },
{ path: 'b', type: 'texture', props: { format: 'rgba', mip: false } },
];
assert.equal(fieldValue(items, 'format'), 'rgba');
assert.equal(fieldValue(items, 'mip'), MIXED);
});
test('空选择返回空字段与 undefined', () => {
assert.deepEqual(sharedFields([]), []);
assert.equal(fieldValue([], 'any'), undefined);
});
test('按类型分组保持选择顺序', () => {
const groups = groupByType([
{ path: 'a.png', type: 'texture', props: {} },
{ path: 'b.mesh', type: 'mesh', props: {} },
{ path: 'c.png', type: 'texture', props: {} },
]);
assert.deepEqual(groups.get('texture'), ['a.png', 'c.png']);
assert.deepEqual(groups.get('mesh'), ['b.mesh']);
});
本机实测(Node v26.5.0,node --test test/02-2-custom-asset-inspector.test.ts):tests 6 · pass 6 · fail 0,耗时约 40 ms。
前两条钉类型解析与优先级覆盖;第三条钉共同字段交集;第四条钉同值与混合值;后两条钉空选择与类型分组。
小结
- 资产 Inspector 按类型解析,高优先级注册覆盖低优先级。
- 多选先分组再求共同字段,字段顺序取第一个资产。
- 混合值用 Symbol 表示,避免与真实值冲突。
- 分组保持用户点选顺序,界面不会自行重排。
阅读导航:上一节:2.1 自定义属性控件 · 下一节:2.3 控件的可访问性 。
继续阅读
探索更多技术文章
浏览归档,发现更多关于系统设计、工具链和工程实践的内容。