有没有新版creator3.6.2的截图方法分享下,谢谢

有没有新版creator3.6.2的截图方法分享下,谢谢

有没有人写过3.x的截屏啊,发现2.x的代码又不能用了,头疼

camera.render();

这个函数在3.x没了,最烦就是每次升级版本函数又不能用了,头疼

搞不定,不知道要不要降低版本回2.4.10了。。。。

对应功能修改即可

修改就是删除憋 :sweat_smile: :sweat_smile: :sweat_smile: :sweat_smile: :sweat_smile:

对应api功能

https://gitee.com/yeshao2069/CocosCreatorDemos/tree/v3.6.x/demo/2d/Creator3.6.0_2D_DrawingBoard

1赞

里面好多例子,但是好像没找到截屏的

算了3.x水太深,我还把握不住,用回2.4.10,等3.x在成熟点再说

export function screenshot({
x, y, w, h, rt
}: {
x?: number,
y?: number,
w?: number, // width 需要整数!
h?: number, // height 需要整数!
rt?: boolean, // 是否翻转图片(截图默认是反过来的,翻转会比较慢 可以改用node.scaleY = -1
} = {}) {
return new Promise(async function (resolve) {
const captureCamera = find(‘CaptureCanvas/CaptureCamera’).getComponent(Camera);

    const viewSize = view.getVisibleSize();

    const s = size(
        Math.ceil(w || viewSize.width),
        Math.ceil(h || viewSize.height),
    );

    const sf = new SpriteFrame();
    const renderTexture = new RenderTexture();

    renderTexture.reset({
        width: viewSize.width,
        height: viewSize.height,
    });
    captureCamera.targetTexture = renderTexture;

    const {width, height} = s;

    await new Promise(function (r) {
        director.once(Director.EVENT_AFTER_DRAW, r);
    });

    var buffer = renderTexture.readPixels(x || 0, y || 0, width, height);

    var rtBuffer = buffer;
    if (rt) {

        rtBuffer = new Uint8Array(width * height * 4);
        for (var i = height - 1; i >= 0; i--) {
            for (var j = 0; j < width; j++) {
                rtBuffer[((height - 1 - i) * (width) + j) * 4 + 0] = buffer[(i * width + j) * 4 + 0];
                rtBuffer[((height - 1 - i) * (width) + j) * 4 + 1] = buffer[(i * width + j) * 4 + 1];
                rtBuffer[((height - 1 - i) * (width) + j) * 4 + 2] = buffer[(i * width + j) * 4 + 2];
                rtBuffer[((height - 1 - i) * (width) + j) * 4 + 3] = buffer[(i * width + j) * 4 + 3];
            }
        }
    }

    const image = new ImageAsset({
        _data: rtBuffer,
        _compressed: false,
        width,
        height,
        format: Texture2D.PixelFormat.RGBA8888,
    });
    const t = new Texture2D();
    t.image = image;

    sf.texture = t;

    captureCamera.targetTexture = null;

    resolve(sf);
});

}

CaptureCanvas/CaptureCamera这个路径你自己新建个摄像机修改一下 我有特殊需求 所以写在场景里了
这个函数使用的时候不能连续调用 否则需要做队列

大佬NB!

大佬牛逼!

大佬牛逼!