Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • 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 hit 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

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.

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 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.

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

I can confirm that this works. Thanks!

avatar image danieleolivieri · Aug 17, 2015 at 04:10 PM 0
Share

Thanks!!!!

avatar image RobertWebb · Feb 19, 2016 at 05:55 AM 4
Share

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.

avatar image Octarina · Dec 23, 2020 at 07:54 PM 0
Share

still the same problem in december 2020 with URP. This solution also always work.

avatar image burly87 · Mar 19, 2021 at 12:49 PM 0
Share

i have multiple Objects with emitted materials in the scene. all static, and it still don't work

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 which 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 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}

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.

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

framework not found FBLPromises after build in XCODE...ERROR 1 Answer

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

Game Objects are not visible in build of my 2D Games 0 Answers

releasing a demo to app store 0 Answers


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