- Home /
How do I change render.material at runtime seamlessly?
I have an enemy death effect that requires a different material. When I set the renderer.material to a new material (happens in an animation event callback), I get a few frames of nothing being rendered for the mesh before it reappears with the new material. I'd like it to be continuous, i.e. one render frame it's the old material and the next it's the new one.
Is there a solution or workaround? Am I just going about this the wrong way?
Answer by Owen-Reynolds · Jan 17, 2015 at 10:44 PM
I've swapped around plenty of materials, and never seen anything like that. Even if it was some sort of loading issue, there would be a frame drop - it wouldn't just run the game without things loaded. And even if it did, I think the object would be pink, not invisible.
My guess is it's a completely unrelated problem (but maybe triggered somehow by your changing the material, or not.)
Is there anything odd about the new material? Crazy custom shader with lots of moving vars? Does it happen with even a simple (diffuse blue) material?
It is a somewhat complex shader from a pack bought on the asset store. The problem doesn't happen with a simple diffuse shader. I don't know why I didn't think to try that. Thanks for the lead. I'll try to remember to update here if I find the specific issue.
ANSWER: I was animating an alpha cutoff on the death material. Despite the material having a default opaque state and my animation curve starting at opaque, there was an animator state transition into the curve. It was interpolating between 0 and the starting value at frame 0 of the curve. I guess 0 is the default value for any float that is undefined in a previous animation state. I made the transition instant (no blending) and the frames of "nothing rendering" went away.
Seems obvious in hindsight...
Answer by DaDonik · Jan 17, 2015 at 08:56 PM
My guess is that the texture belonging to the material you want to display is not already used somewhere else. Therefore Unity has to load the texture, which takes a while.
You could try to preload the material when you are starting the game/level. I would test it by simply assigning the material to a cube, placed offscreen where it's not seen by the player. That way the texture will already be loaded into memory and switching materials should be instant.
I haven't tried this, but it's not that complicated to do so =)
It's a good guess, but that's not it. I made an item in the scene as you suggested (tried in the view too), with the death material permanently on it to verify.
Hmm thats strange. Sadly that was my best guess... Let's hope someone with more experience can shed some light on this.
Your answer
Follow this Question
Related Questions
Check What Material is Being Used? 1 Answer
How to update texture during runtime to display damage? 0 Answers
Building a Unity 5 Standard Material at runtime 3 Answers
Multi materials for one mesh 0 Answers
material Instance only updates after changes, when the Material Component is expanded or folded 0 Answers