SetColor on Material, is it persisting by default?

I have seen a piece of code that does this:

public Material outlineMaterial;

//...

void SomeFunction()
{
    outlineMaterial.SetColor("_OutlineCol", outlineColor);
    
    // ...
}

On Unity program exit, this change will be committed to the material file.

If I want to avoid such change, what’s the proper way to clone/instantiate this material?

PS: the material is not associated with a renderer, as it is being used with Graphics.Blit for a shader effect.

Make a copy of the Material.

public Material originalMaterial;
private Material clonedMaterial;

private void Start()
{
    clonedMaterial = new Material(originalMaterial);
}

void SomeFunction()
{
     clonedMaterial.SetColor("_OutlineCol", outlineColor);
}
2 Likes