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
0
Question by Fornoreason1000 · May 31, 2015 at 09:16 AM · spritematerialmeshrendererpropertiesmaterialpropertyblock

How do I set multiple Material Properties with MaterialPropertyBlock?

From What I know, MaterialPropertyBlock is used to change a Objects material properties without the need to create new materials themselves. It allows us to create multiple objects with the same material that have slightly different properties. and it does this within the Sprite Renderer component.

I'm using it to create multiple objects with slightly different , textures and colors and they have different editable UV values so Sprite Renderer obviously won't work here( I haven't found any Vertex shader hacks for it, not sure if that is legal anyway). So I decided to use the inbuilt MaterialPropertyBlock feature.

Easier said than done, There is very minimal example and the Docs are pretty vague at times. and there's only 11 UA questions that mentioned MaterialPropertyBlock since 2011. But non of them seem to address this issue.

    /// <summary>
     /// Updates the current sprite texture
     /// 
     /// Due to the way MeshRender works, an unavoidable unity glitch will cause the 
     /// sprite to display the defualt white texture in the Editor but will work when   
     ///you play test but Unrelated to UA Question.
     /// </summary>
     /// <param name="color"></param>
 
 public void UpdateObjectTexture()
     {
         print("Updating Object Texture");
         block.Clear();
         block.SetTexture("_MainTex", sprite.texture);
         meshRenderer.SetPropertyBlock(block);
 
         block.Clear();
         block.SetVector("_Color", color);
         meshRenderer.SetPropertyBlock(block);
 
     }


This seems like it should work , I receive no errors or warnings of any kind. but it seems to not working, the last SetPropertyBlock() appears to cancel out the first. for example, if i put color first, the texture will appears not tinted by the color, if color is last, the texture will be set to null (thus making it invisible). Setting both at once doesn't yield any better results.

I've tries, playing around with the functions inside MaterialPropertyBlock, Renderer and Shader. I know you can assign the same property multiple times shown here: http://answers.unity3d.com/questions/856056/help-with-materialpropertyblock-is-needed.html

Is it actually possible to use this MaterialPropertyBLock to assign two property of two different types just like SpriteRenderer Does? It appears to not be working. Or Am i Missing something here?

Thanks in advanced

Comment
Add comment
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

1 Reply

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

Answer by Mnemonic624 · Jun 13, 2015 at 07:12 PM

It's because of block.Clear(). If you do :

 block.Clear();
 meshRenderer.SetPropertyBlock(block);

It will assign the default material values. What you need to do is :

 block.Clear();
 block.SetTexture("_MainTex", sprite.texture);
 block.SetVector("_Color", color);
 // Set other values
 meshRenderer.SetPropertyBlock(block);
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 Fornoreason1000 · Jun 28, 2015 at 05:07 AM 0
Share

I've tried that before ,

What seems to happen here, it will use the last Set value and ignore all the ones before it. I added Block.clear hoping to solve this issue.

In this case when i print the value of meshrenderer.shared$$anonymous$$aterial.mainTexture, it equates to null, but the color value returns appropriately

The result is invisible.

avatar image CaseyLee · Aug 29, 2017 at 03:11 AM 0
Share

using property block, inst going to be able to check the result with meshrenderer.shared$$anonymous$$aterial.mainTexture. because your not changing that value, moreover you may be creating a new instance of the material.

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

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

Related Questions

How to render a sprite to a quad? 1 Answer

Changing sprite properties at runtime 2 Answers

Masking with Mesh Renderer (2d) 1 Answer

Sprite disappears after adding a material to it 1 Answer

Pixel snap for material made with Unlit Shader Graph? 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