feat: 支持 toast

This commit is contained in:
richarjiang
2026-03-16 21:04:38 +08:00
parent b05ef71368
commit 45bb6b35ae
13 changed files with 933 additions and 0 deletions

View File

@@ -0,0 +1,58 @@
import { Node, Prefab, instantiate, find } from 'cc';
/**
* Toast 管理器
* 单例模式,统一管理 Toast 提示的显示
*/
export class ToastManager {
private static _instance: ToastManager | null = null;
private _prefab: Prefab | null = null;
private _container: Node | null = null;
static get instance(): ToastManager {
if (!this._instance) {
this._instance = new ToastManager();
}
return this._instance;
}
private constructor() {}
/**
* 初始化 Toast 管理器
* @param prefab Toast 预制体
* @param container Toast 容器节点(默认为 Canvas
*/
init(prefab: Prefab, container?: Node): void {
this._prefab = prefab;
this._container = container ?? find('Canvas');
}
/**
* 显示 Toast 提示
* @param content 提示内容
* @param duration 显示时长(毫秒),默认 2000ms
*/
show(content: string, duration: number = 2000): void {
if (!this._prefab || !this._container) {
console.error('[ToastManager] 未初始化,请先调用 init()');
return;
}
const node = instantiate(this._prefab);
this._container.addChild(node);
// 动态获取 Toast 组件
const toast = node.getComponent('Toast') as any;
if (toast && typeof toast.show === 'function') {
toast.show(content, duration);
}
}
/**
* 静态快捷方法
*/
static show(content: string, duration: number = 2000): void {
ToastManager.instance.show(content, duration);
}
}

View File

@@ -0,0 +1,9 @@
{
"ver": "4.0.24",
"importer": "typescript",
"imported": true,
"uuid": "0ea3615a-071b-4938-81aa-be8615bd5322",
"files": [],
"subMetas": {},
"userData": {}
}