通过浏览器还原cocos 3.8 场景结构(努力中)

通过浏览器还原cocos 3.8 场景结构(努力中)

let sceneinfo = cc.director.getScene(); // 获取当前场景
let children  = cc.director.getScene().children;//获取当前场景所有节点

let sceneData = []; //全局定义场景存储点


// 定义一个函数来提取场景信息并按照目标格式组织
function extractSceneData(scene) {
    // 场景资产信息
    let sceneAsset = {
        "__type__": "cc.SceneAsset",
        "_name": scene._name,
        "_objFlags": scene._objFlags,
        "__editorExtras__": {},
        "_native": "",
        "scene": {
            "__id__": 1
        }
    };
    sceneData.push(sceneAsset);

    // 场景信息
    let sceneNode = {
        "__type__": "cc.Scene",
        "_name": scene._name,
        "_objFlags": scene._objFlags,
        "__editorExtras__": {},
        "_parent": null,
        "_children": [],
        "_active": scene._active,
        "_components": [],
        "_prefab": null,
        "_lpos": {
            "__type__": "cc.Vec3",
            "x": scene._lpos.x,
            "y": scene._lpos.y,
            "z": scene._lpos.z
        },
        "_lrot": {
            "__type__": "cc.Quat",
            "x": scene._lrot.x,
            "y": scene._lrot.y,
            "z": scene._lrot.z,
            "w": scene._lrot.w
        },
        "_lscale": {
            "__type__": "cc.Vec3",
            "x": scene._lscale.x,
            "y": scene._lscale.y,
            "z": scene._lscale.z
        },
        "_mobility": scene._mobility,
        "_layer": scene._layer,
        "_euler": {
            "__type__": "cc.Vec3",
            "x": scene._euler.x,
            "y": scene._euler.y,
            "z": scene._euler.z
        },
        "autoReleaseAssets": scene.autoReleaseAssets,
        "_globals": {
            "__id__": 8
        },
        "_id": scene._id
    };
    sceneData.push(sceneNode);
    
    //开始处理节点
    
    for (let key in children) {
        console.log('好像发现节点了喔表哥',children[key].name)
        let childrenjan = childrenanalez(children[key]) //进行节点处理
        sceneData.push(childrenjan);
        
    }
    
    return sceneData;
}

//筛选出空姐点
function childrenanalez(data){
    if(data._children.length > 0){
        let childreninfo = data._children
        let parentData = childreninfo._parent ? childreninfo._parent._id : null;
        for(let key in childreninfo){
            let childrenjson = [
                {
                    "__type__": "cc.Node",
                    "_name": childreninfo[key]._name,
                    "_objFlags":childreninfo[key]._objFlags,
                    "__editorExtras__": childreninfo[key].__editorExtras__,
                    "_parent": parentData ? { "__id__": parentData } : null,
                    "_children": formatchildren(childreninfo[key]),
                    "_active": childreninfo[key]._active,
                    "_components": compoentsanalez(childreninfo[key]),
                    "_prefab": null,
                     // 处理 _lpos
                    "_lpos": childreninfo[key]._lpos ? {
                        "__type__": "cc.Vec3",
                        "x": childreninfo[key]._lpos.x,
                        "y": childreninfo[key]._lpos.y,
                        "z": childreninfo[key]._lpos.z
                    } : null,
                    
                    // 处理 _lrot
                    "_lrot": childreninfo._lrot ? {
                        "__type__": "cc.Quat",
                        "x": childreninfo[key]._lrot.x,
                        "y": childreninfo[key]._lrot.y,
                        "z": childreninfo[key]._lrot.z,
                        "w": childreninfo[key]._lrot.w
                    } : null,
                    
                    // 处理 _lscale
                    "_lscale": childreninfo._lscale ? {
                        "__type__": "cc.Vec3",
                        "x": childreninfo[key]._lscale.x,
                        "y": childreninfo[key]._lscale.y,
                        "z": childreninfo[key]._lscale.z
                    } : null,
                    
                    // 处理 _mobility
                    "_mobility": childreninfo[key]._mobility !== undefined ? childreninfo[key]._mobility : null,
                    
                    // 处理 _layer
                    "_layer": childreninfo[key]._layer !== undefined ? childreninfo[key]._layer : null,
                    
                    // 处理 _euler
                    "_euler": childreninfo._euler ? {
                        "__type__": "cc.Vec3",
                        "x": childreninfo[key]._euler.x,
                        "y": childreninfo[key]._euler.y,
                        "z": childreninfo[key]._euler.z
                    } : null,
                    "_id": childreninfo[key]._id  !== undefined ? childreninfo[key]._id : null,
                },
            ]
            sceneData.push(childrenjson);
        }
    
    }else{
        console.log(data._name,'这个节点有点虚')
    }
}

//继续检查空姐点
function formatchildren(data){
    let children = data._children
    if(!children){
        return;
    }
    if(children.length > 0){
        let _childrencat = [];
        console.log('我在给'+ data.name + '做检他有' + data.childrenCount + '节点');
        for(let key in children){
            _childrencat[key] = {"__id__":children[key]._id};
            console.log(children[key])
        }
        return _childrencat;
    }
}

//组件分析
function compoentsanalez(data){
    let compoents = [];
    for(let key in data){
        compoents[key] = {"__id__":  data};
    }
    return compoents;
}



// 提取场景数据
let extractedData = extractSceneData(sceneinfo);

// 将提取的数据转换为 JSON 字符串
let jsonData = JSON.stringify(extractedData, null, 2);
console.log(jsonData)
download(jsonData, '${scene._name}.json');

在浏览器控制直接运行即可

© 版权声明
THE END
喜欢就支持一下吧
点赞10 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容