Cocos Creator 动态设置Canvas的宽度与高度,更改适配
2018-12-04 11:15 罗任德 阅读(13829) 评论(0) 编辑 收藏 举报let c = this.node.getComponent(cc.Canvas);
c.fitHeight = true;
c.fitWidth = false;
let h = 960 * cc.winSize.height / cc.winSize.width;
c.designResolution = new cc.Size(960, h);
this.node.setContentSize(960, h);
// 适配解决方案
let _canvas = cc.Canvas.instance;
// 设计分辨率比
let _rateR = _canvas.designResolution.height/_canvas.designResolution.width;
// 显示分辨率比
let _rateV = cc.winSize.height/cc.winSize.width;
console.log("winSize: rateR: "+_rateR+" rateV: "+_rateV);
if (_rateV > _rateR)
{
_canvas.fitHeight = false;
_canvas.fitWidth = true;
console.log("winSize: fitWidth");
}
else
{
_canvas.fitHeight = true;
_canvas.fitWidth = false;
console.log("winSize: fitHeight");
}
三点注意事项:
1.主域和子域的设计分辨率和适配方案要相同
2.sharedCanvas 不要设置宽高
3.承载sharedCanvas 的sprite要根据实际分辨率动态调整