t1k:cocos:playable:layout
| Field | Value |
|---|---|
| Module | playable |
| Version | 0.5.6 |
| Effort | high |
| Tools | — |
Keywords: ads, aspect ratio, cocos, layout, playable, responsive, safe area, UI
How to invoke
Section titled “How to invoke”/t1k:cocos:playable:layoutResponsiveLayoutService
Section titled “ResponsiveLayoutService”Cocos Component singleton that detects device aspect ratio on load and on every canvas resize, categorizes it into NARROW/STANDARD/WIDE, fires ScreenResizedSignal via SignalBus, and provides helpers for adaptive node positioning and scaling. Attach to a persistent node in the scene (e.g. Canvas root). See also: t1k-cocos-playable-signalbus.
Import paths:
db://assets/PLAGameFoundation/layout/ResponsiveLayoutService
Details
Section titled “Details”- API reference: categories, signal, properties, methods
- Usage examples: signal subscription, adjustForAspectRatio, scaleToFit, getSafeArea
Integration Points
Section titled “Integration Points”ScreenResizedSignalfires once ononLoadimmediately after detection — subscribe beforeonLoadcompletes if you need the initial value, or readservice.categorydirectly instart().- Plays well with the parameter system: subscribe to
ScreenResizedSignalinParameterControllerto re-apply layout-sensitive parameter values when the device rotates or the browser is resized. getSafeArea()returns zeros on web — safe to call unconditionally.- Only one
ResponsiveLayoutServiceinstance is allowed; a secondonLoadcall destroys the newer component.
Common Mistakes
Section titled “Common Mistakes”- Reading
ResponsiveLayoutService.instancebefore the component’sonLoadhas run — returns null. Always guard with a null check or access instart()/ after a frame delay. - Calling
adjustForAspectRatiowithout subscribing toScreenResizedSignal— positions only set once and will not update if browser is resized. - Using
scaleToFiton a node whoseUITransform.contentSizeis zero — method early-returns silently. SetcontentSizeexplicitly. - ES2017 target: do not use optional chaining (
?.) or nullish coalescing (??) in game code.
Gotchas
Section titled “Gotchas”- Cocos
Widgetaligns relative to parent at layout time — animating the parent breaks the widget unlessalignFlagsare reapplied per frame (orWidget.targetis locked). - Safe-area inset on iOS in-app webview is not always reported — provide a manual fallback inset of 24px top, 12px bottom.