Changing CustomRenderTexture.material property changes material in project

Changing properties of a CustomRenderTexture.material, saves those changes to the material asset in the project.

This is very unfortunate in my case, because every time I press Play in the editor, the game modifies properties of the CustomRenderTexture.material and I end up with various material changes in git, that should take place in-memory only.

I guess a workaround would be to instantiate the CustomRenderTexture.material at runtime and work on the in-memory duplicate instead. But this “issue” must be so common, that I’m afraid I must miss something very obvious in the API to not modify the project material asset.

I hoped there would be a [NonSerialized] shader attribute, but that’s not the case.

How do you change CustomRenderTexture.material properties without modifying the material asset in the project?

As you suspected, the correct way to solve this is by running this code before you make any edits to your material at run-time: CustomRenderTexture.material = new Material(CustomRenderTexture.material);

This isn’t as hacky as it seems. When you modify the material of a renderer (such as MeshRenderer.material), Unity will quietly run renderer.material = new Material(renderer.material); the first time you call it so that edits are not permanently applied to your source material. If you’d like to modify the material of a renderer without this automatic cloning of the material, you access renderer.sharedMaterial instead of renderer.material.