Changing A material's _maintex at edit time.

I’m loading a png texture from disk in an editor extension, assigning it to a public field on a gameobject. Then I set this texture to the _mainxTexture slot of material using setTexture(), ( I have a reference to the material through a public inspector on a gameobject). The material is created manually in the editor.

The material is updated, it shows the texture, the mesh using the material shows the texture after my editor script runs.

When I goto file save, I get cleaning leaked objects messages for textures, and the material loses the reference to the texture, and the mesh loses the texture.

What am I missing?

You should use sharedMaterial; changing an object’s material creates a new instance of the material in the scene.

–Eric

Thanks Eric,

I actually am changing the shared material property of the mesh. The problem is that the material loses the reference to this texture.

I’m not sure how my solution is in terms of correctness, but I now create the texture in my first script, and using the gameobjects custom editor I load it using asset database load() I change the material’s texture, and then call assetdatabase save assets().

This is working so far.