在我们实际开发中,往往会遇见需要更换对象材质贴图的情况,这时如果更换材质球就显得十分的笨拙。这时我们可以使用API,配合shader直接动态更改对象的材质贴图。
使用SetTexture方法。

-
首先我们需要知道每个材质球都对应着一个shader。(而unity shaderLab的组织形式有三种可以编写的shader(surface shader,vertex and fragment shaders,fixed function shaders))
-
我们在Inspector面板材质球所有可以编辑的其实都是shader的"原材料",它将在SubShader下的Pass通道中进行渲染处理。


通过以上可以知道,控制我们贴图的也就是shader,Properties中的_MainTex


回到SetTexture方法。
这时的第一个参数也就是_MainTex,第二个Texture就不用说了大家都理解。
- 具体实现,提供一个供UGUI, Button OnClick事件触发的接口方法,并绑定。

- 实现

网友评论