cocos2D插件转3D插件

cocos2D插件转3D插件

javascript 复制代码
'use strict';

/**

 * 3d插件api映射,兼容2d插件

 * */



let fs = require("fs");

let path = require("path");

let baseDir = '';

const prsPath = (Editor.Project && Editor.Project.path ? Editor.Project.path : Editor.remote.projectPath).replace(/\\/g,'/');



// 2D 映射到 3D编辑器的命令

let MAP_CMD = {

    'scene:enter-prefab-edit-mode'  :'scene:open-scene',

    'scene:open-by-uuid'            :'scene:open-scene',

    'assets:hint'                   : "twinkle", // 3d里高亮资源命令是 'twinkle'

    'hint'                          : "hint"           // 高亮选中的节点,未找到映射关系

}



// 模拟creator2d插件的API实现

let Editor2D =

{

    isWin32 : path.sep == '\\',

    appPath : Editor.App.path,

   

    error : console.error,

    log : console.log,

    warn : console.warn,

    info : console.info,

   

    _getUrlLast(url_path,head){

        let p_i = url_path.indexOf(head);

        if(p_i!=-1){

            return url_path.substr(head.length+2);

        }

    },



    url(url_path){

        let absPath = Editor._getUrlLast(url_path,'packages:');

        if(absPath){

            return path.join(baseDir,absPath)

        }



        absPath = Editor._getUrlLast(url_path,'db:');

        if(absPath ) {

            return path.join(prsPath,absPath)

        }

        return url_path;

    },



    require(url){

        url = Editor2D.url(url);

        return require(url);

    },



    Ipc : {



        sendToPanel: (head,cmd,...args)=>Editor2D.Ipc._send(head,cmd,...args),



        sendToAll:(cmd,...args)=>Editor2D.Ipc.sendToMain(cmd,...args),



        sendToMain:(cmd,...args)=>

        {

            cmd = MAP_CMD[cmd] || cmd;

            let temp = cmd.split(':')

            if(temp[1]){

                Editor2D.Ipc._send(temp[0],temp[1] || "",...args);

            }else{

                Editor.Message.send(cmd,...args);

            }

        },



        _send(head,cmd,...args)

        {

            let callback = arguments[arguments.length-1]

            Editor.Message.request(head,cmd,...args).then((v)=>{

                if(typeof callback == 'function'){

                    callback(null,v);

                    callback = null;

                }

            },()=>{

                if(typeof callback == 'function'){

                    callback('run _send error');

                    callback = null;

                }

            })

        }

    },



    Scene :{

        callSceneScript:(head,cmd,...args)=>{

            let info = {

                name: 'scene',

                method: cmd,

                args:args,

            }

            let callback = arguments[arguments.length-1]

            Editor2D.Ipc._send('scene','execute-scene-script',info,(callback instanceof Function) ? callback : null);

        },

    },

   

    assetdb:{

        assetBackupPath : path.join(prsPath,'temp','BackupAssets'),



        async urlToUuid(url){

            return await Editor.Message.request("asset-db",'query-uuid',url);

        },



        async uuidToUrl(uuid){

            return await Editor.Message.request("asset-db",'query-url',uuid);

        },



        async urlToFspath(uuidOrUrl){

            return await Editor.Message.request("asset-db",'query-path',uuidOrUrl);

        },



        async uuidToFspath(uuidOrUrl){

            return await Editor.Message.request("asset-db",'query-path',uuidOrUrl);

        },



        async fspathToUuid(fsPath){

            let url = "db://" + fsPath.replace(/\\/g,'/').replace(prsPath,'').substr(6);

            return await Editor.Message.request("asset-db",'query-uuid',url);

        },



        async existsByUuid(urlOrUUID){

            return await Editor.Message.request("asset-db",'query-asset-info',urlOrUUID);

        },

       

        async existsByUrl(urlOrUUID){

            return await Editor.Message.request("asset-db",'query-asset-info',urlOrUUID);

        },



        async assetInfoByUuid(urlOrUUID){

            return await Editor.Message.request("asset-db",'query-asset-info',urlOrUUID); // 注意3d返回字段与2d不太一样!

        },



        async assetInfoByUrl(urlOrUUID){

            return await Editor.Message.request("asset-db",'query-asset-info',urlOrUUID); // 注意3d返回字段与2d不太一样!

        },



        deepQuery(callback){

            if(!callback){

                return

            }

            Editor.Message.request("asset-db",'query-assets',{pattern:"db://**"}).then((list)=>{

                // 注意3d返回字段与2d不太一样!

                callback(null,list)

            },()=>{

                callback("run deepQuery error")

            });

        },



        queryInfoByUrl(...args){Editor2D.assetdb.queryInfoByUuid(...args)},

        queryInfoByUuid(urlOrUUID,callback){

            Editor.Message.request("asset-db",'query-asset-info',urlOrUUID).then((list)=>{

                // 注意3d返回字段与2d不太一样!

                callback(null,list)

            },()=>{

                callback("run queryInfoByUuid error")

            });

        },




        saveExists(url,text,callback){

            let promise = Editor.Message.request("asset-db",'save-asset',url,text).then(()=>{

                if(callback) callback()

            },()=>{

                if(callback) callback('save error')

            });

        },



        create(url,text,callback){

            let promise = Editor.Message.request("asset-db",'create-asset',url,text,{}).then((info)=>{

                if(callback) callback(null,info)

            },()=>{

                if(callback) callback('save error')

            });

        },



        delete(urls){

            for (let i = 0; i < urls.length; i++) {

                const url = urls[i];

                Editor.Message.request("asset-db",'delete-asset',url)

            }

        },

       

        move(source,target){

            Editor.Message.request("asset-db",'move-asset',source,target)

        },

    },



   

    Selection:{

        curGlobalActivate(){

            let type = Editor.Selection.getLastSelectedType();

            let ids = Editor.Selection.getSelected(type);

            return {type,id:ids[0]};

        },



        curSelection(type){

            return Editor.Selection.getSelected(type);

        },

    }

}



module.exports.analogApi = ()=>

{

    let packageRoot = __dirname.replace(/\\/g,'/')

    packageRoot = packageRoot.substr(0,packageRoot.lastIndexOf('simple-code/')-1)

    baseDir = packageRoot;



    // 插入api

    let copyFunc = (s_obj,t_obj)=>

    {

        for (const key in s_obj)

        {

            const v = s_obj[key];

            if(t_obj[key] == null){

                t_obj[key] = v;

            }else if(t_obj[key] instanceof Object){

                copyFunc(v,t_obj[key]);

            }

        }

    }



    copyFunc(Editor2D,Editor);

}
相关推荐
2202_754421544 分钟前
生成MPSOC以及ZYNQ的启动文件BOOT.BIN的小软件
java·linux·开发语言
蓝染-惣右介7 分钟前
【MyBatisPlus·最新教程】包含多个改造案例,常用注解、条件构造器、代码生成、静态工具、类型处理器、分页插件、自动填充字段
java·数据库·tomcat·mybatis
小林想被监督学习8 分钟前
idea怎么打开两个窗口,运行两个项目
java·ide·intellij-idea
HoneyMoose9 分钟前
IDEA 2024.3 版本更新主要功能介绍
java·ide·intellij-idea
我只会发热11 分钟前
Java SE 与 Java EE:基础与进阶的探索之旅
java·开发语言·java-ee
是老余12 分钟前
本地可运行,jar包运行错误【解决实例】:通过IDEA的maven package打包多模块项目
java·maven·intellij-idea·jar
crazy_wsp13 分钟前
IDEA怎么定位java类所用maven依赖版本及引用位置
java·maven·intellij-idea
.Ayang15 分钟前
tomcat 后台部署 war 包 getshell
java·计算机网络·安全·web安全·网络安全·tomcat·网络攻击模型
醉の虾16 分钟前
Vue3 使用v-for 渲染列表数据后更新
前端·javascript·vue.js
一直学习永不止步21 分钟前
LeetCode题练习与总结:最长回文串--409
java·数据结构·算法·leetcode·字符串·贪心·哈希表