From 2ec88b32997febf0cd45498d0249f130635f48e5 Mon Sep 17 00:00:00 2001 From: dgflash Date: Fri, 26 Apr 2024 22:50:11 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8D=87=E7=BA=A7toast=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=EF=BC=8C=E8=BF=9E=E7=BB=AD=E5=BC=B9=E5=87=BA=E6=97=B6=E4=B8=8D?= =?UTF-8?q?=E4=BC=9A=E7=9B=B8=E4=BA=92=E8=A6=86=E7=9B=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- assets/core/gui/layer/LayerNotify.ts | 41 +++++++++++++++++++++++----- assets/core/gui/prompt/Notify.ts | 4 +++ 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/assets/core/gui/layer/LayerNotify.ts b/assets/core/gui/layer/LayerNotify.ts index ddf0589..360a5af 100644 --- a/assets/core/gui/layer/LayerNotify.ts +++ b/assets/core/gui/layer/LayerNotify.ts @@ -4,7 +4,7 @@ * @LastEditors: dgflash * @LastEditTime: 2022-09-02 13:44:12 */ -import { BlockInputEvents, Layers, Node, Widget } from "cc"; +import { BlockInputEvents, Layers, Node, Widget, find, instantiate } from "cc"; import { ViewUtil } from "../../utils/ViewUtil"; import { Notify } from "../prompt/Notify"; @@ -15,9 +15,13 @@ const WaitPrefabPath: string = 'common/prefab/wait'; * 滚动消息提示层 */ export class LayerNotify extends Node { - /** 游戏运行时永久缓冲资源 */ - private wait: Node = null!; private black!: BlockInputEvents; + /** 等待提示资源 */ + private wait: Node = null!; + /** 自定义弹出提示资源 */ + private notify: Node = null!; + /** 自定义弹出提示内容资源 */ + private notifyItem: Node = null!; constructor(name: string) { super(name); @@ -59,9 +63,32 @@ export class LayerNotify extends Node { * @param useI18n 是否使用多语言 */ toast(content: string, useI18n: boolean): void { - let childNode = ViewUtil.createPrefabNode(ToastPrefabPath) - let toastCom = childNode.getComponent(Notify)!; - childNode.parent = this; - toastCom.toast(content, useI18n); + try { + if (this.notify == null) { + this.notify = ViewUtil.createPrefabNode(ToastPrefabPath); + this.notifyItem = find("item", this.notify)!; + this.notifyItem.parent = null; + } + + this.notify.parent = this; + let childNode = instantiate(this.notifyItem); + let toastCom = childNode.getChildByName("prompt")!.getComponent(Notify)!; + childNode.parent = this.notify; + + toastCom.onComplete = () => { + if (this.notify.children.length == 0) { + this.notify.parent = null; + } + }; + toastCom.toast(content, useI18n); + + // 超过3个提示,就施放第一个提示 + if (this.notify.children.length > 3) { + this.notify.children[0].destroy(); + } + } + catch { + console.error("从oops-game-kit项目中拷贝 assets/bundle/common/prefab/notify.prefab 与 assets/bundle/common/anim/notify.anim 覆盖到本项目中"); + } } } \ No newline at end of file diff --git a/assets/core/gui/prompt/Notify.ts b/assets/core/gui/prompt/Notify.ts index 17058ef..c14a9d2 100644 --- a/assets/core/gui/prompt/Notify.ts +++ b/assets/core/gui/prompt/Notify.ts @@ -18,6 +18,9 @@ export class Notify extends Component { @property(Animation) private animation: Animation = null!; + /** 提示动画完成 */ + onComplete: Function = null!; + onLoad() { if (this.animation) this.animation.on(Animation.EventType.FINISHED, this.onFinished, this); @@ -25,6 +28,7 @@ export class Notify extends Component { private onFinished() { this.node.destroy(); + this.onComplete && this.onComplete(); } /**