• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
5
Question by JosueMariscal · May 19, 2015 at 07:33 PM · buildemission

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 $$anonymous$$t play. Unfortunately the exact same code doesn't work when I build the player.

Emission in Editor:

alt text

Emission in Build:

alt text

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? :)

5-19-2015-10-06-50-am.png (137.8 kB)
5-19-2015-10-07-24-am.png (156.1 kB)
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image argosy_ops · May 28, 2015 at 06:58 AM 0
Share

3 Replies

· Add your reply
  • Sort: 
avatar image
15
Best Answer

Answer by argosy_ops · May 28, 2015 at 06:58 AM

A colleague of mine has just discovered the reason for t$$anonymous$$s behaviour. Unity internally uses various different shader variants and by default, _EMISSION is not used/compiled. When you set t$$anonymous$$s 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.

T$$anonymous$$s 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. T$$anonymous$$s caused the compiler to deliver the needed shader for the standalone player.

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Philip-Rowlands · Jul 18, 2015 at 08:22 AM 0
Share
avatar image danieleolivieri · Aug 17, 2015 at 04:10 PM 0
Share
avatar image RobertWebb · Feb 19, 2016 at 05:55 AM 4
Share
avatar image Octarina · Dec 23, 2020 at 07:54 PM 0
Share
avatar image burly87 · Mar 19, 2021 at 12:49 PM 0
Share
Show more comments
avatar image
2

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 w$$anonymous$$ch uses the _emission keyword. alt text


emission-fix.png (21.4 kB)
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by engelhard90 · Feb 15, 2021 at 05:30 PM

I solved t$$anonymous$$s issue by enabling the Emission in the Shader Material and setting the default color to somet$$anonymous$$ng very low.

T$$anonymous$$s code of the .mat:

- _EmissionColor: {r: 0, g: 0, b: 0.004, a: 1}

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Distribute terrain in zones 3 Answers

Build completed with a result of failed (can't build my android game) 2 Answers

Command-line build 64-bit 1 Answer

not able to build my game 1 Answer

Does Unity cull away unused assets on build time? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges