I am looking to change the motion on a state via code. This is generally so that I can set up mechanim with a state for a certain type of animation and just sub them in as needed.
I was able to do the same concept with the legacy animation - just hold a list of strings for alternative animations. I was also assuming the animation was already on the character. However, I don’t see an obvious way to change it out on mechanim. Is anyone familiar with how to achieve something like this?
And the idea is to avoid having to wire up identical states with only the motion differing.
EDIT: Was able to get help on the forums to update this for Unity 5.4
Here is some code that I current am using to override animations:
/// <summary>
/// Sub in real animations for stubs
/// </summary>
/// <param name="animator">Reference to animator</param>
public void SetCurrentAnimation(Animator animator, string animNameToOverride)
{
RuntimeAnimatorController myOriginalController = animator.runtimeAnimatorController;
AnimatorOverrideController myCurrentOverrideController = myOriginalController as AnimatorOverrideController;
if (myCurrentOverrideController != null) // retrieve original animator controller
{
// don't reset if its already overriden
if (myCurrentOverrideController[animNameToOverride] == animClip)
{
Debug.Log("Current state is already" + animClip.name);
return;
}
myOriginalController = myCurrentOverrideController.runtimeAnimatorController;
// Know issue: Disconnect the orignal controller first otherwise when you will delete this override it will send a callback to the animator
// to reset the SM
myCurrentOverrideController.runtimeAnimatorController = null;
}
AnimatorOverrideController myNewOverrideController = new AnimatorOverrideController();
myNewOverrideController.runtimeAnimatorController = myOriginalController;
myNewOverrideController[animNameToOverride] = animClip;
animator.runtimeAnimatorController = myNewOverrideController;
Object.Destroy(myCurrentOverrideController);
}