前言

在游戏开发中,咱们的开发流程个别是

  1. 制作预制体或者场景
  2. 创立脚本、申明属性
  3. 拖拽节点设置属性
  4. 编写逻辑

我开发了款半自动代码生成器工具次要是解决第2步的问题;之所以称之为半自动,因为我感觉全自动代码生成器应该做到两点:代码生成(第2步)+主动绑定(第3步)。主动绑定须要改变预制体文件,因为所有人的应用形式不尽相同,呈现的问题会比拟多,我喜爱绝对灵便,束缚比拟少的形式,所以我采纳了拖拽设置和代码设置相结合的形式解决主动绑定的问题。

性能介绍

  1. 导出与预制体同名的类文件
  2. 申明属性

  1. 如果属性是有效值进行赋值

  1. 如果属性是按钮,进行函数监听,并生成监听函数

  1. 将生成的类导出到指定目录
  2. 反对导出指定预制体文件或者目录,主动实别目录的子目录。
  3. 反对导出Creator 和 Laya 的预制体
  4. 应用TAG标记是否导出指定名称的属性,带有TAG标记符号的节点才会导出,我TAG是$。如果TAG是有效字符,那么会导出所有名称无效的节点。

  1. creator导出的属性名称前面带有类型,button带有sprite会同时输入。

目录阐明


creator_export:creator文件导出目录,这个目录工具会创立并且能够放到其余中央。
creator_prefabs: creator文件输出目录,个别会设置为我的项目的预制体文件夹。放到这里只是测试应用。
laya_export 和 laya_prefabs :同 creator文件夹。
creator_build.bat: window下的运行脚本,实际上就是直行node 并传递两个参数。如果是mac用户能够自行写一个sh脚本。
creator_prefab.js: creator文件导出的外围代码。
creator_template.txt: creator导出文件的模板文件,实践上就是字符替换。
file_util.js : 文件辅助类
laya_build.bat,laya_prefab.js,laya_template.txt: 同creator文件。

残缺代码

  1. creator导出文件
import BaseView from "../../../cfw/mvc/BaseView";const { ccclass, property } = cc._decorator;@ccclassexport default class LoginView extends BaseView {    @property({type: cc.Sprite, displayName: "logointro$Sprite"})    logointro$Sprite: cc.Sprite = null;    @property({type: cc.Sprite, displayName: "btn_buy_big$Sprite"})    btn_buy_big$Sprite: cc.Sprite = null;    @property({type: cc.Button, displayName: "btn_buy_big$Button"})    btn_buy_big$Button: cc.Button = null;        onLoad() {        if(!this.logointro$Sprite){this.logointro$Sprite = this.findChild("logointro$").getComponent(cc.Sprite)}        if(!this.btn_buy_big$Sprite){this.btn_buy_big$Sprite = this.findChild("btn_buy_big$").getComponent(cc.Sprite)}                if(!this.btn_buy_big$Button){this.btn_buy_big$Button = this.findChild("btn_buy_big$").getComponent(cc.Button)}                this.registerButtonByNode(this.btn_buy_big$Button,this.onbtn_buy_big$ButtonClick)                    }    onbtn_buy_big$ButtonClick(){        }        onDestroy(){        }}
  1. laya导出文件
import BaseView from "../../../cfw/mvc/BaseView";export default class TestView extends BaseView {    /** @prop {name:normal, tips:"normal", type:Node, default:null}*/    public normal: Laya.Button = null;    /** @prop {name:double, tips:"double", type:Node, default:null}*/    public double: Laya.Button = null;        constructor() { super(); }    onAwake() {        super.onAwake()        if(!this.normal){this.normal = this.findChild("normal")}                this.registerButtonByNode(this.normal,this.onnormalClick)                if(!this.double){this.double = this.findChild("double")}                this.registerButtonByNode(this.double,this.ondoubleClick)                    }    onEnable(): void {    }    onDisable(): void {    }        onnormalClick(){        }        ondoubleClick(){        }}

注意事项

  1. 节点的名称不能有空格,横线
  2. 不能用引擎曾经应用的变量名

结语

工具已上传到框架仓库中,有须要的自行拉取,如遇到问题能够微信找我沟通。

欢送扫码关注公众号《微笑游戏》,浏览更多内容。

欢送扫码关注公众号《微笑游戏》,浏览更多内容。