import { Node, UITransform, Vec3, v3 } from 'cc'; import { GBundle, GPath } from 'db://assets/script/game/ConfigRes'; import { RedDotComp, RedDotID } from './RedDotComp'; const DEFAULT_POS = v3(32, 32, 0); /** 在按钮/格子上挂红点:主按钮走 RedDotComp,格子走局部显隐 */ export class RedDotView { static bind(btn: Node | null | undefined, id: RedDotID): void { if (!btn?.isValid || id === RedDotID.无) return; const dot = this._ensureDot(btn, DEFAULT_POS); if (!dot) return; dot.active = true; let comp = dot.getComponent(RedDotComp); if (!comp) comp = dot.addComponent(RedDotComp); comp.id = id; comp.checkRedDotShow(); } static setLocal(parent: Node | null | undefined, show: boolean, pos?: Vec3): void { if (!parent?.isValid) return; if (!show) { const exist = parent.getChildByName('dot'); if (exist?.isValid) exist.active = false; return; } const dot = this._ensureDot(parent, pos ?? DEFAULT_POS); if (!dot) return; dot.active = true; const icon = dot.getChildByName('icon'); if (icon?.isValid) icon.active = true; dot.setSiblingIndex(parent.children.length - 1); } private static _ensureDot(parent: Node, pos: Vec3): Node | null { let dot = parent.getChildByName('dot'); if (dot?.isValid) return dot; const n = gg.res.getNode(GPath.Prefab, 'dot', GBundle.Comm); if (!n?.isValid) return null; n.name = 'dot'; n.parent = parent; n.setPosition(pos.x, pos.y, pos.z); if (!n.getComponent(UITransform)) n.addComponent(UITransform); return n; } }