Search Unity

Make sharedMaterial not leak!

Discussion in 'Wish List' started by Frieza, Feb 23, 2012.

  1. Frieza

    Frieza

    Joined:
    Aug 29, 2008
    Posts:
    68
    If you edit the properties of a material via script using the .material property it will create an instance of the material.

    Obviously, if you wanted to change a lot of different objects that share the same material at run-time you'll want to use the .sharedMaterial property as it won't create multiple copies of the material and help with dynamic batching.

    The thing is, after you have stopped playing in the editor, the material asset itself remains in the state that your scripts last left it, not the state it was in BEFORE you clicked play.

    Thus, materials "leak" when in the editor play mode. I don't know if this is an intended feature or if it is a bug hence why I'm posting here. It doesn't make sense to have the material asset itself change after every run of the game, especially when you are using version control (your material will basically be marked as 'changed' after every run even if you've actually made no changes).
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    The thing to do is instantiate the material in a script (in Awake or wherever), then assign the instantiated material as sharedMaterial. That way the original isn't touched.

    --Eric