import { _decorator, Button, Component, Label, Node, sp, Sprite,Animation, RichText, ProgressBar } from "cc"; /**自动绑定所需字段 */ enum attributeType { Node = 'm_', Node2 = 'n_', Label = 'lab_', RichText = 'ric_', Button = 'btn_', Sprite = 'spr_', Spine = 'spi_', Animation = 'ani_', ProgressBar = 'pro_' } /**数据类型 */ interface attributeInfo{ mKey:attributeType, mtype:string, mtype2:any, } const UIParseConfig : Record = { [attributeType.Node]: { mKey: attributeType.Node, mtype: "Node", mtype2: Node }, [attributeType.Node2]: { mKey: attributeType.Node2, mtype: "Node", mtype2: Node }, [attributeType.Label]: { mKey: attributeType.Label, mtype: "Label", mtype2: Label }, [attributeType.Button]: { mKey: attributeType.Button, mtype: "Button", mtype2: Button }, [attributeType.Sprite]: { mKey: attributeType.Sprite, mtype: "Sprite", mtype2: Sprite }, [attributeType.Spine]: { mKey: attributeType.Spine, mtype: "sp.Skeleton", mtype2: sp.Skeleton }, [attributeType.RichText]: { mKey: attributeType.RichText, mtype: "RichText", mtype2: RichText }, [attributeType.Animation]: { mKey: attributeType.Animation, mtype: "Animation", mtype2: Animation }, [attributeType.ProgressBar]: { mKey: attributeType.ProgressBar, mtype: "ProgressBar", mtype2: ProgressBar }, } export class UIParseTools{ private static tempNodeType: attributeType[] = [attributeType.Node, attributeType.Node2]; static CheckNode(tNode: Node): string { for (const key in UIParseConfig) { const Info = UIParseConfig[key] as attributeInfo; if (this.ParseNode(tNode, Info)) { return this.getStr(tNode, Info); } } return ``; } static AutoBindNode(tNode:Node,Target:any){ for (const key in UIParseConfig) { const Info = UIParseConfig[key] as attributeInfo; if (tNode.name[0] == Info.mKey[0] && tNode.name.includes(Info.mKey)) { if (this.tempNodeType.includes(Info.mKey)) { Target[tNode.name] = tNode; } else { Target[tNode.name] = tNode.getComponent(Info.mtype2); } return; } } } /**获取字符串 */ private static getStr(tNode: Node, Info: attributeInfo) { return ` @property(${Info.mtype}) ${tNode.name}: ${Info.mtype} = null;`; } /**添加按钮组件 */ private static addBtnCommont(TargetNode: Node, Type: any) { let tClass = TargetNode.getComponent(Type); if (tClass) { return true; } tClass = TargetNode.addComponent(Type); if (!tClass) { return false; } switch (Type) { case Button: { let btn = tClass as Button; btn.transition = Button.Transition.SCALE btn.zoomScale = 0.9; } break; } return true; } private static ParseNode(TargetNode: Node, Info: attributeInfo): boolean { if (TargetNode.name.includes(` `)) { return false; } if (TargetNode.name == `label`) { return false; } const tidx = TargetNode.name.indexOf(Info.mKey); if (Info.mKey == attributeType.Node || Info.mKey == attributeType.Node2) { return tidx == 0; } return tidx == 0 && this.addBtnCommont(TargetNode, Info.mtype2); } }