• 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
Question by brianleake · Mar 12, 2015 at 08:57 AM · shaderguimaterial

Updating custom material properties for RawImage

I'm having some difficulty updating custom material properties for a RawImage UI element. I've written a custom shader (which works perfectly) that I've assigned to a "CustomMaterial" material and then added to the Material property for the RawImage at runtime. In code I'm then setting some of the material properties when the element is instantiated (such as color, gradient level, etc. that work with my shader). First time up everything works fine. But if I try to update the material properties, nothing gets sent through following a SetActive(false) and then subsequent SetActive(true).

Here's what I'm doing (objImage is a RawImage) --

         // Instantiate material so it's not shared
         objImage.texture = myTexture;
         objImage.material = Instantiate(Resources.Load("myCustomMaterial")) as Material;

         // Push colors through to material
         objImage.material.SetColor("_Color1",Color.red);
         objImage.material.SetColor("_Color2",Color.blue);

Everything is great, works just fine. So... I want to hide the UI and bring it back later with different properties (change the texture, update the shader parameters, etc.). In the meantime the object has been set to inactive objImage.gameObject.SetActive(false). After the object is set to active, everything seems to get BLOWN AWAY, all material properties are gone. The shader is relying on it's own default values. It looks like there may be an issue with RawImage objects that are deactivated and then reactivated losing their material properties.

It doesn't matter if I try to set any of the material properties, they are completely ignored.

Can anybody verify if this is a bug or if I'm not using the material property for RawImage correctly. I remember in NGUI they would handle this using the onRender callback to assign custom material properties.

  • Brian.

Comment
VamossVoar

People who like this

1 Show 0
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

2 Replies

· Add your reply
  • Sort: 
avatar image

Answer by VamossVoar · Feb 10, 2016 at 11:06 PM

Found the solution!

To remove the shared material you will need to instante the material(create a new one) and then set back to the image:

 Material mat = Instantiate(image.material);
 image.material = mat;
Comment
Carrotpie

People who like this

1 Show 0 · 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

Answer by byrne · Sep 09, 2015 at 03:30 AM

http://docs.unity3d.com/ScriptReference/Material.EnableKeyword.html

A few months late but maybe this might help someone else. I've found that by enabling the keywords on the shaders in custom unity shaders, it sort of 'instantiates' them to work properly. Call it in the awake or start functions on your Material object, maybe it will help.

Comment

People who like this

0 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 VamossVoar · Feb 10, 2016 at 09:16 PM 0
Share

That didn't work for me :( Even when I call it in the Update() mat.DisableKeyword("ShaderFolder/ShaderName"); mat.EnableKeyword("ShaderFolder/ShaderName");

avatar image VamossVoar · Feb 10, 2016 at 09:29 PM 0
Share

This topic has a similar problem: http://forum.unity3d.com/threads/changing-colors-of-a-material-for-separate-prefab-instances.281551/ It looks like the canvasrenderer always share the materal...

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to improve shader to render UI text correctly / DOF Cam / 0 Answers

GUI materials 3 Answers

Material doesn't have a color property '_Color' 4 Answers

Applying materials to the new UI elements (4.6) 1 Answer

How to Create Material Preview in Custom Editor 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