- Home /
How to access Emission Color of a Material in Script?
Hi, so I need a way to change the color of an Object over texture. So I guess the emission light color should be what I want. But I simply call it in Unity by using Material.SetColor("_Emission", newColor) as described in UnityDoc:
http://docs.unity3d.com/ScriptReference/Material.SetColor.html
But it seems not working. Am I right to use dat function? BTW, it could be changed in Inspector.
Answer by Unshackled · Dec 10, 2017 at 12:38 AM
I changed the emission color with this code:
Material mymat = GetComponent<Renderer>().material;
mymat.SetColor("_EmissionColor", Color.red);
Answer by terresquall · Feb 10, 2020 at 05:18 AM
You have to make sure that the Emission property on the material is enabled, which you can do using: material.EnableKeyword("_EMISSION");
. You will also have to use DynamicGI.UpdateEnvironment();
if you want the emission lighting to affect the environment. Take a look at this article. It explains all the little details you have to take care of to get emission lighting to work properly: https://blog.terresquall.com/2020/01/getting-your-emission-maps-to-work-in-unity/
Answer by CHPedersen · Aug 02, 2015 at 11:49 AM
Calling Material.SetColor("_Emission", newColor) only has an effect if the shader which is used to render the object actually has a variable called _Emission defined in it. This is the case for Vertex Lit shaders, as the docs also describe. Are you using a vertex lit shader?
No, actually I only use default Standard shader. Does it have a variable defining emission light?
Nope. :) Select the vertex lit shader like the docs say, and you'll see the Emissive color in the inspector:
No emissive color in the shader = no reaction when setting the color using SetColor.
@CHPedersen it works but not the way I want...So if I have a texture contains black, I actually want a red color could be kinds up covering that black. In VertexLit shader, the red is still covered by black. If I use standard shader and change Emission in the inspector, the Emission color could do that.
So I guess if I could check standard shader file and figure out which variable represents "Emission" in the inspector. I could still use standard shader. But obviously there is no ways to check out source code in Unity.......
Sure there is. :) Just not from within the editor itself. You can view the source code for the built-in shaders if you download them from this site. Click the link under "Additional downloads" and select built in shaders.
After you get them, you'll have to study the standard diffuse shader and standard vertexlit shader a bit, of course, to figure out how to add the emissive functionality to the diffuse one. It won't be terribly difficult, but you have to be at least comfortable with shader code to do so.
@CHPederson Haha you know what? Standard shader does have a definition for Emission called "_EmissionColor". But the weird thing is when I run it in the Editor, emission color was still black even though I called SetColor("_EmissionColor". Color.red) func. as below,
However when I unfolded the material window (game is on), it change to red immediately....O$$anonymous$$G
Have you ever met this before?
I had the same bug. When i changed the Global Illu$$anonymous$$ation, everything worked perfectly.
Answer by agenda · Aug 13, 2017 at 12:40 PM
this is because the shader wasnt in the scene at the start containing a entry called emission! if u apply and enable emission through script without having a copy with those features in the scene from start it fails!