Mobile, touch and orientation
Sur cette page
Mobile, touch and orientation
Most players arrive on a phone. If you tick mobile in the submission, reviewers test on one, and the game must offer the same features as on desktop.
Know the device#
await SDK.init();
const info = SDK.environment !== "disabled" ? SDK.user.systemInfo : null;
const isMobile = info?.device.type === "mobile";
const inAppShell = info !== null && info.applicationType !== "web";device.type is "desktop", "tablet" or "mobile". Use it, not "ontouchstart" in window: laptops have touch screens and tablets have keyboards. applicationType is "web", "pwa", "google_play_store" or "apple_store"; the last three mean the portal runs inside an app shell, where safe areas matter more. In local mode systemInfo is read from your own browser, so confirm the values the portal reports in the Preview tool.
Orientation#
You declare orientation in the submission: landscape, portrait or both. The portal shows a rotate prompt when the phone is held the wrong way, so you do not lock orientation in code.
- A game that declares both must lay out correctly in each. Listen to
resize, notorientationchange, and readinnerWidthandinnerHeightfresh. - Portrait games should make the playfield fit a 360 x 640 CSS pixel viewport without scrolling.
- Keep important UI away from the corners and bottom edge, where the OS reserves gesture zones.
Safe areas#
.hud {
padding-top: max(8px, env(safe-area-inset-top));
padding-bottom: max(8px, env(safe-area-inset-bottom));
padding-left: max(8px, env(safe-area-inset-left));
padding-right: max(8px, env(safe-area-inset-right));
}Include <meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover, user-scalable=no"> in index.html.
Touch controls#
- Use pointer events and track each finger by
pointerId. - Touch targets are at least 44 x 44 CSS pixels with a gap between them.
- Put virtual sticks and buttons where thumbs rest, and let the player see through them.
touch-action: noneon the canvas stops panning and pinch zoom. Adduser-select: noneand-webkit-touch-callout: noneto the body.- Never require hover or a right-click. Offer a long-press or a visible button instead.
- Show on-screen controls only when
device.typeis"mobile"or"tablet"and hide keyboard hints there. Show keyboard hints on desktop.
More input pitfalls are in control pitfalls.
Audio on iOS#
Resume the AudioContext inside the first touch or click handler. See audio on iOS. Also honour SDK.game.settings.muteAudio.
Memory and performance#
- Cap
devicePixelRatioat 1 on phones for large canvases; 2 is usually the most you can afford. - Keep textures compressed or small. A 2048 x 2048 texture uses about 16 MB of memory however small the file.
- Mobile initial download target is 20 MB. Above it the validator warns and reviewers test on a phone.
- Avoid allocating in the frame loop. Phones throttle when they heat up.
- Unity WebGL is off on iOS by default. Tick Works on iOS only after testing.
Text input#
Use a real <input> so the on-screen keyboard opens, and keep it visible when the visual viewport shrinks. Use window.visualViewport to reposition UI when the keyboard opens.
Ads and banners on small screens#
- Use banners on menus, game-over and shop screens, never over controls. A 320x50 or 320x100 banner fits a 360 px wide phone; see banners.
- Pause on
adStartedand refocus the canvas afteradFinished.
Test checklist#
- Playable at 360 x 640 and at 360 x 800 with no clipped buttons.
- Rotating the phone does not reset the game or break the layout, if both orientations are declared.
- No accidental zoom, text selection or pull-to-refresh.
- Sound starts after the first tap and respects mute.
- The game runs on a mid-range Android phone with a throttled network. The target is about 10 seconds to reach play on 4G.
- Works with the on-screen keyboard open when there is text input.