在游戏开发中,UI界面是必不可少一个环节,在这个环节中,UI界面的脚本创建以及属性绑定是一个必不可少一个操作。
正常情况下,操作如下:
1、在对应目录下创建自定义命名脚本;
2、在脚本里面
@property(Component)
name:Component = null!
3、将节点拖入脚本组件上绑定;
数量少的情况下还好,如果界面比较复杂的话,还是比较麻烦的。所以写了一个自动化插件;
插件地址:链接
具体实现步骤:
1、生成脚本并绑定
async createScript() {
const type = Editor.Selection.getLastSelectedType();
const uuids = Editor.Selection.getSelected(type);
if (uuids && uuids[0]) {
const uuid = uuids[0];
const node = await Editor.Message.request('scene', 'query-node', uuid);
if (node) {
//获取选中节点的名字,脚本名字默认与选中节点名字一样;
const nodeName = node.name.value;
//创建脚本时的内容,可根据自己需求定制;
const scriptText = `
import { _decorator, Component, Node } from 'cc';
const { ccclass, property } = _decorator;
@ccclass('${nodeName}')
export class ${nodeName} extends Component {
start() {
}
// update (deltaTime: number) {
// }
}`;
//脚本完整的路径;
let scriptPath = '';
//脚本生成路径;
const createPath = await Editor.Profile