Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

feat(examples): record a run and share the clip

esenginee9b0143d3e08
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 3470527e3..ad1179064 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -16,6 +16,8 @@ published separately; it ships inside the editor.
### Added
+- **The `record-share` example.** Record a run, mark a highlight every few seconds, stop, and share the clip; the share button hides where the host cannot share, which is what a real game wants too.
+- **`tsc` and an outside editor type-check the official packages too.** Opening a project mirrors the packages the editor ships into `.esengine/packages/`, beside the SDK's types, and a tsconfig maps them with `"estella-plugin-*": ["./.esengine/packages/*"]` — the `wechat-minigame` template and the examples that use one already do.
- **A game can record a run and share the clip.** `Recorder` in `estella-plugin-minigame-services` starts, pauses, resumes and stops the host's screen recorder, and `highlight(before, after)` keeps the moment that just happened; `share()` then publishes the highlights, or the whole recording without any. WeChat keeps the video and shares it through its own call (at most 60 s of highlights); Douyin hands over a temp file and cuts the highlights on its side. `available` and `canShare` are separate answers, so a record button and a share button can each hide where the host lacks one. Every call resolves when the host says the thing happened, and a host that never answers is reported after 5 s rather than awaited forever. In the editor's play mode the recording is real — the canvas as WebM — and `canShare` is false there.
### Fixed
diff --git a/docs/astro/src/content/docs/publishing/ads-sharing.mdx b/docs/astro/src/content/docs/publishing/ads-sharing.mdx
index 6d15920bf..f1f342d96 100644
--- a/docs/astro/src/content/docs/publishing/ads-sharing.mdx
+++ b/docs/astro/src/content/docs/publishing/ads-sharing.mdx
@@ -155,7 +155,8 @@ where the host has no share call, `canShare` is `false`.
In the editor's play mode the recording is real: `stop()` hands back the
canvas as WebM, so a record button can be built and tried at your desk.
`canShare` is `false` there, so the share button hides itself exactly as it
-would on a host that cannot share.
+would on a host that cannot share. The `record-share` example is all of this in
+one screen.
## Friends leaderboard
diff --git a/docs/astro/src/content/docs/zh-cn/publishing/ads-sharing.mdx b/docs/astro/src/content/docs/zh-cn/publishing/ads-sharing.mdx
index ddcee7718..96cba22b3 100644
--- a/docs/astro/src/content/docs/zh-cn/publishing/ads-sharing.mdx
+++ b/docs/astro/src/content/docs/zh-cn/publishing/ads-sharing.mdx
@@ -139,7 +139,8 @@ if (recorder.canShare) await recorder.share({ title: '看我这一局' });
</Aside>
编辑器 Play 里的录屏是真的:`stop()` 把画布录成 WebM 交回来,录屏按钮在自己桌面上就能
-做和试。那里 `canShare` 为 `false`,分享按钮会像在不能分享的宿主上一样自己藏起来。
+做和试。那里 `canShare` 为 `false`,分享按钮会像在不能分享的宿主上一样自己藏起来。`record-share`
+示例把这些都放在了一个画面里。
## 好友排行榜
diff --git a/examples/README.md b/examples/README.md
index 9a5bf4a1a..1ae3c0187 100644
--- a/examples/README.md
+++ b/examples/README.md
@@ -17,6 +17,7 @@ for your own game.
| **event-system** | ECS | Decoupled gameplay with typed events — collect, score, react. |
| **scene-flow** | Basics | Runtime scene flow — menu → level 1 → level 2 through `SceneManager` fade transitions, over a persistent shell scene. |
| **save-load** | Basics | Versioned persistence — `SaveManager` save/load with a live v1→v2 migration, plus raw `Storage` preferences. |
+| **record-share** | Platform | Record a run, mark highlights, share the clip with the mini-game recorder — the share button hides where the host cannot share. |
| **timers-demo** | Basics | `TimerManager` delays, intervals and handles — pause/resume/cancel/reset plus `timeScale`, replacing hand-rolled `time.delta` accumulators. |
| **sprite-rendering** | Rendering | Draw sprites with rotation, tint and flipping. A SortingGroup shows a subtree taking over its members' layers. |
| **sprite-animation** | Animation | Frame animation via `.esanim` clips with an idle/walk switcher. |
diff --git a/examples/record-share/.gitignore b/examples/record-share/.gitignore
new file mode 100644
index 000000000..359472ac2
--- /dev/null
+++ b/examples/record-share/.gitignore
@@ -0,0 +1,7 @@
+node_modules/
+dist/
+.vscode/
+.idea/
+.DS_Store
+Thumbs.db
+.esengine/cache/
diff --git a/examples/record-share/assets/scenes/main.esscene b/examples/record-share/assets/scenes/main.esscene
new file mode 100644
index 000000000..53eb37a06
--- /dev/null
+++ b/examples/record-share/assets/scenes/main.esscene
@@ -0,0 +1,372 @@
+{
+ "version": 4,
+ "name": "Main",
+ "entities": [
+ {
+ "id": 0,
+ "name": "Camera",
+ "parent": null,
+ "children": [],
+ "components": [
+ {
+ "type": "Transform",
+ "data": {
+ "position": {
+ "x": 0,
+ "y": 0,
+ "z": 10
+ }
+ }
+ },
+ {
+ "type": "Camera",
+ "data": {
+ "projectionType": 1,
+ "orthoSize": 300,
+ "isActive": true,
+ "priority": 0
+ }
+ }
+ ],
+ "visible": true
+ },
+ {
+ "id": 1,
+ "name": "Sprite0",
+ "parent": null,
+ "children": [],
+ "components": [
+ {
+ "type": "Transform",
+ "data": {
+ "position": {
+ "x": -200,
+ "y": 0,
+ "z": 0
+ }
+ }
+ },
+ {
+ "type": "Sprite",
+ "data": {
+ "size": {
+ "x": 60,
+ "y": 60
+ },
+ "color": {
+ "r": 0.92,
+ "g": 0.28,
+ "b": 0.28,
+ "a": 1
+ },
+ "texture": "@uuid:37286c86-bd8d-4522-b7c4-11df50f49427"
+ }
+ },
+ {
+ "type": "Rotator",
+ "data": {}
+ }
+ ],
+ "visible": true
+ },
+ {
+ "id": 2,
+ "name": "Sprite1",
+ "parent": null,
+ "children": [],
+ "components": [
+ {
+ "type": "Transform",
+ "data": {
+ "position": {
+ "x": -100,
+ "y": 0,
+ "z": 0
+ }
+ }
+ },
+ {
+ "type": "Sprite",
+ "data": {
+ "size": {
+ "x": 60,
+ "y": 60
+ },
+ "color": {
+ "r": 0.79,
+ "g": 0.92,
+ "b": 0.28,
+ "a": 1
+ },
+ "texture": "@uuid:37286c86-bd8d-4522-b7c4-11df50f49427"
+ }
+ },
+ {
+ "type": "Rotator",
+ "data": {
+ "speed": 135,
+ "bobSpeed": 2.3,
+ "bobAmount": 40
+ }
+ }
+ ],
+ "visible": true
+ },
+ {
+ "id": 3,
+ "name": "Sprite2",
+ "parent": null,
+ "children": [],
+ "components": [
+ {
+ "type": "Transform",
+ "data": {
+ "position": {
+ "x": 0,
+ "y": 0,
+ "z": 0
+ }
+ }
+ },
+ {
+ "type": "Sprite",
+ "data": {
+ "size": {
+ "x": 60,
+ "y": 60
+ },
+ "color": {
+ "r": 0.28,
+ "g": 0.92,
+ "b": 0.54,
+ "a": 1
+ },
+ "texture": "@uuid:37286c86-bd8d-4522-b7c4-11df50f49427"
+ }
+ },
+ {
+ "type": "Rotator",
+ "data": {
+ "speed": 180,
+ "bobSpeed": 2.6,
+ "bobAmount": 50
+ }
+ }
+ ],
+ "visible": true
+ },
+ {
+ "id": 4,
+ "name": "Sprite3",
+ "parent": null,
+ "children": [],
+ "components": [
+ {
+ "type": "Transform",
+ "data": {
+ "position": {
+ "x": 100,
+ "y": 0,
+ "z": 0
+ }
+ }
+ },
+ {
+ "type": "Sprite",
+ "data": {
+ "size": {
+ "x": 60,
+ "y": 60
+ },
+ "color": {
+ "r": 0.28,
+ "g": 0.54,
+ "b": 0.92,
+ "a": 1
+ },
+ "texture": "@uuid:37286c86-bd8d-4522-b7c4-11df50f49427"
+ }
+ },
+ {
+ "type": "Rotator",
+ "data": {
+ "speed": 225,
+ "bobSpeed": 2.9,
+ "bobAmount": 60
+ }
+ }
+ ],
+ "visible": true
+ },
+ {
+ "id": 5,
+ "name": "Sprite4",
+ "parent": null,
+ "children": [],
+ "components": [
+ {
+ "type": "Transform",
+ "data": {
+ "position": {
+ "x": 200,
+ "y": 0,
+ "z": 0
+ }
+ }
+ },
+ {
+ "type": "Sprite",
+ "data": {
+ "size": {
+ "x": 60,
+ "y": 60
+ },
+ "color": {
+ "r": 0.79,
+ "g": 0.28,
+ "b": 0.92,
+ "a": 1
+ },
+ "texture": "@uuid:37286c86-bd8d-4522-b7c4-11df50f49427"
+ }
+ },
+ {
+ "type": "Rotator",
+ "data": {
+ "speed": 270,
+ "bobSpeed": 3.2,
+ "bobAmount": 70
+ }
+ }
+ ],
+ "visible": true
+ },
+ {
+ "id": 6,
+ "name": "Canvas",
+ "parent": null,
+ "children": [
+ 7,
+ 8
+ ],
+ "components": [
+ {
+ "type": "Transform",
+ "data": {}
+ },
+ {
+ "type": "Canvas",
+ "data": {
+ "designResolution": {
+ "x": 800,
+ "y": 600
+ },
+ "scaleMode": 2
+ }
+ },
+ {
+ "type": "UINode",
+ "data": {}
+ }
+ ],
+ "visible": true
+ },
+ {
+ "id": 7,
+ "name": "ButtonRow",
+ "parent": 6,
+ "children": [],
+ "components": [
+ {
+ "type": "Transform",
+ "data": {}
+ },
+ {
+ "type": "UINode",
+ "data": {
+ "position": 1,
+ "insetLeft": {
+ "value": 20,
+ "unit": 0
+ },
+ "insetRight": {
+ "value": 20,
+ "unit": 0
+ },
+ "insetBottom": {
+ "value": 20,
+ "unit": 0
+ },
+ "height": {
+ "value": 44,
+ "unit": 0
+ }
+ }
+ },
+ {
+ "type": "FlexContainer",
+ "data": {
+ "direction": 0,
+ "justifyContent": 1,
+ "alignItems": 1,
+ "gap": {
+ "x": 12,
+ "y": 0
+ }
+ }
+ }
+ ],
+ "visible": true
+ },
+ {
+ "id": 8,
+ "name": "Status",
+ "parent": 6,
+ "children": [],
+ "components": [
+ {
+ "type": "Transform",
+ "data": {}
+ },
+ {
+ "type": "UINode",
+ "data": {
+ "position": 1,
+ "insetLeft": {
+ "value": 20,
+ "unit": 0
+ },
+ "insetRight": {
+ "value": 20,
+ "unit": 0
+ },
+ "insetTop": {
+ "value": 20,
+ "unit": 0
+ },
+ "height": {
+ "value": 28,
+ "unit": 0
+ }
+ }
+ },
+ {
+ "type": "Text",
+ "data": {
+ "content": "",
+ "fontSize": 18,
+ "color": {
+ "r": 1,
+ "g": 1,
+ "b": 1,
+ "a": 1
+ },
+ "align": 1,
+ "verticalAlign": 1
+ }
+ }
+ ],
+ "visible": true
+ }
+ ]
+}
diff --git a/examples/record-share/assets/scenes/main.esscene.meta b/examples/record-share/assets/scenes/main.esscene.meta
new file mode 100644
index 000000000..ee278cac3
--- /dev/null
+++ b/examples/record-share/assets/scenes/main.esscene.meta
@@ -0,0 +1,8 @@
+{
+ "uuid": "1ff61328-011d-4318-a1c7-b349ac3eea0f",
+ "version": "2.0",
+ "type": "scene",
+ "importer": {
+ "autoMigrate": true
+ }
+}
diff --git a/examples/record-share/assets/textures/logo.png b/examples/record-share/assets/textures/logo.png
new file mode 100644
index 000000000..8beef0cfd
Binary files /dev/null and b/examples/record-share/assets/textures/logo.png differ
diff --git a/examples/record-share/assets/textures/logo.png.meta b/examples/record-share/assets/textures/logo.png.meta
new file mode 100644
index 000000000..983a8263f
--- /dev/null
+++ b/examples/record-share/assets/textures/logo.png.meta
@@ -0,0 +1,17 @@
+{
+ "uuid": "37286c86-bd8d-4522-b7c4-11df50f49427",
+ "version": "2.0",
+ "type": "texture",
+ "importer": {
+ "maxSize": 2048,
+ "filterMode": "linear",
+ "wrapMode": "repeat",
+ "premultiplyAlpha": false,
+ "sliceBorder": {
+ "left": 0,
+ "right": 0,
+ "top": 0,
+ "bottom": 0
+ }
+ }
+}
\ No newline at end of file
diff --git a/examples/record-share/project.esproject b/examples/record-share/project.esproject
new file mode 100644
index 000000000..032400f0c
--- /dev/null
+++ b/examples/record-share/project.esproject
@@ -0,0 +1,13 @@
+{
+ "formatVersion": "1",
+ "name": "Record & Share",
+ "description": "Record a run, keep its best moments, and share the clip — the mini-game recorder, with a share button that hides where the host cannot share.",
+ "tag": "Platform",
+ "version": "0.1.0",
+ "defaultScene": "assets/scenes/main.esscene",
+ "designResolution": {
+ "width": 800,
+ "height": 600
+ },
+ "spineVersion": "none"
+}
diff --git a/examples/record-share/src/components.ts b/examples/record-share/src/components.ts
new file mode 100644
index 000000000..c798456d8
--- /dev/null
+++ b/examples/record-share/src/components.ts
@@ -0,0 +1,8 @@
+import { defineComponent } from 'esengine';
+
+export const Rotator = defineComponent('Rotator', {
+ speed: 90,
+ baseY: 0,
+ bobSpeed: 2,
+ bobAmount: 30,
+});
diff --git a/examples/record-share/src/main.ts b/examples/record-share/src/main.ts
new file mode 100644
index 000000000..971610ec4
--- /dev/null
+++ b/examples/record-share/src/main.ts
@@ -0,0 +1,15 @@
+// Record & Share — record a run, mark a highlight every few seconds, stop, and
+// share the clip. Share is hidden where the host cannot share (the editor, a
+// browser), which is what a real game wants too.
+import { addPlugin, addStartupSystem, addSystemToSchedule, Schedule } from 'esengine';
+import { miniGameServicesPlugin } from 'estella-plugin-minigame-services';
+
+import './components';
+import { rotateSystem, colorPulseSystem } from './systems/animate';
+import { recordUiSystem, recordStateSystem } from './record';
+
+addPlugin(miniGameServicesPlugin);
+addSystemToSchedule(Schedule.Update, rotateSystem);
+addSystemToSchedule(Schedule.Update, colorPulseSystem);
+addStartupSystem(recordUiSystem);
+addSystemToSchedule(Schedule.Update, recordStateSystem);
diff --git a/examples/record-share/src/record.ts b/examples/record-share/src/record.ts
new file mode 100644
index 000000000..0bcddc07b
--- /dev/null
+++ b/examples/record-share/src/record.ts
@@ -0,0 +1,108 @@
+import {
+ defineSystem, Res, GetWorld, Time, Name, UIEvents, UINode, UIDisplay, Text, createButton, themeColors, px,
+} from 'esengine';
+import type { Entity, World, UIEventQueue, TextData, TimeData } from 'esengine';
+import { Recorder, type RecorderAPI } from 'estella-plugin-minigame-services';
+
+/** How often the demo pretends something worth keeping just happened. */
+const HIGHLIGHT_EVERY_S = 4;
+
+const ui = {
+ built: false,
+ record: null as Entity | null,
+ stop: null as Entity | null,
+ share: null as Entity | null,
+ status: null as Entity | null,
+ sinceHighlight: 0,
+ highlights: 0,
+ message: '',
+};
+
+export const recordUiSystem = defineSystem(
+ [Res(UIEvents), Res(Recorder), GetWorld()],
+ (events: UIEventQueue, recorder: RecorderAPI, world: World) => {
+ if (ui.built) return;
+ const row = world.findEntityByName('ButtonRow');
+ ui.status = world.findEntityByName('Status');
+ if (row === null || ui.status === null) return;
+ ui.built = true;
+
+ const c = themeColors();
+ const button = (label: string, onClick: () => void): Entity => {
+ const { entity } = createButton({
+ world, events, parent: row,
+ node: { width: px(150), height: px(44) },
+ states: { normal: { color: c.primary }, hover: { color: c.primary }, pressed: { color: c.primary } },
+ text: { content: label, color: c.onPrimary, fontSize: 16 },
+ onClick,
+ });
+ world.insert(entity, Name, { value: `${label}Button` });
+ return entity;
+ };
+
+ ui.record = button('Record', () => {
+ ui.highlights = 0;
+ ui.sinceHighlight = 0;
+ recorder.start({ maxSeconds: 60 }).catch((e: Error) => { ui.message = e.message; });
+ });
+ ui.stop = button('Stop', () => {
+ recorder.stop()
+ .then((r) => { ui.message = `recorded ${(r.durationMs / 1000).toFixed(1)}s, ${r.highlights.length} highlight(s)`; })
+ .catch((e: Error) => { ui.message = e.message; });
+ });
+ // A share has to come from the player's tap — both hosts refuse any other.
+ ui.share = button('Share', () => {
+ recorder.share({ title: 'Look at this run' })
+ .then(() => { ui.message = 'shared'; })
+ .catch((e: Error) => { ui.message = e.message; });
+ });
+ recorder.onFailure((e) => { ui.message = `recording stopped: ${e.message}`; });
+ },
+ { name: 'RecordUiSystem' },
+);
+
+export const recordStateSystem = defineSystem(
+ [Res(Recorder), Res(Time), GetWorld()],
+ (recorder: RecorderAPI, time: TimeData, world: World) => {
+ if (!ui.built) return;
+ const recording = recorder.state === 'recording' || recorder.state === 'paused';
+
+ if (recorder.state === 'recording') {
+ ui.sinceHighlight += time.delta;
+ if (ui.sinceHighlight >= HIGHLIGHT_EVERY_S) {
+ ui.sinceHighlight = 0;
+ ui.highlights++;
+ recorder.highlight(2, 1);
+ }
+ }
+
+ show(world, ui.record, recorder.available && !recording);
+ show(world, ui.stop, recording);
+ // Hidden, not disabled, where it cannot work: a button that only ever
+ // fails teaches the player to ignore it.
+ show(world, ui.share, recorder.canShare && recorder.last !== null && !recording);
+
+ const line = !recorder.available ? 'This platform cannot record.'
+ : recording ? `Recording… ${ui.highlights} highlight(s) marked`
+ : ui.message || (recorder.canShare ? 'Record a run, then share it.' : 'Record a run — sharing needs a mini-game host.');
+ setText(world, ui.status, line);
+ },
+ { name: 'RecordStateSystem' },
+);
+
+function show(world: World, entity: Entity | null, on: boolean): void {
+ if (entity === null || !world.valid(entity)) return;
+ const node = world.get(entity, UINode);
+ const display = on ? UIDisplay.Flex : UIDisplay.None;
+ if (node.display === display) return;
+ node.display = display;
+ world.insert(entity, UINode, node);
+}
+
+function setText(world: World, entity: Entity | null, content: string): void {
+ if (entity === null || !world.valid(entity) || !world.has(entity, Text)) return;
+ const t = world.get(entity, Text) as TextData;
+ if (t.content === content) return;
+ t.content = content;
+ world.insert(entity, Text, t);
+}
diff --git a/examples/record-share/src/systems/animate.ts b/examples/record-share/src/systems/animate.ts
new file mode 100644
index 000000000..cd1b39421
--- /dev/null
+++ b/examples/record-share/src/systems/animate.ts
@@ -0,0 +1,32 @@
+import {
+ defineSystem, Query, Mut, Res, Time,
+ Transform, Sprite,
+} from 'esengine';
+import { Rotator } from '../components';
+
+export const rotateSystem = defineSystem(
+ [Query(Mut(Transform), Rotator), Res(Time)],
+ (query, time) => {
+ for (const [_entity, transform, rotator] of query) {
+ const angle = rotator.speed * time.elapsed * (Math.PI / 180);
+ const cos = Math.cos(angle);
+ const sin = Math.sin(angle);
+ transform.rotation = { w: cos, x: 0, y: 0, z: sin };
+
+ transform.position.y =
+ rotator.baseY + Math.sin(time.elapsed * rotator.bobSpeed) * rotator.bobAmount;
+ }
+ },
+ { name: 'RotateSystem' }
+);
+
+export const colorPulseSystem = defineSystem(
+ [Query(Mut(Sprite), Rotator), Res(Time)],
+ (query, time) => {
+ for (const [_entity, sprite, rotator] of query) {
+ const pulse = 0.7 + 0.3 * Math.sin(time.elapsed * rotator.bobSpeed * 1.5);
+ sprite.color.a = pulse;
+ }
+ },
+ { name: 'ColorPulseSystem' }
+);
diff --git a/examples/record-share/thumbnail.png b/examples/record-share/thumbnail.png
new file mode 100644
index 000000000..3b6eda466
Binary files /dev/null and b/examples/record-share/thumbnail.png differ
diff --git a/examples/record-share/thumbnail.png.meta b/examples/record-share/thumbnail.png.meta
new file mode 100644
index 000000000..a15c7e43f
--- /dev/null
+++ b/examples/record-share/thumbnail.png.meta
@@ -0,0 +1,17 @@
+{
+ "uuid": "ebe4641c-95f5-4c46-b50a-c9f334d58b41",
+ "version": "2.0",
+ "type": "texture",
+ "importer": {
+ "maxSize": 2048,
+ "filterMode": "linear",
+ "wrapMode": "repeat",
+ "premultiplyAlpha": false,
+ "sliceBorder": {
+ "left": 0,
+ "right": 0,
+ "top": 0,
+ "bottom": 0
+ }
+ }
+}
diff --git a/examples/record-share/tsconfig.json b/examples/record-share/tsconfig.json
new file mode 100644
index 000000000..86043684a
--- /dev/null
+++ b/examples/record-share/tsconfig.json
@@ -0,0 +1,21 @@
+{
+ "compilerOptions": {
+ "target": "ES2020",
+ "module": "ESNext",
+ "moduleResolution": "bundler",
+ "strict": true,
+ "esModuleInterop": true,
+ "skipLibCheck": true,
+ "forceConsistentCasingInFileNames": true,
+ "declaration": false,
+ "outDir": "./dist",
+ "rootDir": ".",
+ "paths": {
+ "esengine": ["./.esengine/sdk/index.d.ts"],
+ "esengine/wasm": ["./.esengine/sdk/wasm.d.ts"],
+ "estella-plugin-*": ["./.esengine/packages/*"]
+ }
+ },
+ "include": ["src/**/*"],
+ "exclude": ["node_modules"]
+}
diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjs
index e1e44b737..731f87d15 100644
--- a/tools/releaseNotes.mjs
+++ b/tools/releaseNotes.mjs
@@ -972,6 +972,12 @@ export const NOTED = {
{ note: 'Sharing, in-game purchase, the friends leaderboard and the recorder can be used at all.' },
'feat(editor): a project resolves and type-checks the official packages the editor ships':
{ note: 'Sharing, in-game purchase, the friends leaderboard and the recorder can be used at all.' },
+ 'feat(examples): record a run and share the clip':
+ { note: 'The `record-share` example.' },
+ 'feat(editor): a project carries the official packages for tsc and an outside editor':
+ { note: '`tsc` and an outside editor type-check the official packages too.' },
+ 'feat(tooling): the examples type-check the official packages the way a project does':
+ { note: '`tsc` and an outside editor type-check the official packages too.' },
'fix(agent): the history toggle presses grey again, not the accent':
{ note: "The agent drawer's History button shows it is open with a grey press again, not the accent colour 0.71 gave it." },
};