Hi,
I’m using the standard shader in Fade mode to fade some object’s alpha from 0 to 1 on a prefab with an animation. The scenario is :
- Prefab is instantiated with standard shader in Opaque mode.
- I start the animation.
- When it’s started, I set the shader’s mode to Fade by doing this :
foreach (Material m in meshRenderer.materials) {
Material currentMaterial = m;
currentMaterial.SetFloat("_Mode", 2f);
currentMaterial.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
currentMaterial.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
currentMaterial.SetInt("_ZWrite", 0);
currentMaterial.DisableKeyword("_ALPHATEST_ON");
currentMaterial.EnableKeyword("_ALPHABLEND_ON");
currentMaterial.DisableKeyword("_ALPHAPREMULTIPLY_ON");
Color c = currentMaterial.color;
currentMaterial.color = new Color(c.r, c.g, c.b, 0f);
}
- I start fading the objects alpha from 0 to 1.
- When it’s done, I reset the shader’s mode to Opaque by doing this :
foreach (Material m in meshRenderer.materials) {
Material currentMaterial = m;
currentMaterial.SetFloat("_Mode", 0f);
currentMaterial.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
currentMaterial.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
currentMaterial.SetInt("_ZWrite", 1);
currentMaterial.DisableKeyword("_ALPHATEST_ON");
currentMaterial.DisableKeyword("_ALPHABLEND_ON");
currentMaterial.DisableKeyword("_ALPHAPREMULTIPLY_ON");
Color c = currentMaterial.color;
currentMaterial.color = new Color(c.r, c.g, c.b, 1f);
}
It’s working well in the Editor but when I build it on iOS (I tried setting the Graphics API to Automatic, and I also tried to force it to Open GL ES 2.0, 3.0 and Metal), the fading doesn’t work at all.
Do you have an idea what the issue could be ?
Here’s my fading coroutine by the way :
private IEnumerator FadeObjectIn()
{
float time = 0.5f;
MeshRenderer meshRenderer = GetComponent<MeshRenderer>();
if (meshRenderer != null) {
foreach (Material m in meshRenderer.materials) {
Material currentMaterial = m;
Color c = currentMaterial.color;
float t = 0f;
while (t <= 1f) {
c.a = Mathf.Lerp(0f, 1f, t);
t += Time.deltaTime/time;
currentMaterial.color = c;
yield return null;
}
}
SetShaderMode(BlendMode.Opaque);
}
}