I’m trying to set the texture via code on a material used in a post-process image effect. I’m calling SetTexture() on a reference to the material, but it’s not having any effect.
In this example, myMaterial has a shader that has a texture property. Just for this test, the shader simply overwrites the output with whatever color is in the texture.
[ExecuteInEditMode]
public class MyImageEffect : MonoBehaviour
{
public Material myMat;
public Texture myTexture;
void Start()
{
myMat.SetTexture(0, myTexture);
}
void OnRenderImage(RenderTexture source, RenderTexture destination)
{
Graphics.Blit(source, destination, myMat);
}
}
This all works fine if I drag a texture into the material’s texture property before running. However, if I try to assign the texture at runtime, it does not work.
For non-image-effect materials, you can change the texture on a material by grabbing the material from an object’s renderer:
void Start ()
{
thisRenderer = GetComponent<MeshRenderer>();
thisRenderer.sharedMaterial.SetTexture(0, myTex);
}
In the case of a material used in a image effect, there is no renderer to grab the material off, so I’ not sure if I’m accessing it the correct way.
Any help would be appreciated.