[教程][Headless] 使用 NodeJs 运行无渲染的Cocos项目

性能这些指标有测试过没有,服务器压力大不大

可以了,感谢

没有测过哦,我也就周末才弄出来的,建议运行试试 肯定比没有渲染快吧 然后实战看逻辑复杂不复杂 我感觉还好

mark!

预格式化文本将缩进 4 格    //这里替换HTML URL 地址 须 index.html 结尾

const express = require(“express”);
const app = express();

const runCocos = () => {
const URL = “http://127.0.0.1:3000/static/index.html

const { JSDOM, ResourceLoader } = require('jsdom')
const fetch = (...args) => import('node-fetch').then(({ default: fetch }) => fetch(...args));

const WebGL = require('gl');
const WebGLRenderingContext = WebGL.WebGLRenderingContext;
WebGLRenderingContext.prototype.texSubImage2D = function () { };
const gl = WebGL(64, 64, { preserveDrawingBuffer: true });

const resourceLoader = new ResourceLoader({
    proxy: URL,
    strictSSL: false,
    userAgent: "Mellblomenator/9000",
});

fetch(URL).then(res => res.text()).then(html => {
    console.log("HTML Index 文件: ", html)
    global.window = (new JSDOM(html, {
        url: URL,
        referrer: URL,
        contentType: "text/html",
        resources: resourceLoader,
        storageQuota: 10000000,
        runScripts: "dangerously"
    })).window;

    let HGContext = window.HTMLCanvasElement.prototype.getContext;
    window.TextDecoder = global.TextDecoder;
    window.HTMLCanvasElement.prototype.getContext = function (type, data) {
        if (type == "2d") return HGContext.bind(this)(type, data);
        return gl;
    };
    window.requestAnimationFrame = global.requestAnimationFrame;
    window.fetch = fetch;
})

setTimeout(() => { }, 99999999);

}

app.use(’/static’, express.static(‘public’))
app.listen(3000, () => {
console.log(“服务启动”);
runCocos();
}); 改造了下加载服务端本地静态文件,静态文件cocos creator 打包的产物

2赞

感谢分享,我做了一些调整支持2.x版本,只测试了2.4.11image
红色加粗部分为调整新增的内容:
global.document = window.document;
gl.canvas = document.getElementById(‘GameCanvas’);
image

1赞

谢谢分享(^▽^)

我也测了下2.4.11版本的,但是一直没通过.

期待您能给点提示.

Error: Uncaught [TypeError: Cannot read property ‘RenderComponentHandle’ of undefined]
at reportException (E:\Mine\DocumCocos2\Cocos RunInNodeJs\node_modules\jsdom\lib\jsdom\living\helpers\runtime-script-errors.js:66:24)
at Timeout.task [as _onTimeout] (E:\Mine\DocumCocos2\Cocos RunInNodeJs\node_modules\jsdom\lib\jsdom\browser\Window.js:558:9)
at listOnTimeout (node:internal/timers:557:17)
at processTimers (node:internal/timers:500:7) TypeError: Cannot read property ‘RenderComponentHandle’ of undefined
at Object.initCanvas (http://192.168.90.101/LZZ_Config/Headless/cocos2d-js-min.b1c26.js:1:819387)
at Object._initRenderer (http://192.168.90.101/LZZ_Config/Headless/cocos2d-js-min.b1c26.js:1:290666)
at Object._initEngine (http://192.168.90.101/LZZ_Config/Headless/cocos2d-js-min.b1c26.js:1:285360)
at Object._prepareFinished (http://192.168.90.101/LZZ_Config/Headless/cocos2d-js-min.b1c26.js:1:285514)
at http://192.168.90.101/LZZ_Config/Headless/cocos2d-js-min.b1c26.js:1:286160
at Object._loadPreviewScript (http://192.168.90.101/LZZ_Config/Headless/cocos2d-js-min.b1c26.js:1:285464)
at Object.prepare (http://192.168.90.101/LZZ_Config/Headless/cocos2d-js-min.b1c26.js:1:286127)
at Object.run (http://192.168.90.101/LZZ_Config/Headless/cocos2d-js-min.b1c26.js:1:286242)
at http://192.168.90.101/LZZ_Config/Headless/main.d939b.js:111:35
at Array. (http://192.168.90.101/LZZ_Config/Headless/cocos2d-js-min.b1c26.js:1:338173)

可以给个http地址吗

我没有服务器,在本地测试的.3.8的很顺利就通过了,但是2.4.11不行.
下面是用来测试的文件.Headless.zip (395.1 KB)

index2.4.11.zip (909 字节)

你试试打包不压缩 然后调试模式

好的,谢谢

我的构建选项是这样的:
image
node版本是16.20.2
别的就没什么了,引擎本身没做任何修改

在Headless中 使用 画布 会导致 2D坐标 出现 NaN 解决方法:去除画布中所有的Widget 将 Canvas 的 Align Canavs With Screen 勾选去除

一开始看这个贴子以为是青铜,看了几天,发现原来是个王者

思路真的很不错。就看性能了。

如果你在使用 axios 会发现Nodejs使用axios 响应体一直是 html/text 导致404 解决方法 nodejs 导入 axios 放在window 在Nodejs环境中使用nodejs的axios


大受震撼大受震撼

有解决吗,遇到了同样的问题

大佬,能发下文件吗,用2.4.11会报
Error: Uncaught [TypeError: Cannot read properties of undefined (reading ‘RenderComponentHandle’)]
at reportException (/Users/chris/Downloads/headless/node_modules/_jsdom@22.1.0@jsdom/lib/jsdom/living/helpers/runtime-script-errors.js:66:24)
at Timeout.task [as _onTimeout] (/Users/chris/Downloads/headless/node_modules/_jsdom@22.1.0@jsdom/lib/jsdom/browser/Window.js:558:9)