cc.Canvas 在代码中设置宽高无效,只能在属性面板中设置??

    let canvas = this.addComponent(cc.Canvas)
    canvas.fitWidth = true
    canvas.fitHeight = true
    if (cc.sys.isBrowser) {
        canvas.designResolution.width = 1900
        canvas.designResolution.height = 860
        //this.node.setContentSize(cc.size(1900, 860))
    } else if (cc.sys.isNative) {
        canvas.designResolution.width = 1280
        canvas.designResolution.height = 720
        // this.node.setContentSize(cc.size(1280, 720))
    }

如上代码,在启动页面onload 中,宽度和高度无法这样设置,只能在启动场景的属性面板中添加canvas才可以,如下:

addcom?怕是get哟

你的this是StartScene还是StartScene所在的node, Canvas组件是要添加到节点上的。如果this是StartScene,那就把let canvas = this.addComponent(cc.Canvas)
改成下面这样试试
let canvas = this.node.addComponent(cc.Canvas)

表示这样的,按你说的还是不行,


你的StartScene已经在面板中添加了Canvas,你要把StartScene的Canvas移除掉啊,如果不移除那就这样: let canvas = this.getComponent(cc.Canvas)

按你说的也是不行!移出或者getCom都是不生效,只有属性面板中才生效

来,有引擎组的同学回答下吗??

我找到可行办法了。
其实可以OK的,就是node也需要跟着设置一下ContentSize

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);

2赞

designResolution有get/set方法,直接设置vec2宽高不会触发set当然无效