天天热点评!截图!长按保存分享!Cocos Creator
背景一种3D截图方案
(资料图片仅供参考)
参考 TRUE SPACE with Cocos 技术分享[1]中的动态生成海报的介绍,以及作者的支持。实现一个长按保存分享截图的方案。
效果在微信浏览器中预览效果如下
环境Cocos Creator 3.7.1Web 浏览器原理原作者讲的非常清楚,只需要按照以下步骤实现即可。
3D相机生成一个RT 赋予给2D精灵摆上一些二维码等UIUI相机再生成一张RT读取RT中的数据,传给 canvascanvas 生成图片数据传给 Image适配Image对象大小位置视频录了一段操作视频[2]供大家参考。
代码这段代码的截图功能是按照高度适配的方式写的,如有其他需求,可自行修改相关逻辑。仅供大家参考学习。
import{_decorator,Component,Node,Camera,RenderTexture,view,UITransform,log,game,screen,NodeEventType}from"cc";const{ccclass,property}=_decorator;@ccclass("CaptureImage")exportclassCaptureImageextendsComponent{@property(Camera)copyCamera:Camera=null!;@property(Node)targetNode:Node=null!;@property(Node)captureBtn:Node=null!;@property(Node)closeBtn:Node=null!;rt:RenderTextureprivate_image:HTMLImageElement_canvas:HTMLCanvasElement=null!;_buffer:ArrayBufferView=null!;start(){log("欢迎关注微信公众号【白玉无冰】https://mp.weixin.qq.com/s/4WwCjWBtZNnONh8hZ7JVDA")this.rt=newRenderTexture();this.rt.reset({width:view.getVisibleSize().width,height:view.getVisibleSize().height,})this.copyCamera.targetTexture=this.rt;this.captureBtn.active=true;this.closeBtn.active=false;this.captureBtn.on(NodeEventType.TOUCH_END,this.copyRenderTex,this)this.closeBtn.on(NodeEventType.TOUCH_END,this.clearCapture,this)}privatecopyRenderTex(){constwidth=this.targetNode.getComponent(UITransform).width;constheight=this.targetNode.getComponent(UITransform).height;constanchorPoint=this.targetNode.getComponent(UITransform).anchorPoint;constworldPos=this.targetNode.getWorldPosition();this._buffer=this.rt.readPixels(Math.round(worldPos.x-width*anchorPoint.x),Math.round(worldPos.y-height*anchorPoint.y),width,height);if(!this._canvas){this._canvas=document.createElement("canvas");this._canvas.width=width;this._canvas.height=height;}else{letctx=this._canvas.getContext("2d");ctx.clearRect(0,0,this._canvas.width,this._canvas.height);}letctx=this._canvas.getContext("2d")!;letrowBytes=width*4;for(letrow=0;row其他 最近写文偏笔记的形式,主要是记录一下代码,下次要用的时候再翻出来。希望对大家有所帮助。
参考资料[1]
TRUE SPACE with Cocos 技术分享:https://forum.cocos.org/t/topic/146459
[2]视频:https://www.bilibili.com/video/BV1w84y1N7XQ
往期精彩:
3DUI Cocos Creator
三渲二 Cocos Creator
零代码实现面片效果(UV滚动,帧动画) Cocos Creator
游戏开发资料合集,2022年版
点击“阅读原文”查看精选导航
“点赞“ ”在看”鼓励一下▼
X 关闭
- 1、天天热点评!截图!长按保存分享!Cocos Creator
- 2、当前资讯!若羽臣2022年财报:营收12.17亿元,扣非净利同比增长64.73%
- 3、焦点简讯:FGO泳装巴御前技能宝具介绍(FGO泳装巴御前技能介绍)
- 4、衣服上的褶子怎么处理 衣服上的褶子怎么弄平|天天报道
- 5、“五一”假期来临!这份铁路出行提示条条需牢记! 天天速看料
- 6、环球热讯:新鲜的椰子怎么打开 新鲜的椰子的打开方法
- 7、“旅游+”为留住“乡愁”注入新动能-天天最资讯
- 8、赫塔费官方:球队体育总监鲁文-雷耶斯出任球队主教练
- 9、劳动者之歌丨孙晓霞:为铸“重器”勤“续火”_全球热资讯
- 10、全球热消息:云南昆明蓝花楹文化艺术节启动
-
【全球新视野】iPhone 15高清图出炉:USB-C敲定
2023-04-29
-
深中通道伶仃洋大桥合龙
2023-04-29
-
地铁1、3号线将延时运营
2023-04-29
-
聚焦三次产业“三驾马车”加大加劲推动海口经济高质量发展 | 海口启动国际消费年全年促消费活动
2023-04-29
-
美股三大股指均收涨 热门科技股多数上涨
2023-04-29