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: add audioPlugin to editor Game View and mute toggle button

esengine2645047c9f72
diff --git a/editor/src/panels/game-view/GameViewPanel.ts b/editor/src/panels/game-view/GameViewPanel.ts
index 9e15f19e5..882cd28d3 100644
--- a/editor/src/panels/game-view/GameViewPanel.ts
+++ b/editor/src/panels/game-view/GameViewPanel.ts
@@ -5,6 +5,7 @@ import { GameViewToolbar } from './GameViewToolbar';
import { GameViewRenderer } from '../../renderer/GameViewRenderer';
import { getSharedRenderContext } from '../../renderer/SharedRenderContext';
import { getPlayModeService } from '../../services/PlayModeService';
+import { Audio } from 'esengine';
export interface GameViewPanelOptions {
projectPath?: string;
@@ -42,6 +43,9 @@ export class GameViewPanel implements PanelInstance, Resizable {
this.scale_ = scale;
this.updateCanvasSize();
},
+ onMuteToggle: (muted) => {
+ Audio.setMasterVolume(muted ? 0 : 1);
+ },
});
this.buildUI();
diff --git a/editor/src/panels/game-view/GameViewToolbar.ts b/editor/src/panels/game-view/GameViewToolbar.ts
index 387b1f1f0..b188f1668 100644
--- a/editor/src/panels/game-view/GameViewToolbar.ts
+++ b/editor/src/panels/game-view/GameViewToolbar.ts
@@ -24,6 +24,7 @@ export interface GameViewToolbarCallbacks {
onSpeedChange(speed: number): void;
onResolutionChange(preset: ResolutionPreset): void;
onScaleChange(scale: number): void;
+ onMuteToggle(muted: boolean): void;
}
export class GameViewToolbar {
@@ -42,6 +43,8 @@ export class GameViewToolbar {
private customWidthInput_: HTMLInputElement | null = null;
private customHeightInput_: HTMLInputElement | null = null;
private currentPreset_: ResolutionPreset = RESOLUTION_PRESETS[0];
+ private muteBtn_: HTMLButtonElement | null = null;
+ private muted_ = false;
private previewUrlEl_: HTMLElement | null = null;
constructor(container: HTMLElement, callbacks: GameViewToolbarCallbacks) {
@@ -71,6 +74,7 @@ export class GameViewToolbar {
<option value="2">2x</option>
<option value="4">4x</option>
</select>
+ <button class="es-btn es-btn-icon es-gameview-mute" title="Mute Audio">${icons.volume(14)}</button>
<div class="es-toolbar-divider"></div>
<select class="es-gameview-resolution">${options}<option value="custom">Custom</option></select>
<div class="es-gameview-custom-size" style="display:none">
@@ -108,12 +112,22 @@ export class GameViewToolbar {
this.customHeightInput_ = this.container_.querySelector('.es-gameview-custom-h');
this.scaleSelect_ = this.container_.querySelector('.es-gameview-scale');
+ this.muteBtn_ = this.container_.querySelector('.es-gameview-mute');
+
this.playBtn_?.addEventListener('click', () => this.callbacks_.onPlay());
this.pauseBtn_?.addEventListener('click', () => this.callbacks_.onPause());
this.resumeBtn_?.addEventListener('click', () => this.callbacks_.onResume());
this.stepBtn_?.addEventListener('click', () => this.callbacks_.onStepFrame());
this.stopBtn_?.addEventListener('click', () => this.callbacks_.onStop());
+ this.muteBtn_?.addEventListener('click', () => {
+ this.muted_ = !this.muted_;
+ this.muteBtn_!.innerHTML = this.muted_ ? icons.volumeX(14) : icons.volume(14);
+ this.muteBtn_!.title = this.muted_ ? 'Unmute Audio' : 'Mute Audio';
+ this.muteBtn_!.classList.toggle('es-active', this.muted_);
+ this.callbacks_.onMuteToggle(this.muted_);
+ });
+
this.speedSelect_?.addEventListener('change', () => {
const speed = parseFloat(this.speedSelect_!.value);
this.callbacks_.onSpeedChange(speed);
@@ -207,6 +221,7 @@ export class GameViewToolbar {
this.resumeBtn_ = null;
this.stepBtn_ = null;
this.stopBtn_ = null;
+ this.muteBtn_ = null;
this.speedSelect_ = null;
this.fpsDisplay_ = null;
this.resolutionSelect_ = null;
diff --git a/editor/src/renderer/SharedRenderContext.ts b/editor/src/renderer/SharedRenderContext.ts
index da2684e6e..ecd3079a3 100644
--- a/editor/src/renderer/SharedRenderContext.ts
+++ b/editor/src/renderer/SharedRenderContext.ts
@@ -14,6 +14,7 @@ import {
assetPlugin,
prefabsPlugin,
animationPlugin,
+ audioPlugin,
RenderPipeline,
Renderer,
createMaskProcessor,
@@ -156,6 +157,7 @@ export class SharedRenderContext {
app.addPlugin(assetPlugin);
app.addPlugin(prefabsPlugin);
app.addPlugin(animationPlugin);
+ app.addPlugin(audioPlugin);
for (const plugin of uiPlugins) {
app.addPlugin(plugin);
}
diff --git a/editor/src/utils/icons.ts b/editor/src/utils/icons.ts
index 2a84ac95d..963e1b6ed 100644
--- a/editor/src/utils/icons.ts
+++ b/editor/src/utils/icons.ts
@@ -38,6 +38,7 @@ import {
Layers,
Code,
Volume2,
+ VolumeX,
Braces,
MousePointer2,
Tag,
@@ -159,6 +160,7 @@ export const icons = {
layers: (size?: number) => renderIcon(Layers as IconNode, size),
code: (size?: number) => renderIcon(Code as IconNode, size),
volume: (size?: number) => renderIcon(Volume2 as IconNode, size),
+ volumeX: (size?: number) => renderIcon(VolumeX as IconNode, size),
braces: (size?: number) => renderIcon(Braces as IconNode, size),
pointer: (size?: number) => renderIcon(MousePointer2 as IconNode, size),
rotateCw: (size?: number) => renderIcon(RotateCw as IconNode, size),