Cocos Creator 動態設置Canvas的寬度與高度,更改適配

let c = this.node.getComponent(cc.Canvas);
c.fitHeight = true;
c.fitWidth = false;node

let h = 960 * cc.winSize.height / cc.winSize.width;canvas

c.designResolution = new cc.Size(960, h);
this.node.setContentSize(960, h);this

 

 

// 適配解決方案
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.主域和子域的設計分辨率和適配方案要相同get

2.sharedCanvas 不要設置寬高it

3.承載sharedCanvas 的sprite要根據實際分辨率動態調整io

相關文章
相關標籤/搜索