spineset/scene-obtain.js
2022-03-31 08:59:17 +08:00

223 lines
8.2 KiB
JavaScript

"use strict";
let fs = require("fs");
let path = require("path");
module.exports = {
/**
* 獲取場景節點下的配置信息
* @param event event
* @param data 這邊把panel的Node帶進來
*/
"get-default-info": function (event, request) {
let self = this;
// Editor.log(`example: ${JSON.stringify(args[2])}`);
let Canvas = cc.find("Canvas");
var response = null;
var args = request.args;
let node = cc.find("Canvas/Prefab");
let node_uuid = "";
if (node) {
node_uuid = node.uuid;
}
let example = cc.find("Canvas/example");
let example_uuid = "";
if (example) {
example_uuid = example.uuid;
}
response = JSON.stringify({ node_uuid: node_uuid, example_uuid: example_uuid })
if (event.reply) {
event.reply(null, response);
}
},
/**
* 獲取場景節點下的配置信息
* @param event event
* @param data 這邊把panel的Node帶進來
*/
"get-asset-info": function (event, request) {
let self = this;
// Editor.log(`example: ${JSON.stringify(args[2])}`);
let Canvas = cc.find("Canvas");
var response = null;
var args = request.args;
let node = cc.find(args[0]._name);
if (!node) {
node = this.getAllChildByUuid(Canvas, args[0]._nodeID);
}
let asset = args[1];
let example = cc.find(args[2]._name);
if (!example) {
example = this.getAllChildByUuid(Canvas, args[2]._nodeID);
}
if (!example) {
example = cc.instantiate(new cc.Node);
}
let scale = args[3]._value;
let anim = args[4]._value;
// Editor.log(`scale: ${scale}`);
// Editor.log(`node: ${node.name}`);
// Editor.log(`example: ${example.name}`);
Editor.assetdb.queryInfoByUuid(asset._value, function (err, info) {
// info.path// info.url // info.type
if (err) {
response = err;
}
let FolderName = [];
// Editor.log("讀取資料夾中");
fs.readdir(info.path, (err, files) => {
files.forEach(file => {
if (file.indexOf(".meta") === -1) {
FolderName.push(file);
// Editor.log(file);
}
});
// Editor.log(`資料夾數量: ${FolderName.length}`);
Editor.log(`根據來源生成${FolderName.length}個節點`);
node.removeAllChildren();
for (let i = 0; i < FolderName.length; i++) {
// for (let i = 0; i < 1; i++) {
// let i = 11;
self.CreateNode(node, example, info.path, FolderName[i], i + 1, scale, anim);
}
});
// response = JSON.stringify(info);
if (event.reply) {
event.reply(null, "response");
}
});
},
/**
* 把節點需要輸出的資料設置到每個json
* @param Parent 需要取得資料的節點
* @param Node 需要取得資料的節點
*/
async CreateNode(Parent, Node, path, FolderName, name, scale, anim) {
scale = scale ? scale : 1;
anim = anim ? anim : null;
let file_path = `${path}\\${FolderName}`;
let abs_path = `${path}\\${FolderName}\\${FolderName}.json`;
let url = Editor.remote.assetdb.fspathToUrl(abs_path);
let uuid = Editor.remote.assetdb.urlToUuid(url);
// Editor.log(`FolderName: ${FolderName}`);
// Editor.log(`abs_path: ${abs_path}`);
// Editor.log(`url: ${url}`);
// Editor.log(`uuid: ${uuid}`);
let NewNode = cc.instantiate(Node);
let Spine = NewNode.getChildByName("Spine");
if (!Spine) {
NewNode.addChild(new cc.Node, 0, "Spine");
Spine = NewNode.getChildByName("Spine");
}
let Skeleton = Spine.getComponent(sp.Skeleton);
if (!Skeleton) {
Skeleton = Spine.addComponent(sp.Skeleton);
}
NewNode.active = false;
Parent.addChild(NewNode, 0, name + "");
let _pngcount = function () {
return new Promise((resolve, reject) => {
fs.readdir(file_path, (err, files) => {
if (err) {
Editor.log(`err: ${err}`);
reject(err);
}
let pngcount = 0;
files.forEach(file => {
if (file.indexOf(".png") > -1 && file.indexOf(".meta") === -1) {
pngcount++;
}
});
resolve(pngcount);
});
});
}
let pngcount = await _pngcount();
//TODO : 此处为你的远程资源路径
var image = `${path}\\${FolderName}\\${FolderName}`;
var ske = `${path}\\${FolderName}\\${FolderName}.json`;
var atlas = `${path}\\${FolderName}\\${FolderName}.atlas`;
// Editor.log(`image: ${image}`);
// Editor.log(`image2: ${image2}`);
// let texture = [await this.Load(image), await this.Load(image2)];
// let textureName = [`${FolderName}.png`, `${FolderName}2.png`];
let texture = [];
let textureName = [];
for (let i = 0; i < pngcount; i++) {
let num = i + 1;
texture.push(await this.Load(`${image}${(num > 1 ? num : "")}.png`));
textureName.push(`${FolderName}${(num > 1 ? num : "")}.png`);
// Editor.log(`texture ${i}: ${texture[i]}`);
}
let atlasJson = (await this.Load(atlas)).text;
// Editor.log(`atlasJson: ${atlasJson}`);
let spineJson = (await this.Load(ske)).json;
// Editor.log(`spineJson: ${spineJson}`);
var asset = new sp.SkeletonData();
asset._uuid = uuid;
asset.skeletonJson = spineJson;
asset.atlasText = atlasJson;
asset.textures = texture;
asset.textureNames = textureName;
Skeleton.skeletonData = asset;
// Editor.log(scale);
Skeleton.node.scale = +scale;
if (anim) {
Skeleton.animation = anim;
}
Skeleton._updateSkeletonData();
},
Load(abs_path) {
return new Promise((resolve, reject) => {
// cc.loader.load(abs_path, (error, response) => {
cc.assetManager.loadRemote(abs_path, (error, response) => {
if (error) {
Editor.log(`error: ${error}`);
reject(error);
}
// Editor.log(`abs_path: ${abs_path}`);
// Editor.log(`response: ${response}`);
resolve(response);
});
});
},
/**
* 根據Node去尋找底下每個子節點有沒有符合的UUID
* @param Node 需要搜尋的節點
* @param UUID 目標的UUID
*/
getAllChildByUuid(Node, UUID) {
for (let i = 0; i < Node.childrenCount; i++) {
// Editor.log("Node: " + Node.name, "UUID: " + Node.uuid);
if (Node.uuid == UUID) {
// Editor.log("找到 Node: " + Node.name, "UUID: " + Node.uuid);
return Node;
} else {
// Editor.log("Node: " + Node.children[i].name, "UUID: " + Node.children[i].uuid);
if (Node.children[i].uuid == UUID) {
return Node.children[i];
}
}
if (Node.children[i].childrenCount > 0) {
// Editor.log(Node.children[i].name + " childrenCount > 0");
let getAllChildByUuid = this.getAllChildByUuid(Node.children[i], UUID);
if (getAllChildByUuid) {
return getAllChildByUuid;
}
}
}
// Editor.log("Node: " + Node.children[1].childrenCount);
return null;
},
};