Standalone Player Transparency Different Than Editor,Mesh Renderer acts different in standalone game
I have a scene with several mesh chunks. I have a script that interprets what the camera is looking at, grabs that particular chunks renderer, and use the mouse wheel to change the transparency. When I Play the scene within the Editor, it works perfectly. However, when I export a standalone player and launch that, it does not work as expected. I can tell the material property changes in response to the mouse wheel input, but it doesn't make the shader transparent like it does in the Editor. Here's the code in question, as part of the FirstPersonController's Update() function:
var axisDelta = Input.GetAxisRaw("Mouse ScrollWheel");
if (axisDelta != 0)
{
GameObject targetGO = lastHit.transform.gameObject;
Renderer meshRenderer = targetGO.GetComponentInChildren<Renderer>();
Material mat = meshRenderer.sharedMaterial;
Color currentColor = mat.color;
if (currentTool == 1)
{
var newAlpha = 1.0f;
if (currentColor.a + axisDelta >= 1.0f)
{
mat.SetFloat("_Mode", 0); // Set to Opaque Mode if new alpha >= 1
mat.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
mat.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
mat.SetInt("_ZWrite", 1);
mat.DisableKeyword("_ALPHATEST_ON");
mat.DisableKeyword("_ALPHABLEND_ON");
mat.DisableKeyword("_ALPHAPREMULTIPLY_ON");
mat.renderQueue = -1;
newAlpha = 1.0f;
}
else
{
mat.SetFloat("_Mode", 3);
mat.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
mat.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
mat.SetInt("_ZWrite", 0);
mat.DisableKeyword("_ALPHATEST_ON");
mat.DisableKeyword("_ALPHABLEND_ON");
mat.EnableKeyword("_ALPHAPREMULTIPLY_ON");
mat.renderQueue = 3000;
newAlpha = (currentColor.a + axisDelta);
}
if (newAlpha > 1.0f)
{
meshRenderer.sharedMaterial.color = new Color(currentColor.r, currentColor.g, currentColor.b, 1.0f);
}
else if (newAlpha < 0.0f)
{
meshRenderer.sharedMaterial.color = new Color(currentColor.r, currentColor.g, currentColor.b, 0.0f);
}
else
{
meshRenderer.sharedMaterial.color = new Color(currentColor.r, currentColor.g, currentColor.b, newAlpha);
}
Debug.Log("Current Alpha = " + meshRenderer.sharedMaterial.color.a);
}
else if (currentTool == 2)
{
meshRenderer.sharedMaterial.color = new Color(checkColorBounds(currentColor.r, axisDelta),
currentColor.g,
currentColor.b,
currentColor.a);
}
}
For what it's worth - using the second tool (towards the bottom of the code) that changes just the red channel and not the alpha value works perfectly, so it's something specific to changing the alpha value...
Here's what it's supposed to do: GIF of Functionality Working in Editor