Search Unity

I modify the standard shader emission in code, it not work in standalone build

Discussion in 'Shaders' started by sparkcube, Aug 2, 2015.

  1. sparkcube

    sparkcube

    Joined:
    Jul 19, 2015
    Posts:
    3
    Hi
    Please help.

    I modify the emission color of standard shader in my C# script code, it work in the editor view and game view, but if I make a standalone build, it dosen't work at all, Is there anyone met this problem before.
    My method of modify emission color is below.

    render .material.EnableKeyword ("_EMISSION");
    render .material.SetColor("_EmissionColor", tcol);

    It works well in the editor, but in standalone, take no effect.

    Please help!
     
  2. INedelcu

    INedelcu

    Unity Technologies

    Joined:
    Jul 14, 2015
    Posts:
    173
    Hi

    Do you have the Emission Color of your material set to black in the Editor? If it's set to black then the _EMISSION shaders will be stripped away at built time because _EMISSION is defined with shader_feature. This is an optimization. You can find more detail about this here: http://docs.unity3d.com/Manual/MaterialsAccessingViaScript.html

    Make sure the Emission Color is a non black color and you'll be able to animate the color using the script.
     
  3. Xoduz

    Xoduz

    Joined:
    Apr 6, 2013
    Posts:
    135
    Wild Hack Appears!

    Thanks to @argosy_ops for posting this information in a reply in the unityAnswers section, thought I'd mention it here since it took me a while to find it myself. Here's my step-by-step take on it:
    1. Place empty cube somewhere in your scene.
    2. Create a new material that uses a shader (like Standard or Standard (Specular setup) that matches that of materials you want to modify Emission Color for later.
    3. Give the new material an Emission Color value higher than 0.
    4. Apply the new material to the cube, then disable the cube through inspector.
    5. Now, even after making a standalone build, you can modify the Emission Color for any material that uses the same shader as this "dummy material", even if those materials didn't have any Emission Color specified at build-time.
    This seems very useful to me (I'm using Emission Color to highlight objects with mouse, for instance), but I am unsure about the potential negative effects this hack might have on performance, since @INedelcu mentioned that it works like this for optimization reasons...
     
    argosy_ops likes this.