又被问到多重材质球的特定材质球怎么替换。之前遇到这个问题的时候,天真的以为可以用skinned.materials[0] = xxx替换,结果不能替换,但是材质球特定的贴图是可以替换。想到materials是个数组,结果用一组材质球替换就成功了。
public Material[] mat;
public Texture texture;
SkinnedMeshRenderer skinned;
//替换多重材质球
skinned.materials = mat;
//替换多重材质球里特定贴图
skinned.materials[0].mainTexture = texture;
//
先看下效果图
上代码
public class ChangeMat : MonoBehaviour
{
//多层材质球
public Material[] mat;
//贴图--眼睛
public Texture eye;
//题图---衣服
public Texture cloth;
SkinnedMeshRenderer skinned;
/// <summary>
/// 替换多重材质球
/// </summary>
void ChangeMats()
{
skinned.materials = mat;
//之前的错误写法
//skinned.materials[0] = xxx;
//skinned.materials[1] = sss;
//skinned.materials[2] = fff;
}
/// <summary>
/// 替换多重材质球贴图--眼睛
/// </summary>
void ChangeMatsTextureEye()
{
skinned.materials[0].mainTexture = eye;
}
/// <summary>
/// 替换多重材质球贴图--衣服
/// </summary>
void ChangeMatsTextureCloth()
{
skinned.materials[3].mainTexture = cloth;
}
private void Start()
{
skinned=transform.Find("/f05_bathtowel_02_l/f05/f05_bathtowel_02_l").GetComponent<SkinnedMeshRenderer>();
Button changeMats = transform.Find("/Canvas/Button").GetComponent<Button>();
changeMats.onClick.AddListener(() => { ChangeMats(); });
Button changeEye = transform.Find("/Canvas/Button (1)").GetComponent<Button>();
changeEye.onClick.AddListener(() => { ChangeMatsTextureEye(); });
Button changeCloth = transform.Find("/Canvas/Button (2)").GetComponent<Button>();
changeCloth.onClick.AddListener(() => { ChangeMatsTextureCloth(); });
}
}