- Home /
Emission at run time works in Editor, but not in build
I'm setting the Emission in the Standard Shader at runtime and it works perfectly as expected in the editor when I hit play. Unfortunately the exact same code doesn't work when I build the player.
Emission in Editor:
Emission in Build:
Here is the code I'm using to change the emission at run time:
foreach (Transform obj in Xforms)
{
if (obj.GetComponent<MeshRenderer>() != null)
{
foreach (Material mat in obj.GetComponent<MeshRenderer>().materials)
{
if (mat.mainTexture != null)
{
if (mat.HasProperty("_EmissionMap"))
{
if (mat.GetTexture("_EmissionMap") != null)
{
float randNum = Random.Range(0f, 1f);
mat.EnableKeyword("_EMISSION");
if (randNum < .2)
{
mat.SetColor("_EmissionColor", new Color(0f, 0f, 0f, 0f));
}
else
{
float randColor = Random.Range(.8f, 1f);
mat.SetColor("_EmissionColor", new Color(1f, 1f, randColor, 1f) * 1.5f);
}
}
}
}
}
DynamicGI.UpdateMaterials(obj.GetComponent<MeshRenderer>());
}
}
DynamicGI.UpdateEnvironment();
Ideas? :)
Having exactly the same issue and can't figure out what's wrong! I've tested this against forward and deferred renderer and everytime I build for Windows (DX11), all emissive surfaces are no longer emissive.
I wonder if this is a missing build setting or maybe a bug. This approach has often been discussed as the way to go on the forums, yet it doesn't work in a build.
Answer by argosy_ops · May 28, 2015 at 06:58 AM
A colleague of mine has just discovered the reason for this behaviour. Unity internally uses various different shader variants and by default, _EMISSION is not used/compiled. When you set this in runtime, the editor will compile the respective shader variant on the fly, so you see your emission there.
When you build a standalone player however, Unity will only compile the shaders that are referenced by objects in your scene. So in case you don't already have emissive materials in your scene (not set at runtime), your code will call for a shader that hasn't been compiled.
This should usually be circumvented through the use of ShaderVariantCollection, but that did not work in our case. What we did was to add a simple cube to the scene with an emissive material. This caused the compiler to deliver the needed shader for the standalone player.
I fixed it even simpler by adding a material to the Assets/Resources folder (I called it AllowEmission), and set the emission of the material to something other than black.
You don't need to use the material on anything because as long as it's in the Resources folder Unity will presume it's going to be needed.
still the same problem in december 2020 with URP. This solution also always work.
i have multiple Objects with emitted materials in the scene. all static, and it still don't work
Answer by Art-Leaping · Nov 28, 2016 at 09:32 AM
Got it working with ShaderVariantCollection by manually including one of the Standard shader variants which uses the _emission keyword.
Answer by engelhard90 · Feb 15, 2021 at 05:30 PM
I solved this issue by enabling the Emission in the Shader Material and setting the default color to something very low.
This code of the .mat:
- _EmissionColor: {r: 0, g: 0, b: 0.004, a: 1}
Your answer
