问题:CocosCreator中,Line无法设置为不透明的。
2022-11-30 本文已影响0人
全新的饭
问题说明
如图,Line是半透明的。

解决方案
为Line配置自己创建的材质:

如图,Effect必须是builtin-particle-trail,否则line无法显示。
Technique选1-alpha-blend,是为了不透明。
自制目标颜色的图片,拖入Line的Texture中,如

如图,配置了Texture和Material后,Line是不透明的。

如果想要运行时新建不透明的Line,可以在编辑时做出上述Line作为模板,在运行时
复制(instantiate)模板Line的Node,再获取复制出来的Node的Line组件,设置其Positions。
如果通过AddComponent的方式新建Line,再将Line模板的相关属性(Texture、Material、Width等)设置给新建的Line,实测设置Material是不成功的。效果如下图。

const line = this._lineTemplate.node.addComponent(Line);
line.texture = this._lineTemplate.texture;
line.material = this._lineTemplate.material;
line.positions = ([new Vec3(2, 0, 0), new Vec3(2, 5, 0)]) as never[];
line.width = this._lineTemplate.width;
// line.enabled = false;
// line.enabled = true;