直接更改是不可以的:
private MeshRenderer render;
private float FadeSpeed = 10;
private void Start()
{
render = GetComponent();
}
private void OnGUI()
{
if (GUILayout.Button("淡入淡出"))
{
StartCoroutine(Fadeout());
}
}
private IEnumerator Fadeout()
{
Color currentColor = render.material.color;
while(currentColor.a>0)
{
render.material.color.a -= 0.01f;
yield return null;
}
}
必须将循环中的内容换成: private IEnumerator Fadeout()
{
Color currentColor = render.material.color;
while(currentColor.a>0)
{
currentColor.a -= 0.01f;
render.material.color = currentColor;
yield return null;
}
}
请问是为什么呢
因为你没有修改设置材质的渲染模式(RenderingMode),需要改成Transparent
SetMaterialRenderingMode (GetComponent ().material, RenderingMode.Transparent);