From 12e32827607544f4c8c11e70560c3cc682a54ca6 Mon Sep 17 00:00:00 2001 From: JianMiau Date: Thu, 31 Mar 2022 08:57:26 +0800 Subject: [PATCH] [add] first --- README.md | 60 +++++++++++++++++++++++ main.js | 83 ++++++++++++++++++++++++++++++++ package.json | 21 ++++++++ panel/index.js | 57 ++++++++++++++++++++++ scene-obtain.js | 37 ++++++++++++++ src/main/editor-api.js | 107 +++++++++++++++++++++++++++++++++++++++++ 6 files changed, 365 insertions(+) create mode 100644 README.md create mode 100644 main.js create mode 100644 package.json create mode 100644 panel/index.js create mode 100644 scene-obtain.js create mode 100644 src/main/editor-api.js diff --git a/README.md b/README.md new file mode 100644 index 0000000..6846297 --- /dev/null +++ b/README.md @@ -0,0 +1,60 @@ +# Cocos Creater OutputComponent + +## 插件 安裝方式 + +1. 安裝在 **項目** 底下 +> 把插件資料夾放到項目的packages(跟assets同層) + +2. 安裝在 **全局** 底下 +> 把插件資料夾放到C:\Users\%USERNAME%\.CocosCreator\packages + +## 插件稍微說明(都是搬過來的資料 XD) + +剩下沒寫的可以到參考資料裡面看看😀 + +1. 定義你的包描述文件:**package.json** +> **name** String - 定義了包的名字,包的名字是全局唯一的,他關係到你今後在官網服務器上登錄時的名字。 +> +> **version** String - 版本號,我們推薦使用semver格式管理你的包版本。 +> +> **description** String(可选) - 一句話描述你的包是做什麼的。 +> +> **author** String(可选) - 擴展包的作者 +> +> **main** String (可选) - 入口程序 +> +> **scene-script** String (可选) - 調用引擎API 和項目腳本 +> +> **main-menu** Object (可选) - 主菜單定義 +> +> **有要使用介面的話:** +> +> **panel** Object (可选) - 定義的面板在package裡的描述 +> +> **注意panel的type有兩種:** +> +> dockable:可停靠面板,打開該面板後,可以通過拖拽面板標籤到編輯器裡,實現擴展面板嵌入到編輯器中。下面我們介紹的面板入口程序都是按照可停靠面板的要求聲明的。 +> +> simple:簡單Web面板,不可停靠到編輯器主窗口,相當於一份通用的HTML前端頁面。詳細情況請見定義簡單面板。 +> +> 在simple-package文件夾下面創建一個panel文件夾,然後在panel文件夾下創建一個index.js或者一個html文件都可以 + +2. 入口程序:**main.js** + +3. 定義介面以及按鈕綁定的方法,和主進程的通信:**index.js** + +3. 可以使用包括全部引擎API 和用戶組件腳本里聲明的方法和屬性:**scene-obtain..js** +> 可以在擴展包中獲取到場景裡的Canvas根節點有多少子節點,當然還可以用來對場景節點進行更多的查詢和操作。 + +## 參考資料 + +* 你的第一個擴展包 +> https://docs.cocos.com/creator/manual/zh/extension/your-first-extension.html + +* CocosCreator拓展编辑器 +> https://blog.csdn.net/qq_34772097/category_9577457.html + +* Cocos Creator Editor 編輯器擴展API記錄 +> https://blog.csdn.net/kingbook928/article/details/108659319 +> https://blog.csdn.net/qq_17209641/article/details/106822296 +> https://forum.cocos.org/t/creator-api/92605 \ No newline at end of file diff --git a/main.js b/main.js new file mode 100644 index 0000000..132c510 --- /dev/null +++ b/main.js @@ -0,0 +1,83 @@ +"use strict"; + +const EditorAPI = require('./src/main/editor-api'); + +// ------------decode-uuid +const BASE64_KEYS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='; +const values = new Array(123); // max char code in base64Keys +for (let i = 0; i < 123; ++i) { values[i] = 64; } // fill with placeholder('=') index +for (let i = 0; i < 64; ++i) { values[BASE64_KEYS.charCodeAt(i)] = i; } + +// decoded value indexed by base64 char code +const BASE64_VALUES = values; + +const HexChars = '0123456789abcdef'.split(''); + +const _t = ['', '', '', '']; +const UuidTemplate = _t.concat(_t, '-', _t, '-', _t, '-', _t, '-', _t, _t, _t); +const Indices = UuidTemplate.map((x, i) => x === '-' ? NaN : i).filter(isFinite); + + + +let HexMap = {} +{ + for (let i = 0; i < HexChars.length; i++) { + let char = HexChars[i] + HexMap[char] = i + } +} + +module.exports = { + load() { + this.init(); + }, + unload() { + // Editor.log("卸載執行"); + }, + /** 初始化 */ + init() { + // this.createDirectory(); + }, + /** 創建Spine節點 */ + uuidconvert(...args) { + let self = this; + try { + let needconvertuuid = args[0]; + let uuid; + if (needconvertuuid.indexOf("-") === -1) { + // let convertuuid = this.decodeUuid(needconvertuuid); + let convertuuid = EditorAPI.decompressUuid(needconvertuuid); + Editor.log('convertuuid', convertuuid); + uuid = convertuuid; + } else { + // let convertuuid = this.compressUuid(needconvertuuid); + let convertuuid = EditorAPI.compressUuid(needconvertuuid); + Editor.log('convertuuid', convertuuid); + uuid = needconvertuuid; + } + Editor.Ipc.sendToAll('assets:hint', uuid); + Editor.log('path: ', Editor.assetdb.uuidToUrl(uuid)); + } catch (error) { + Editor.log(`uuidconvert error: ${error}`); + } + }, + + messages: { + /** 打開面板 */ + "open-panel"() { + Editor.Panel.open("uuidconvert"); + }, + /** 保存按鈕點擊 */ + "run-click"(event, ...args) { + this.uuidconvert(...args); + }, + // /** 面板加載完成 */ + // "panel-load-finish"(evnet, ...args) { + // Editor.Scene.callSceneScript("uuidconvert", "get-default-info", { args: args }, function (err, response) { + // // Editor.log("callSceneScript: " + response); + // Editor.Ipc.sendToPanel("uuidconvert", "setDefault", response); + // }); + // }, + }, + +} \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..50160d8 --- /dev/null +++ b/package.json @@ -0,0 +1,21 @@ +{ + "name": "uuidconvert", + "version": "0.0.1", + "description": "Sample-Package", + "author": "Sample", + "main": "main.js", + "scene-script": "scene-obtain.js", + "engineSupport": true, + "main-menu": { + "扩展/Uuid Convert": { + "message": "uuidconvert:open-panel" + } + }, + "panel": { + "main": "panel/index.js", + "type": "dockable", + "title": "Uuid Convert", + "width": 400, + "height": 350 + } +} \ No newline at end of file diff --git a/panel/index.js b/panel/index.js new file mode 100644 index 0000000..30e9b69 --- /dev/null +++ b/panel/index.js @@ -0,0 +1,57 @@ +Editor.Panel.extend({ + style: ` + :host { margin: 5px; } + h2 { color: #f90; } + .bottom { + height: 30px; + } + `, + + template: ` +

Uuid Convert

+
+
+ 1. 填入要轉換的UUID
+
+
+
+ 要轉換的UUID + +
+
+
+ 執行 +
+ `, + + $: { + /** uuid */ + uuid: "#uuid", + /** 生成按鈕 */ + run: "#run", + }, + + ready() { + // Editor.Ipc.sendToMain("uuidconvert:panel-load-finish"); + this.onClickRun(); + + }, + /** 保存按鈕點擊事件 */ + onClickRun() { + this.$run.addEventListener("confirm", () => { + if (!this.$uuid._value) { + Editor.error("請輸入要轉換的UUID"); + return; + } + Editor.Ipc.sendToMain("uuidconvert:run-click", this.$uuid._value); + }); + }, + messages: { + // "setDefault": function (event, ...agrs) { + // if (event.reply) { + // //if no error, the first argument should be null + // event.reply(null, "Fine, thank you!"); + // } + // } + } +}); \ No newline at end of file diff --git a/scene-obtain.js b/scene-obtain.js new file mode 100644 index 0000000..76b0b4c --- /dev/null +++ b/scene-obtain.js @@ -0,0 +1,37 @@ +"use strict"; +let fs = require("fs"); +let path = require("path"); + +module.exports = { + /** + * 獲取場景節點下的配置信息 + * @param event event + * @param data 這邊把panel的Node帶進來 + */ + "getFsPath": function (event, request) { + let self = this; + var response = null; + var args = request.args; + try { + let url = `db://assets/${args[0]._value}`; + url = url.replace(/\\/g, "/"); + // let url = `db://assets/=SceneLobby`; + // Editor.log(`url: ${url}`); + let fsPath = Editor.remote.assetdb.urlToFspath(url); + // Editor.log(`fsPath: ${fsPath}`); + + // this.fileSearch(fsPath); + + let res = { + url: url, + fsPath, fsPath + } + response = JSON.stringify(res); + } catch (error) { + Editor.log(`getFsPath error: ${error}`); + } + if (event.reply) { + event.reply(null, response); + } + }, +}; \ No newline at end of file diff --git a/src/main/editor-api.js b/src/main/editor-api.js new file mode 100644 index 0000000..d2ec8e2 --- /dev/null +++ b/src/main/editor-api.js @@ -0,0 +1,107 @@ +/** + * 编辑器 API(用于抹平不同版本编辑器之间的差异) + * @author 陈皮皮 (ifaswind) + * @version 20210830 + */ +const EditorAPI = { + + /** + * 当前语言 + * @returns {string} + */ + getLanguage() { + return Editor.lang || Editor.I18n.getLanguage(); + }, + + /** + * 绝对路径转为编辑器资源路径 + * @param {string} fspath + */ + fspathToUrl(fspath) { + return Editor.assetdb.fspathToUrl(fspath); + }, + + /** + * 编辑器资源路径转为绝对路径 + * @param {string} url + */ + urlToFspath(url) { + return Editor.assetdb.urlToFspath(url); + }, + + /** + * 通过 uuid 获取资源信息 + * @param {string} uuid + */ + assetInfoByUuid(uuid) { + return Editor.assetdb.assetInfoByUuid(uuid); + }, + + /** + * 通过 uuid 获取子资源信息 + * @param {string} uuid + */ + subAssetInfosByUuid(uuid) { + return Editor.assetdb.subAssetInfosByUuid(uuid); + }, + + /** + * 获取当前选中的资源 uuid + * @returns {string[]} + */ + getCurrentSelectedAssets() { + return Editor.Selection.curSelection('asset'); + }, + + /** + * 获取当前选中的节点 uuid + * @returns {string[]} + */ + getCurrentSelectedNodes() { + return Editor.Selection.curSelection('node'); + }, + + /** + * 是否为 uuid + * @param {string} uuid + */ + isUuid(uuid) { + return Editor.Utils.UuidUtils.isUuid(uuid); + }, + + /** + * 压缩 uuid + * @param {string} uuid + */ + compressUuid(uuid) { + return Editor.Utils.UuidUtils.compressUuid(uuid); + }, + + /** + * 反压缩 uuid + * @param {string} uuid + */ + decompressUuid(uuid) { + return Editor.Utils.UuidUtils.decompressUuid(uuid); + }, + + /** + * 获取uuid资源所在bundle的目录 + * @param {string} uuid + * @returns {string} + */ + uuid2path(uuid) { + return uuid.substr(0, 2) + "/" + uuid; + }, + + /** + * 获取uuid对应的资源所在的文件夹 + * @param {string} uuid + * @returns {string} + */ + uuiddir(uuid) { + return uuid.substr(0, 2) + }, +}; + +module.exports = EditorAPI;