t1k:cocos:playable:toast
| Field | Value |
|---|---|
| Module | playable |
| Version | 0.5.6 |
| Effort | high |
| Tools | — |
Keywords: ads, cocos, notification, playable, pooled, toast, ToastController, UI
How to invoke
Section titled “How to invoke”/t1k:cocos:playable:toastToastController — Pooled Toast Notifications
Section titled “ToastController — Pooled Toast Notifications”Singleton at PLAGameFoundation/ui/utils/ToastController.ts. Integrates with ObjectPoolManager — pool key "Toast", prefab path resources/prefab/Toast. See also: t1k-cocos-playable-async-utilities (duration timing), t1k-cocos-playable-gameflow (where debug toasts are shown).
Import: db://assets/PLAGameFoundation/ui/utils/ToastController
Architecture
Section titled “Architecture”ToastController (singleton)└── ObjectPoolManager pool: "Toast" ← prefab: resources/prefab/Toast └── Toast (Component on prefab) ├── Label (text) ├── UIOpacity (fade animations) └── Node background (Sprite for bg color)Toast auto-recycles itself after animation completes via recycle() → recycleToast().
Setup (one-time, in GameView or LoadingView)
Section titled “Setup (one-time, in GameView or LoadingView)”// Must be called before any show() callsawait ToastController.instance.Initialize();
// Optional: override defaultsToastController.instance.defaultDuration = 2.0;ToastController.instance.defaultPosition = ToastPosition.TOP_CENTER;ToastController.instance.defaultColor = new Color(255, 255, 255, 255);ToastController.instance.defaultBgColor = new Color(50, 50, 50, 200);ToastController.instance.defaultFontSize = 24;ToastController.instance.initialPoolSize = 5;
// Optional: override container (defaults to scene Canvas)ToastController.instance.setToastContainer(myCanvasNode);Positions — ToastPosition enum
Section titled “Positions — ToastPosition enum”9 presets + custom:
TOP_LEFT,TOP_CENTER,TOP_RIGHTCENTER_LEFT,CENTER,CENTER_RIGHTBOTTOM_LEFT,BOTTOM_CENTER,BOTTOM_RIGHTCUSTOM— requirescustomPosition: Vec3
All presets use 100px edge offset from canvas bounds.
Show API
Section titled “Show API”// Simple string — uses all defaultsToastController.instance.show("Level Complete!");
// Full configToastController.instance.show({ text: "Score: 100", position: ToastPosition.BOTTOM_CENTER, duration: 3.0, color: new Color(255, 215, 0, 255), // gold text bgColor: new Color(0, 0, 0, 180), fontSize: 32, animationType: 'slideUpFade' // default});
// Custom positionToastController.instance.show({ text: "Hit!", position: ToastPosition.CUSTOM, customPosition: new Vec3(100, -200, 0), animationType: 'fadeIn', duration: 1.5});Animation Types
Section titled “Animation Types”| Type | Behavior |
|---|---|
slideUpFade | Slides up 50px + fades in, then continues sliding + fades out (default) |
slideUp | Slides up 50px at full opacity, then slides up 30px more on exit |
fadeIn | Fades in only, holds, then fades out (no movement) |
Timing: 0.3s enter, (duration - 0.6)s hold, 0.3s exit.
Clear / Cleanup
Section titled “Clear / Cleanup”ToastController.instance.clearAll(); // recycles all active toasts immediatelyToastController.instance.dispose(); // unloads pool, clears singleton referenceCommon Mistakes
Section titled “Common Mistakes”- Calling
show()beforeInitialize()— logs error and does nothing. Alwaysawait Initialize()first. - Pool size too small for rapid toasts — increase
initialPoolSizebeforeInitialize(). - No
Toastprefab atresources/prefab/Toast—Initialize()will fail; prefab must exist in resources. - The
Toastprefab must haveToastcomponent attached withLabel,UIOpacity, andbackgroundnode wired in the Inspector. clearAll()does not cancel the underlying tweens gracefully —cleanup()is called on eachToastcomponent which stops tweens and resets opacity.- Container defaults to scene
Canvaschild named"Canvas"— if your Canvas node has a different name, callsetToastContainer().
Gotchas
Section titled “Gotchas”- Toast queue must not stack-block input — pointer-events must be
noneon toast containers. - Toast on top-layer must obey aspect ratio guards — landscape playable toasts stretched to portrait read as bugs.