根据3.3教程 组件和组件执行顺序 · Cocos Creator
用这段代码:
const myComponent = node.addComponent(MyComponent);
我写的是:
const button = this.node.addComponent(Button);
调试时button 不为null且有btn对象
但是界面没有出现BTN,请问之后还需要设置什么属性?
根据3.3教程 组件和组件执行顺序 · Cocos Creator
用这段代码:
const myComponent = node.addComponent(MyComponent);
我写的是:
const button = this.node.addComponent(Button);
调试时button 不为null且有btn对象
但是界面没有出现BTN,请问之后还需要设置什么属性?
没有添加sprite组件
感谢,好像是第二次回我的帖子了
const button = this.node.addComponent(Button);
const sprite = this.node.addComponent(Sprite);
这样没显示控件
还需要什么?
sprite还有设置spriteframe
动态创建的组件,Layer是默认的DEFAULT,2种修改方案
1 把Canvas下的Camera的visibility的DEFAULT勾选上,允许渲染DEFAULT的节点;
2 修改Button的Layer为UI_2D,设置 button.layer = Layers.Enum.UI_2D;
能显示出来了,3.3这个版本位置怎么该? this.node.setPosition(new Vec3(500, 500, 500));没反应
//添加BTN为点击事件做准备
const button = this.node.addComponent(Button);
this.node.layer = Layers.Enum.UI_2D;
//添加渲染组件
const sprite = this.node.addComponent(Sprite);
sprite.sizeMode = Sprite.SizeMode.CUSTOM;
sprite.spriteFrame =this.targetNode;
//添加矩形信息
const uITransform =this.node.addComponent(UITransform);
uITransform.width =100;
uITransform.height =100;
// button.node.setPosition(new Vec3(500, 500, 500));
this.node.setPosition(new Vec3(500, 500, 500));
设置 new Vec3(500, 500, 0);
this.node.setPosition(new Vec3(500, 500, 0)); 这个代码位置没变
是不是因为设置了this.node.layer = Layers.Enum.UI_2D; 导致位置坐标变了?
this.node 你这个node是哪个?
TS脚本是挂在添加控件时自动添加的Canvas上,所以this.node应该是Canvas的node
新建一个node之后就可以了
感谢两位!
感谢两位!