I have a problem which drives me crazy about material swapping.
This happens on iOS. Not saying it doesn’t happen on other platforms, just that this is the one I’m testing with.
Imagine a chess game; the board is composed of multiple tiles, the user being able to touch them in order to select them. Depending on which tile is selected and what the user is allowed to do with it (attack, move, etc.) the tiles change their material in order to give the user a visual feedback.
I’m running my game on an iPad, using Xcode’s Instruments to follow the memory consumption. I noticed that every time I touch a tile and thus change its material some new memory is allocated. I don’t get why, because all I do is
if (.....) renderer.material = attackMat; else if (...) renderer.material = validMoveMat;
where attackMat and validMoveMat are some standard Unity Material already assigned at scene start.
The 2nd point is that this memory allocation occurs only the 1st time I touch each tile: if I touch tile A1 some memory is allocated and its material is changed; if I touch it again (whenever, not necessarily justa after the first touch) its material will be changed again, but no more memory allocation (even if the new material is not one of the 2 already used).
I ran some extensive tests and am pretty sure now that the memory is allocated for that purpose, I just don’t understand why and how to avoid it.