更换材质问题。

有点蠢的问题

@property({ type: cc.Material, displayName: '要更换的材质'}) RZMaterial = null;
@property({ type: cc.RenderableComponent, displayName: '目标节点带有Mesh'}) RZMesh = null;

见上,我有两个开放的编辑器槽,一个传入资产里的材质,另一个传入一个节点,这个节点是带 cc.MeshRenderer 的,Mesh是设置了一个资产的模型,Meaterials数量有1个,也是设置了。

我希望通过代码也就是获取目标节点的MeshRenderer后设置它的材质。
代码类似:

this.RZMesh._materials[0] = this.RZMaterial;
console.log('-------------------'+this.RZMesh.materials()[0]);

当然它是错误的。api文档我看了在
https://docs.cocos.com/creator3d/api/zh/classes/model.renderablecomponent.html

另外编辑器开放接口不能设置为cc.MeshRenderer 它不认,然后即便定义是cc.Node然后用getComponent(cc.MeshRenderer) 是null

所以它到底是什么逻辑?

我重新搜了一下发现有个换材质参数的做法

@property({ type: cc.Texture2D, displayName: '要更换的贴图'}) RZMaterial = null;
@property({ type: cc.ModelComponent, displayName: '目标节点带有Mesh'}) RZMesh = null;

this.RZMesh.materials[0].setProperty('mainTexture',this.RZMaterial);

所以该方法是更换材质上的一个mainTexture参数的数据,这里RZMaterial 是Texture2D

另外发现一个更诡异的现象 当我开放编辑器参数为cc.Material的时候下拉框的确可以选择某材质,但框里面内容依然显示cc.Texture2D 应该只是一个显示bug

但我到现在也无法试出更换整个materials中下标的一个

不能直接修改任何 renderable component 的私有成员噢,如果要修改某个索引的材质可以用 setMaterial 接口

我C奇葩,之前文档里我翻来覆去都没找到有set 或者get 的接口,都是直接得到一个material,这下找了一下果然有,然后还起效了。。。。。。。

@property({ type: cc.Material , displayName: '要更换的材质'}) RZMaterial = null;
@property({ type: cc.RenderableComponent, displayName: '目标节点带有Mesh'}) RZMesh = null;

this.RZMesh.setMaterial(this.RZMaterial,0);