Replace Avatar in Animator at runtime to create character configurator

I am working on some character customization. I am currently able to create Avatars from code (via the AvatarBuilder.BuildHumanAvatar API) which have the correct proportions and work fine with Mecanim.

However, I find that when I create those at runtime and assign them to an existing animator, the new Avatar is not taken into account.

What I have to do is destroy the Animator component, create a new one, and then assign the new Avatar. What’s more, destroying and creating the Animator component cannot happen on the same frame. So I end up destroying, waiting for the next frame, and then continue the rest of my setup.

There doesn’t seem to be anything in the API which could help me out. Rebind() for example sounds like a candidate, but doesn’t end up working.

Is there any way to have an existing Animator understand that it should work using the newly supplied Avatar? I don’t even mind if that implies it will restart the current controller/animation.

It seems that WriteDefaultValues() is what I want to use.
The documentation is unfortunately utterly unclear, so I have no real idea what it does, but a random trial of this seems to have the effect I’m looking for.

Hi did you find any solution for your issue?

Hi. A call to WriteDefaultValues() does update things to a point where the new Avatar is taken into account. So that solves that aspect of the problem. I have not taken that any further however, and I do seem to remember is resets any playing animation. So the change won’t be “smooth” as such.

1 Like