- Home /
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
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);
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.
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.