So I’ve run into this strange issue with using MaterialPropertyBlock.SetTexture on a custom shader.
Is there some special way to specify in your shader properties that a texture can be set using this method?
When I test it on the Unity SimpleUnlit material it works as expected but when I apply it to my own shader it totally fails.
I can use Material.SetTexture with the same parameter and Texture2D and that works fine, but using a MaterialPropertyBlock it doesn’t. At the same time I can update ints, floats, and vectors with a property block totally ok, just not textures.
Shader property is defined like this:
[NoScaleOffset] _Weights("Weights", 2D) = "white" {}
_Frame("Frame", Float) = 0
_Imagesize("ImageSize", Vector) = (400,150,0,0)
and I’m overriding like this at runtime
Renderer rend = obj.transform.GetComponent<Renderer>();
propBlock = new MaterialPropertyBlock();
rend.GetPropertyBlock(propBlock);
propBlock.SetTexture("_Weights", tex);
propBlock.SetVector("_Imagesize", size);
propBlock.SetInt("_Frame", 1);
rend.SetPropertyBlock(propBlock);
that fails whilst this works
Renderer rend = obj.transform.GetComponent<Renderer>();
Material[] mats = rend.sharedMaterials;
mats[0].SetTexture("_Weights", tex);
So I know the issue isn’t in the texture2d it’s just not getting set by the property block.
and if I change the material to a SimpleUnlit I can set the _BaseMap to my texture using the same property block code, just changing the parameter name, so the code is ok. It has to be in the shader, right?
What am I missing?