Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
4
Question by dmfarrington · Jun 12, 2015 at 12:59 AM · c#unity 5shadertexture

Changing material in code doesn't update shader

I am trying to generate a series of Materials, all from on a base texture with varying Albedo detail textures applied to it (decals).

Currently to test this, I have it generating a single material with the very first decal on it and then applying it to an object. The result is that it does this correctly.. but it does not show in game with the new shader unless I manually go in an open the shaders detail panel.

 void Update()
     {
         if(_isAddNew)
         {
             _isAddNew = false;
 
             // Get the mesh renderer, as this is the renderer type used on _objectToApplyTo.
             MeshRenderer newRenderer = _objectToApplyTo.GetComponent<MeshRenderer>();
             // Set the material to use a Standard shader.
             newRenderer.material = new Material(Shader.Find("Standard"));
 
             // Albedo. Getting the base material, then the main texture from it.
             newRenderer.material.mainTexture = _materials[0].GetMaterial().mainTexture;
             // Detail Mask
             newRenderer.material.SetTexture("_DetailMask",_decals[0]);
             // Detail Albedo x2
             newRenderer.material.SetTexture("_DetailAlbedoMap",_decals[0]);
 
             _objectToApplyTo.GetComponent<MeshRenderer>().material = newRenderer.material;
         }
     }

Here is a step by step to help better show this. Image describing material issue with shader

I am using Unity 5.0.0f4 Personal Edition.

Thank you for any help and advice.

materialissue.png (121.4 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 Roy Kelleners · Jun 18, 2015 at 06:55 PM 0
Share

Sorry for not posting a solution. I just want to confirm that I have the exact same issue with version 5.1.0f3 Personal. I tried assigning a different shader and then switching back but that does not work either. If only we could see what gets executed once we click the material in the editor.

3 Replies

· Add your reply
  • Sort: 
avatar image
8

Answer by smcclaire · Jul 07, 2015 at 10:35 PM

It seems the Standard Shader is 'smart' about what parts of the shader are enabled, and you need to use material.EnableKeyword() to tell it to turn on a feature if you're changing things through code. In my case I was assigning a normal map via the "_BumpMap" parameter, which worked after I added the following line to my code:

material.EnableKeyword("_NORMALMAP");

Comment
Add comment · Show 2 · 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 trx · Sep 10, 2015 at 10:46 AM 3
Share

Confirmed Solution, Be sure to use the correct keyword (not the propertyname!), was my mistake first.

see: http://docs.unity3d.com/$$anonymous$$anual/$$anonymous$$aterialsAccessingViaScript.html

avatar image kamathaj · Jan 17, 2017 at 05:50 AM 0
Share

Was facing the same issue. This solution works properly in Unity 5.4.3 as well. Thanks a ton!

avatar image
4

Answer by saulth · Jul 06, 2017 at 11:52 PM

The comprehensive way to do this is to use the code written by Unity that is executed when you make changes to the material in the Editor. The code is available for download from https://github.com/Unity-Technologies/UnityCsReference/blob/master/Editor/Mono/Inspector/StandardShaderGUI.cs. Then, open up the StandardShaderGUI.cs file and copy out the five functions at the bottom (SetupMaterialWithBlendMode, GetSmoothnessMapChannel, SetMaterialKeywords, MaterialChanged, SetKeyword). Also copy the enums at the top of the class (WorkflowMode, BlendMode, SmoothnessMapChannel) and put in your own class. Then you want to call MaterialChanged() with the WorkflowMode.Metallic on your Material that you modified. Works identically to in-Editor changes :).

Comment
Add comment · Show 4 · 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 Waleguene · Nov 08, 2017 at 03:15 PM 0
Share

Thanks a lot for this!

avatar image ExatexX · Sep 06, 2020 at 08:09 AM 0
Share

Link is broken, you can now find the file here: https://github.com/Unity-Technologies/UnityCsReference/blob/master/Editor/$$anonymous$$ono/Inspector/StandardShaderGUI.cs

avatar image saulth ExatexX · Sep 06, 2020 at 08:18 AM 0
Share

Thanks for the link

avatar image HunterAhlquist ExatexX · Dec 19, 2021 at 02:10 PM 0
Share

Link is broken again, here's a new one https://github.com/Unity-Technologies/UnityCsReference/blob/master/Editor/Mono/Inspector/StandardShaderGUI.cs

avatar image
1

Answer by smcclaire · Jul 08, 2015 at 06:59 AM

Confirmed in 5.1.1f as well. Something doesn't update until you click the inspector.

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

12 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 avatar image

Related Questions

Color specific sections of a texture 1 Answer

Mobile Shader with Albedo Color and Normal Map 0 Answers

Applying decals / textures to uneven surfaces runtime 1 Answer

Texture2D to Texture3D 2 Answers

Distribute terrain in zones 3 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