《creator插件开发常用API命令列表》备忘录

**

原文地址

**

【资源管理器API整理】
官方API文档:
官方文件管理API: 移动、写入、重命名、创建、刷新、获得文件信息…
官方文件管理remote API: 移动、写入、重命名、创建、刷新、获得文件信息、uuid转url、uuid转绝对路径、url转uuid…
官方文件选中状态API: 获得选中的资源、设置选中状态、取消选中、获得选中的是资源还是节点…
参考文档:管理项目资源
参考文档:常用 IPC 消息

官方常用API例子,粘贴搬砖专用:
// 打印文件信息

Editor.assetdb.queryInfoByUuid(uuid, function (err, info) { // info.path// info.url // info.type});
//移动、重命名文件
Editor.assetdb.move('db://assets/foo/bar/foobar.js', 'db://assets/foo/bar/foobar02.js');
//创建或写入文件
Editor.assetdb.createOrSave('db://assets/foo/bar/foobar.js', 'var foobar = 0;');
//删除文件
Editor.assetdb.delete(['db://assets/foo/bar/foobar.js','db://assets/foo/bar/foobar02.js',]);
//uuid转url
Editor.remote.assetdb.uuidToUrl(file_uuid);
//绝对路径转url
Editor.remote.assetdb.fspathToUrl(abs_path);
//url转uuid
Editor.remote.assetdb.urlToUuid(url);
// 编译uuid
//Editor.remote.UuidUtils.compressUuid(uuid)
// 解uuid
//Editor.remote.UuidUtils.decompressUuid(uuid)
// 生成新uuid
Editor.Utils.UuidUtils.decompressUuid( Editor.Utils.UuidUtils.uuid() )
// 清除选中
Editor.Selection.clear('asset')
// 选中资源
Editor.Selection.select('asset', uuid)
// 获得选中资源们
Editor.Selection.curSelection("asset")
// 检测面板焦点在资源管理器还是层级管理器
let activeInfo = Editor.Selection.curGlobalActivate()
if (activeInfo && activeInfo.type == "node"){
}
else (activeInfo && activeInfo.type == "asset"){
}

API事件名
“assets:copy”
“assets:paste”
“assets:hint” // 在资源管理器高亮该资源,例子: Editor.Ipc.sendToAll(‘assets:hint’, file_uuid)
“assets:search”
“assets:clearSearch”
“assets:new-asset”
“assets:find-usages”
“assets:rename”
“assets:delete”
“assets:start-refresh”
“assets:end-refresh”
“assets:popup-context-menu” // 鼠标右击资源管理菜单
“assets:open-text-file” // 通过外部编辑器打开代码文件,例子: Editor.Ipc.sendToMain(‘assets:open-text-file’,file_uuid);
“selection:selected”
“selection:unselected”
“selection:activated”
“selection:deactivated”
“selection:hoverin”
“selection:hoverout”

可监听的事件名:
“asset-db:assets-created” // 文件创建
“asset-db:assets-moved” //文件移动
“asset-db:asset-changed” // 文件改变
“asset-db:assets-deleted” // 文件删除
“asset-db:state-changed” // 文件状态改变
“asset-db:asset-uuid-changed”

【场景编辑器API整理】
官方API文档:
官方文件选中状态API: 获得选中的资源、设置选中状态、取消选中、获得选中的是资源还是节点…
参考文档:管理项目资源
参考文档:常用 IPC 消息

官方常用API例子,粘贴搬砖专用:

// 清除选中
Editor.Selection.clear('node')
// 选中资源
Editor.Selection.select('node', uuid)
// 获得选中资源们
Editor.Selection.curSelection("node")

//创建一个空节点:
Editor.Ipc.sendToPanel('scene', 'scene:create-node-by-classid', 'New Node', '', 'parentUuid');
//添加一个组件:
Editor.Ipc.sendToPanel('scene', 'scene:add-component', nodeID, 'cc.Animation');
//删除一个组件:
Editor.Ipc.sendToPanel('scene', 'scene:remove-component', nodeID, compID);
//复制节点
Editor.Ipc.sendToPanel('scene', 'scene:copy-nodes', uuids);
//粘贴节点:
Editor.Ipc.sendToPanel('scene', 'scene:paste-nodes', parentID);
// 插入个预制节点
Editor.Ipc.sendToPanel("scene","scene:create-nodes-by-uuids",[parfab_uuid],parentUuid,{unlinkPrefab:null})

// 修改运行中的节点属性:
Editor.Ipc.sendToPanel('scene', 'scene:set-property',{
id: info.args.uuid,
path: "name",//要修改的属性
type: "String",
value: info.args.name,
isSubProp: false,
});

// 修改精灵纹理
Editor.Ipc.sendToPanel('scene', 'scene:set-property',{
id: compObj.uuid,
path: "spriteFrame",//要修改的属性
type: "cc.SpriteFrame",
value: {uuid:spriteFrameUuid},
isSubProp: false,
});

API事件名
“scene:is-ready”
“scene:new-scene”// 新建场景
“scene:saved”// 场景保存
“scene:play-on-device”
“scene:reload-on-device”
“scene:preview-server-scene-stashed”
“scene:load-package-scene-script”
“scene:unload-package-scene-script”
“scene:stash-and-reload”
“scene:soft-reload”
“scene:enter-prefab-edit-mode” // 预制节点加载完,回调里可以获得预制节点文件uuid
“scene:stash-and-save”
“scene:print-simulator-log”
“scene:generate-texture-packer-preview-files”
“scene:query-texture-packer-preview-files”
“scene:export-particle-plist”
“scene:undo” //撤销重置
“scene:redo” //重置
“scene:undo-record”
“scene:undo-commit”
“scene:undo-cancel”
“scene:query-dirty-state”
“scene:query-group-list”
“scene:query-hierarchy”
“scene:query-nodes-by-comp-name”
“scene:query-node”
“scene:query-node-info”
“scene:query-node-functions”
“scene:choose-last-rigid-body”
“scene:choose-next-rigid-body”
“scene:is-child-class-of”
“scene:has-copied-component”
“scene:query-animation-hierarchy”
“scene:query-animation-list”
“scene:query-animation-properties”
“scene:query-animation-record”
“scene:query-animation-clip”
“scene:create-nodes-by-uuids” //通过uuid创建node
“scene:create-node-by-classid”
“scene:create-node-by-prefab” //通过uuid创建node
“scene:new-property”
“scene:reset-property”
“scene:set-property”
“scene:add-component”
“scene:remove-component”
“scene:reset-node”
“scene:reset-all”
“scene:move-up-component”
“scene:move-down-component”
“scene:reset-component”
“scene:copy-component”
“scene:paste-component”
“scene:move-nodes” //移动node
“scene:delete-nodes”//删除node
“scene:copy-nodes”
“scene:paste-nodes”
“scene:duplicate-nodes”
“scene:create-prefab”//创建预制节点
“scene:apply-prefab”
“scene:revert-prefab”
“scene:set-prefab-sync”
“scene:break-prefab-instance”
“scene:link-prefab”
“scene:regenerate-polygon-points”
“scene:change-node-lock”
“scene:query-animation-time”
“scene:animation-time-changed”
“scene:animation-clip-changed”
“scene:save-clip”
“scene:set-animation-speed”
“scene:change-animation-record”
“scene:mount-clip”
“scene:change-animation-state”
“scene:change-animation-current-clip”
“scene:center-nodes”
“scene:ready”// 场景加载完
“scene:reloading”
“scene:animation-record-changed”
“scene:animation-state-changed”
“scene:node-component-added”
“scene:node-component-removed”
“scene:node-component-updated”

事件来源追踪
// 面板通讯发送口
C:\CocosCreator\resources\electron.asar\renderer\api\ipc-renderer.js
在 ipcRenderer.send 函数设置断点查看面板通讯发送的信息与事件触发来源

事件终点追踪
// 面板通讯接收口
/events.js
在 EventEmitter.prototype.emit 函数设置断点查看面板通讯接收信息与分发位置

事件通讯日志打印(必备)
// 用于想知道某个动作会触发什么样的事件时
// 在控制台输入以下这段代码重写通讯函数打印通讯日志: 事件名、参数格式、
func = Editor.Ipc.sendToPanel
Editor.Ipc.sendToPanel = (n,r,…i)=>{console.log(n,r,…i); return func(n,r,…i)}

编辑器自带面板api调用
非公开的东西仅仅开发自己用的插件
//修改场景编辑模式 2.0后面不支持了
panel = document.getElementById(“tools”)
panel.transformTool = “move” //其它的选项 move ,rotate ,scale ,rect

//场景渲染层
mm = document.getElementById(“scene”)
mm._newScene()
mm._onAlignTop()//顶对齐

//属性面板
mm = document.getElementById(“inspector”)
mm._clear() // 清空面板显示

//当前局域网的地址(127.0.0.1:7456)(2.0以上的版本)
document.getElementById(“toolbar”).__ vue __.$data.url

//当前局域网的地址(127.0.0.1:7456)(2.0以前的版本)
document.getElementById(“playButtons”).dataHost.previewURL;

事件列表
“editor:panel-run”
“editor:panel-unload”
“editor:panel-out-of-date”
“editor:ipc-main2panel”
“editor:ipc-main2renderer”
“editor:ipc-reply”
“editor:window-inspect”
“editor:dragstart”
“editor:dragend”
“editor:reset-layout”
[email protected]:main2renderer”
[email protected]:reply”
“electron-profile:changed”
“editor:query-ipc-events”
“_selection:selected”
“_selection:unselected”
“_selection:activated”
“_selection:deactivated”
“_selection:hoverin”
“_selection:hoverout”
“_selection:context”
“_selection:patch”
“app:global-step-changed”
“@base/[email protected]:broadcast”
“@base/[email protected]:send-reply”
“@base/[email protected]:send”
“asset-db:assets-moved”
“asset-db:asset-changed”
“asset-db:assets-deleted”
“asset-db:state-changed”
“asset-db:watch-state-changed”
“compiler:state-changed”
“editor:console-failed”
“editor:console-warn”
“editor:console-error”
“editor:console-clear”
“preview-server:connects-changed”
“editor:ready”
“profile:local-ip”
“assets:copy”
“assets:paste”
“selection:selected”
“selection:unselected”
“selection:activated”
“selection:deactivated”
“asset-db:assets-created”
“asset-db:asset-uuid-changed”
“assets:hint”
“assets:search”
“assets:clearSearch”
“assets:new-asset”
“assets:find-usages”
“assets:rename”
“assets:delete”
“assets:start-refresh”
“assets:end-refresh”
“editor:panel-undock”
“editor:project-profile-updated”
“scene:is-ready”
“scene:new-scene”
“scene:saved”
“scene:play-on-device”
“scene:reload-on-device”
“scene:preview-server-scene-stashed”
“scene:load-package-scene-script”
“scene:unload-package-scene-script”
“scene:stash-and-reload”
“scene:soft-reload”
“scene:enter-prefab-edit-mode”
“scene:stash-and-save”
“scene:print-simulator-log”
“scene:generate-texture-packer-preview-files”
“scene:query-texture-packer-preview-files”
“scene:export-particle-plist”
“scene:undo”
“scene:redo”
“scene:undo-record”
“scene:undo-commit”
“scene:undo-cancel”
“scene:query-dirty-state”
“scene:query-group-list”
“scene:query-hierarchy”
“scene:query-nodes-by-comp-name”
“scene:query-node”
“scene:query-node-info”
“scene:query-node-functions”
“scene:choose-last-rigid-body”
“scene:choose-next-rigid-body”
“scene:is-child-class-of”
“scene:has-copied-component”
“scene:query-animation-hierarchy”
“scene:query-animation-list”
“scene:query-animation-properties”
“scene:query-animation-record”
“scene:query-animation-clip”
“scene:create-nodes-by-uuids”
“scene:create-node-by-classid”
“scene:create-node-by-prefab”
“scene:new-property”
“scene:reset-property”
“scene:set-property”
“scene:add-component”
“scene:remove-component”
“scene:reset-node”
“scene:reset-all”
“scene:move-up-component”
“scene:move-down-component”
“scene:reset-component”
“scene:copy-component”
“scene:paste-component”
“scene:move-nodes”
“scene:delete-nodes”
“scene:copy-nodes”
“scene:paste-nodes”
“scene:duplicate-nodes”
“scene:create-prefab”
“scene:apply-prefab”
“scene:revert-prefab”
“scene:set-prefab-sync”
“scene:break-prefab-instance”
“scene:link-prefab”
“scene:regenerate-polygon-points”
“scene:change-node-lock”
“scene:query-animation-time”
“scene:animation-time-changed”
“scene:animation-clip-changed”
“scene:save-clip”
“scene:set-animation-speed”
“scene:change-animation-record”
“scene:mount-clip”
“scene:change-animation-state”
“scene:change-animation-current-clip”
“scene:center-nodes”
“selection:hoverin”
“selection:hoverout”
“scene:ready”
“scene:reloading”
“scene:animation-record-changed”
“change-filter”
“delete”
“rename”
“show-path”
“duplicate”
“filter”
“hint”
“hierarchy:hint”
“editor:record-node-changed”
“scene:animation-state-changed”
“scene:node-component-added”
“scene:node-component-removed”
“scene:node-component-updated”
“timeline:property-add”
“timeline:property-remove”
“timeline:property-add-key”
“timeline:property-delete-selected-key”
“timeline:property-clear”
“timeline:edit-event”
“timeline:delete-event”
“timeline:clear-node”
“timeline:rename-node”
“simple-code:customCmd”
“node-library:delete-prefab”
“node-library:rename-prefab”
“node-library:set-prefab-icon”
“onLoad”
“onSearchAccept”
“searchCmd”
“onDestroy”
“editor:console-log”
“editor:console-success”
“editor:console-info”
“console:query-last-error-log”
“editor:console-on-device-play”
事件参数格式获取:
在 Editor.Ipc.sendToPanel/ Editor.Ipc.sendToMain/ Editor.Ipc.sendToAll
这些函数入口打上条件断点查看发送的数据格式
或者将creaotr编辑器源码全部下载后格式化后用编辑器全局搜索相关命令跟踪实现原理

以上全都是之前懵懵懂懂被坑无数次之后走出的一条插件开发必经之道路

你可能感兴趣的:(cocos,code)