// panel/index.js
Editor.Panel.extend({
style: `
:host {
margin: 5px;
display: flex;
flex-direction: column;
}
h2 { color: #f90; }
.wrapper {
box-sizing: border-box;
border: 2px solid white;
font-size: 20px;
font-weight: bold;
}
.top {
height: 30px;
border-color: red;
}
.middle {
flex: 1;
overflow: auto;
border-color: green;
}
.bottom {
height: 30px;
border-color: blue;
}
`,
template: `
<h2>创建脚本</h2>
<div class="layout horizontal">
<ui-input id="sript_name" value="请填写你的脚本名"></ui-input>
<ui-button id="btn_create">创建自定义脚本</ui-button>
</div>
<hr />
<div">游戏脚本</div>
<hr />
<div class="layout horizontal">
<ui-select id="script_select_game" value = 0>
<option value="0">Hero</option>
<option value="1">Enemy</option>
<option value="2">Battle_Ctr</option>
<option value="3">Game_Ctr</option>
<option value="4">Level_Ctr</option>
</ui-select>
<ui-button id="btn_create_script_game" class="flex-1">创建游戏脚本</ui-button>
</div>
<hr />
<div>UI脚本 </div>
<hr />
<div class="layout horizontal">
<ui-select id="script_select_ui" value = 0>
<option value="0">Panel_About</option>
<option value="1">Panel_Achievement</option>
<option value="2">Panel_Battle</option>
<option value="3">Panel_Exit</option>
<option value="4">Panel_Guide</option>
<option value="5">Panel_Level</option>
<option value="6">Panel_Lose</option>
<option value="7">Panel_Pause</option>
<option value="8">Panel_Start</option>
<option value="9">Panel_SelectRole</option>
<option value="10">Panel_SetUp</option>
<option value="11">Panel_Tip</option>
<option value="12">Panel_Team</option>
<option value="13">Panel_Tech</option>
<option value="14">Panel_UpLv_Role</option>
<option value="15">Panel_UpLv_Equip</option>
<option value="16">Panel_UpLv_Skill</option>
<option value="17">Panel_Win</option>
</ui-select>
<ui-button id="btn_create_script_ui" class="flex-1">创建UI脚本</ui-button>
</div>
<hr />
<div class="layout horizontal">
<div class="layout vertical">
<div>生命周期函数****</div>
<ui-checkbox id="fun1">[onLoad]</ui-checkbox>
<ui-checkbox id="fun2">[start]</ui-checkbox>
<ui-checkbox id="fun3">[update]</ui-checkbox>
</div>
<div class="layout vertical">
<div>****自定义函数****</div>
<ui-checkbox id="fun4">[initVar]</ui-checkbox>
<ui-checkbox id="fun5">[init]</ui-checkbox>
<ui-checkbox id="fun6">[UpdatePanel]</ui-checkbox>
</div>
<div class="layout vertical">
<div>存放位置****</div>
<ui-select id="script_place" value = 0>
<option value="0">[assets/Script]</option>
<option value="1">[assets/Script/Game]</option>
<option value="2">[asset/Script/UI]</option>
<option value="3">[assets/script]</option>
<option value="4">[assets/script/game]</option>
<option value="5">[asset/script/ui]</option>
<option value="6">[assets/Scripts]</option>
<option value="7">[asset/scripts]</option>
</ui-select>
</div>
</div>
<hr />
<div>代码预览</div>
<ui-markdown id="sript_perview" class="flex-1"></ui-markdown>
<hr />
<div>创建状态: <span id="state_create">--</span></div>
<div class="top">
</div>
<div class="middle layout vertical">
</div>
<div class="bottom layout horizontal end-justified">
<ui-button id="btn_perview" class="green">预览</ui-button>
</div>
`,
$: {//声明格式:变量:'#id'
sript_name: '#sript_name',
btn_create: '#btn_create',
sript_perview: '#sript_perview',
state_create: '#state_create',
btn_perview: '#btn_perview',
script_select_game:'#script_select_game',
script_select_ui:'#script_select_ui',
btn_create_script_game:'#btn_create_script_game',
btn_create_script_ui:'#btn_create_script_ui',
fun1:'#fun1',fun2:'#fun2',fun3:'#fun3',
fun4:'#fun4',fun5:'#fun5',fun6:'#fun6',
script_place:'#script_place'
},
ready () {
//初始化预览
this.$sript_perview.value = '--------';
var data = '';
var scriptName_Game = [
"Hero", "Enemy", "Battle_Ctr",
"Game_Ctr", "Level_Ctr",
];
var scriptName_UI = [
"Panel_About", "Panel_Achievement",
"Panel_Battle",
"Panel_Exit",
"Panel_Guide",
"Panel_Level", "Panel_Lose",
"Panel_Pause",
"Panel_Start", "Panel_SelectRole", "Panel_SetUp",
"Panel_Tip", "Panel_Team", "Panel_Tech",
"Panel_UpLv_Role", "Panel_UpLv_Equip", "Panel_UpLv_Skill",
"Panel_Win",
];
var scriptPlace = [
"Script", "Script/Game", "Script/UI",
"script", "script/game", "script/ui",
"Scripts","scripts",
];
var fs= require('fs');
//创建脚本 并预览
this.$btn_create.addEventListener('confirm', () => {
var ScriptName = this.$sript_name.value;
let index_folder = parseInt(this.$script_place.value);
var FolderName = scriptPlace[index_folder];
var url = 'db://assets/' + FolderName + '/' + ScriptName + '.ts';
// fs.exists(url, function(exists) {
// Editor.log('asedasdasd'+exists);
// });
//脚本存在校验 API有问题,不去判断,不会覆盖元脚本
// var st = Editor.assetdb.exists(url); //return true or false
// if (st) { Editor.log('创建脚本:中断\n脚本已存在'); return; }
//脚本创建
data = '';
data = 'const {ccclass, property} = cc._decorator;';
data += '\n';
data += '@ccclass'; data += '\n';
data += 'export default class ' + ScriptName + ' extends cc.Component {';
data += '\n';data += '\n';
if(this.$fun1.value){ data += 'onLoad(){ }';data += '\n\n'; }
if(this.$fun2.value){ data += 'start(){ }';data += '\n\n'; }
if(this.$fun3.value){ data += 'update(dt){ }';data += '\n\n'; }
if(this.$fun4.value){ data += 'initVar(){ }';data += '\n\n'; }
if(this.$fun5.value){ data += 'init(){ }';data += '\n\n'; }
if(this.$fun6.value){ data += 'UpdatePanel(){ }';data += '\n\n'; }
data += '\n'; data += '}';
Editor.assetdb.create(url,
data,
(err, results) => {
if (err)
Editor.log('创建脚本error', err);
results.forEach(function (result) {
// result.uuid
// result.parentUuid
// result.url
// result.path
// result.type
});
});
Editor.log('创建脚本:完成');
//更改首栏输入内容 和 预览内容
this.$sript_name.value = ScriptName;
this.$sript_perview.value = data;
//处理完成回调
this.$state_create.innerText = '创建成功';
setTimeout(() => { this.$state_create.innerText = '--'; }, 1000);
});
//预览脚本
this.$btn_perview.addEventListener('confirm', () => {
var ScriptName = this.$sript_name.value;
let index_folder = parseInt(this.$script_place.value);
var FolderName = scriptPlace[index_folder];
var url = 'db://assets/' + FolderName + '/' + ScriptName + '.ts';
//脚本创建
data = '';
data = 'const {ccclass, property} = cc._decorator;';
data += '\n';
data += '@ccclass'; data += '\n';
data += 'export default class ' + ScriptName + ' extends cc.Component {';
data += '\n';data += '\n';