You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
148 lines
5.7 KiB
148 lines
5.7 KiB
import { _decorator, Button, Component, Label, Node, sp, Sprite,Animation, RichText } from "cc";
|
|
import UIParse from "../UIParse";
|
|
import { EDITOR } from "cc/env";
|
|
import { SysDefine } from "../Config/SysDefine";
|
|
import EditorTools from "../../Framework/Editor/EditorTools";
|
|
import { UIParseTools } from "./UIParseTools";
|
|
|
|
const { ccclass, property, menu, executeInEditMode } = _decorator;
|
|
@ccclass
|
|
@executeInEditMode
|
|
@menu("编辑器操作/UI脚本生成")
|
|
export default class UIScriptsCreate extends Component {
|
|
static isAutoAddCommont: boolean = true;
|
|
static UIPath: string = "ui_script%s";
|
|
static AutoBindHead: string = '//#region 节点自动生成';
|
|
static AutoBindTail: string = '//#endregion 节点自动生成';
|
|
static importDir:string[]=[
|
|
"../",
|
|
"../../",
|
|
"../../../",
|
|
]
|
|
|
|
protected async onLoad() {
|
|
await this.ScriptsCheck();
|
|
}
|
|
|
|
async ScriptsCheck(){
|
|
let Commont = this.node.getComponent(UIParse);
|
|
if(Commont){
|
|
await UIScriptsCreate.ReParsing(Commont,this.node);
|
|
this.destroy();
|
|
}else{
|
|
let findBundleName = await this.getNodePrefabBundle();
|
|
let UIPath ="";
|
|
let haveDir = 0;
|
|
ConsoleEx.Log("findBundleName",findBundleName)
|
|
if (findBundleName && findBundleName.length > 1) {
|
|
if (findBundleName.includes("/")) {
|
|
haveDir = 2;
|
|
} else {
|
|
haveDir = 1;
|
|
}
|
|
UIPath = `${Editor.Project.path}/assets/Scripts/${UIScriptsCreate.UIPath.replace("%s", "")}/${findBundleName}/${`${this.node.name}${SysDefine.NameSuffix}`}${`.ts`}`
|
|
} else {
|
|
UIPath = `${Editor.Project.path}/assets/Scripts/${UIScriptsCreate.UIPath.replace("%s", "")}/${`${this.node.name}${SysDefine.NameSuffix}`}${`.ts`}`
|
|
}
|
|
ConsoleEx.Log("UIPath",UIPath)
|
|
let tempdata:{ScriptText:string, UIPath:string} = {
|
|
ScriptText:UIScriptsCreate.CreateNewScripts(this.node.name,this.node,haveDir),
|
|
UIPath:UIPath,
|
|
}
|
|
let nodeUUId = this.node.uuid;
|
|
this.destroy();
|
|
await Editor.Message.request("editor-tools","ui-scritpts-creator-new",tempdata);
|
|
let com = this.node.getComponent(`${this.node.name}${SysDefine.NameSuffix}`);
|
|
if(!com){
|
|
Editor.Message.request('scene', 'create-component', {
|
|
uuid: nodeUUId,
|
|
component: `${this.node.name}${SysDefine.NameSuffix}`
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
async getNodePrefabBundle():Promise<string>{
|
|
/**当前节点不是预设 */
|
|
if(!this.node["_prefab"]){
|
|
ConsoleEx.Log("当前节点不是预设 不寻找对应Bundle")
|
|
return "";
|
|
}
|
|
let path = await Editor.Message.request('asset-db', 'query-url', this.node["_prefab"].asset.uuid);
|
|
ConsoleEx.Log("目标路径",path);
|
|
let tempPath =path.replace("db://assets/","");
|
|
let bundle = await EditorTools.getBundleName("db://assets/", tempPath.split("/"));
|
|
ConsoleEx.Log("查找到的包 ",bundle);
|
|
return bundle;
|
|
}
|
|
|
|
|
|
static async ReParsing(Commont: UIParse, tNode:Node) {
|
|
if (Commont) {
|
|
//@ts-ignore
|
|
let data = await Editor.Message.request('asset-db', 'query-asset-info', Commont.__scriptUuid);
|
|
let file_body = UIScriptsCreate.generateBindNode(tNode, ``) + `
|
|
`;
|
|
//@ts-ignore
|
|
let tempdata:{data:any,AutoBindHead:string,AutoBindTail:string, ScriptText:string} = {
|
|
data:data,
|
|
AutoBindHead:UIScriptsCreate.AutoBindHead,
|
|
AutoBindTail:UIScriptsCreate.AutoBindTail,
|
|
ScriptText:file_body,
|
|
}
|
|
await Editor.Message.request("editor-tools","ui-scritpts-creator-old",tempdata);
|
|
}
|
|
}
|
|
|
|
static newfile_head(scriptName: string,haveDir:number): string {
|
|
return "" +
|
|
"//#region 文件引入\n" +
|
|
'import { _decorator, Button, Component, Label, Node, sp, Sprite,Animation, RichText } from "cc";\r\n' +
|
|
`import BaseUIForm from "${this.importDir[haveDir]}Mgr_UI/BaseUIForm";\n` +
|
|
"//#endregion 文件引入\n" +
|
|
"const {ccclass, property,menu} = _decorator;\n" +
|
|
`@ccclass("${scriptName}${SysDefine.NameSuffix}")\n` +
|
|
`export default class ${scriptName}${SysDefine.NameSuffix} extends BaseUIForm{`;
|
|
}
|
|
|
|
static getNewfile_body(targetNode:Node): string {
|
|
return `
|
|
${this.AutoBindHead}
|
|
${this.generateBindNode(targetNode, ``)}
|
|
${this.AutoBindTail}`;
|
|
}
|
|
|
|
static get newfile_Fail(): string {
|
|
return `\n
|
|
onButtonClick(button:Button){\n
|
|
/**点击事件触发*/
|
|
switch (button) {
|
|
default:
|
|
ConsoleEx.Log(button);
|
|
break;
|
|
}
|
|
}\n${`}`}`;
|
|
}
|
|
|
|
static CreateNewScripts(scriptName: string,TargetNode:Node,haveDir:number): string {
|
|
return `${this.newfile_head(scriptName,haveDir).toString()}${this.getNewfile_body(TargetNode).toString()}${this.newfile_Fail.toString()}`;
|
|
}
|
|
|
|
static generateBindNode(TgetNode: Node, BindString: string): string {
|
|
for (let i = 0; i < TgetNode.children.length; i++) {
|
|
const element = TgetNode.children[i];
|
|
if(element.getComponent(UIParse)){
|
|
continue;
|
|
}
|
|
BindString += UIParseTools.CheckNode(element);
|
|
BindString += this.generateBindNode(element, ``);
|
|
}
|
|
return BindString;
|
|
}
|
|
|
|
}
|
|
|
|
if(EDITOR){
|
|
window["UIScriptsCreate"] = UIScriptsCreate;
|
|
}
|
|
|
|
|