How to NOT save a mainTexture? [SOLVED]

Hello!
I am working on a custom sprite renderer as I need to matrix distort the mesh and play with advanced shader things. (As of Unity 2019.2.9f1, I couldn’t find how to do that with the provided sprite renderer)

Everything works just fine but I have some optimization woes. I am using atlases, but because my sprite needs a texture in edition mode (for obvious reasons), I use the texture that the sprite provides (mainly because I don’t know if the atlas is built when I use the sprites) and in runtime my sprite’s mesh renderer and material and texture is loaded with the sprite’s texture. Then I can immediately switch to the atlas’s texture.

What I’d like to do is to never save the material, just omit it from serialization, I’m dynamically creating it.

Maybe its impossible, so anyone got any luck with a custom sprite solution that heavily relies on Unity’s?

Hello again!

Found a solution. Kind of dirty but it does the trick.

EditorSceneManager.sceneSaving += OnSceneSaving
EditorSceneManager.sceneSaved += OnSceneSaved

On sceneSaving, I remove the texture.
On sceneSaved, I add the texture back.

Sadly when building, the active scene is saved without calling these events, so we must patch this with:

public class OnSavedWithBuild : IPreprocessBuildWithReport, IPostprocessBuildWithReport

This class retrieves all the active sprites and calls OnSceneSaving and OnSceneSaved on them.

Also added:
EditorUtility.ClearDirty()
on the mesh renderer and on the material whenever I change them (including when I clear the texture on sceneSaving.) It also prevents some other editor problems.

The solution seems to work with the few scenarios I ran through, I hope it will prove solid in the long run.