13.1 UI 画布与锚点布局
UI 要适配不同分辨率,锚点系统解决「控件跟着哪走」:拉伸锚点让控件随父容器缩放,居中锚点让控件保持尺寸居中。本节把锚点布局做出来。
本章与站内既有内容的分工
| 主题 | 站内既有内容 | 本节做什么 |
|---|---|---|
| 场景集成 | 12.3 特效与场景集成 | 3D 特效入场景;本节管 2D UI |
| 运行时 UI | 游戏引擎实现原理 | 引擎渲染 UI;本节管编辑布局 |
| 全卷脉络 | 本卷目录 | toc 列章节;锚点布局是 UI 编辑器的核心 |
从零实现:UI 画布与锚点布局
anchoredRect 由父矩形、锚点与边距计算控件的最终矩形。stretchAnchors 给拉伸锚点(min=max=0 四边贴齐),centerAnchors 给居中锚点。canvasToPixel 把归一化坐标转像素。preserveAspect 在父容器内按源宽高比等比适配并居中。
对标与示例边界
Unity RectTransform 的 anchoredPosition 描述枢轴相对锚点参考位置的偏移。helio 示例直接从父矩形、归一化锚点和边距计算子矩形。示例采用自己的坐标及边距约定。 Unity RectTransform.anchoredPosition
可运行产物:6 条用例
下面是 src/canvas-layout.ts,层级 L1,完整可编译。运行命令 node --test test/13-1-canvas-layout-anchor.test.ts:
// file: src/canvas-layout.ts
// tier: L1
// verify: node --test test/13-1-canvas-layout-anchor.test.ts
//
// 视图(第十三章):UI 与关卡编辑器——画布、布局与锚点。
// UI 编辑器的核心是锚点布局:控件在父矩形内的位置由「锚点」(0..1 的
// 归一化坐标) 与「边距」共同决定。锚点贴合父容器边缘时控件随之拉伸,
// 锚点聚于一点时控件保持固定尺寸。分辨率变化时按设计比例留黑边(letterbox)。
export interface Rect {
x: number;
y: number;
w: number;
h: number;
}
export interface Anchors {
minX: number;
minY: number;
maxX: number;
maxY: number;
}
export interface Margins {
left: number;
right: number;
top: number;
bottom: number;
}
/** 按锚点与边距计算子控件矩形。 */
export function anchoredRect(parent: Rect, anchors: Anchors, margins: Margins): Rect {
const left = parent.x + parent.w * anchors.minX + margins.left;
const right = parent.x + parent.w * anchors.maxX - margins.right;
const top = parent.y + parent.h * anchors.minY + margins.top;
const bottom = parent.y + parent.h * anchors.maxY - margins.bottom;
return { x: left, y: top, w: right - left, h: bottom - top };
}
/** 全屏拉伸锚点(四边贴边)。 */
export function stretchAnchors(): Anchors {
return { minX: 0, minY: 0, maxX: 1, maxY: 1 };
}
/** 中心固定锚点(锚在父中心)。 */
export function centerAnchors(): Anchors {
return { minX: 0.5, minY: 0.5, maxX: 0.5, maxY: 0.5 };
}
/** 归一化画布坐标转像素。 */
export function canvasToPixel(parent: Rect, u: number, v: number): { x: number; y: number } {
return { x: parent.x + parent.w * u, y: parent.y + parent.h * v };
}
/** 按设计分辨率做 letterbox 适配,返回居中缩放后的画布矩形。 */
export function preserveAspect(parent: Rect, designW: number, designH: number): Rect {
const scale = Math.min(parent.w / designW, parent.h / designH);
const w = designW * scale;
const h = designH * scale;
return { x: parent.x + (parent.w - w) / 2, y: parent.y + (parent.h - h) / 2, w, h };
}
anchoredRect 的计算分两步:锚点给出控件两端贴靠的父坐标,边距再内缩或外扩。preserveAspect 处理「保持比例」——宽高比不同的容器里用 letterbox 居中,UI 不拉伸变形。
下面是 test/13-1-canvas-layout-anchor.test.ts,层级 L1:
// file: test/13-1-canvas-layout-anchor.test.ts
// tier: L1
// verify: node --test test/13-1-canvas-layout-anchor.test.ts
import { test } from 'node:test';
import assert from 'node:assert/strict';
import {
anchoredRect,
stretchAnchors,
centerAnchors,
canvasToPixel,
preserveAspect,
} from '../src/canvas-layout.ts';
const PARENT = { x: 0, y: 0, w: 800, h: 600 };
const NO_MARGIN = { left: 0, right: 0, top: 0, bottom: 0 };
test('全屏锚点铺满父容器', () => {
const r = anchoredRect(PARENT, stretchAnchors(), NO_MARGIN);
assert.deepEqual(r, { x: 0, y: 0, w: 800, h: 600 });
});
test('中心锚点 + 固定尺寸得中心控件', () => {
const r = anchoredRect(PARENT, centerAnchors(), {
left: -50,
right: -50,
top: -25,
bottom: -25,
});
assert.equal(r.x, 350);
assert.equal(r.y, 275);
assert.equal(r.w, 100);
assert.equal(r.h, 50);
});
test('边距在锚点基础上内缩', () => {
const r = anchoredRect(
PARENT,
stretchAnchors(),
{ left: 10, right: 20, top: 5, bottom: 15 },
);
assert.deepEqual(r, { x: 10, y: 5, w: 770, h: 580 });
});
test('归一化坐标转像素', () => {
assert.deepEqual(canvasToPixel(PARENT, 0.5, 0.5), { x: 400, y: 300 });
});
test('letterbox 保持比例并居中', () => {
const r = preserveAspect(PARENT, 400, 600);
assert.equal(r.h, 600);
assert.equal(r.w, 400);
assert.equal(r.x, 200);
});
test('更宽的父容器按高度适配', () => {
const r = preserveAspect({ x: 0, y: 0, w: 1600, h: 600 }, 400, 600);
assert.equal(r.w, 400);
assert.equal(r.x, 600);
});
本机实测(Node v26.5.0,node --test test/13-1-canvas-layout-anchor.test.ts):tests 6 · pass 6 · fail 0,耗时约 40 ms。
前两条钉拉伸与居中锚点。第三条钉边距内缩。第四条钉归一化转像素。第五、六条钉等比适配与更宽容器居中。
小结
- 锚点 = 贴靠参照 + 边距,拉伸与居中两种语义。
- preserveAspect 用 letterbox 保比例。
- 归一化坐标支撑跨分辨率设计。
- UI 章从锚点布局起步。
阅读导航:上一节:特效与场景集成 · 下一节:关卡摆放与对齐 。
继续阅读
探索更多技术文章
浏览归档,发现更多关于系统设计、工具链和工程实践的内容。