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);

}
相关推荐
AAA简单玩转程序设计17 小时前
Java进阶小妙招:ArrayList和LinkedList的"相爱相杀"
java
是一碗螺丝粉17 小时前
React Native 运行时深度解析
前端·react native·react.js
Jing_Rainbow17 小时前
【前端三剑客-9 /Lesson17(2025-11-01)】CSS 盒子模型详解:从标准盒模型到怪异(IE)盒模型📦
前端·css·前端框架
lkbhua莱克瓦2417 小时前
集合进阶8——Stream流
java·开发语言·笔记·github·stream流·学习方法·集合
爱泡脚的鸡腿17 小时前
uni-app D6 实战(小兔鲜)
前端·vue.js
青年优品前端团队17 小时前
🚀 不仅是工具库,更是国内前端开发的“瑞士军刀” —— @qnvip/core
前端
北极糊的狐17 小时前
Vue3 中父子组件传参是组件通信的核心场景,需遵循「父传子靠 Props,子传父靠自定义事件」的原则,以下是资料总结
前端·javascript·vue.js
20岁30年经验的码农17 小时前
Java Elasticsearch 实战指南
java·开发语言·elasticsearch
okseekw17 小时前
Java 中的注释与关键字的初步学习
java
luv_sw18 小时前
JavaSE-面向对象-构造器
java