This example demonstrates how to use an AnimationMixerPlayable
to blend an AnimationClip
with an AnimatorController
. To do this, you must first wrap each asset in its corresponding playable:
AnimationClip
(clip
) in an AnimationClipPlayable
(clipPlayable
).RuntimeAnimatorController
(controller
) in an AnimatorControllerPlayable
(controllerPlayable
).The SetInputWeight()
method dynamically adjusts the blend weight of each playable.
To use the BlendClipWithController
script in your project, your project must have the following:
RequireComponent
attribute adds this component if it is not present.To use the BlendClipWithController
script in your project, follow these steps:
Add a script component to your GameObject. Name the script file BlendClipWithController.cs
and use the following code:
using UnityEngine;
using UnityEngine.Playables;
using UnityEngine.Animations;
[RequireComponent(typeof(Animator))]
public class BlendClipWithController : MonoBehaviour
{
public AnimationClip clip;
public RuntimeAnimatorController controller;
public float weight;
PlayableGraph graph;
AnimationMixerPlayable mixer;
void Start()
{
// Create the graph, the mixer, and bind them to the Animator.
graph = PlayableGraph.Create("BlendClipWithController");
mixer = AnimationMixerPlayable.Create(graph, 2);
var output = AnimationPlayableOutput.Create(graph, "Animation", GetComponent<Animator>());
output.SetSourcePlayable(mixer);
// Create playables and connect them to the mixer.
var clipPlayable = AnimationClipPlayable.Create(graph, clip);
var controllerPlayable = AnimatorControllerPlayable.Create(graph, controller);
graph.Connect(clipPlayable, 0, mixer, 0);
graph.Connect(controllerPlayable, 0, mixer, 1);
// Play the Graph.
graph.Play();
}
void Update()
{
// Clamp the weight between 0 and 1.
weight = Mathf.Clamp01(weight);
// Adjust the weight of each mixer input.
mixer.SetInputWeight(0, 1.0f-weight);
mixer.SetInputWeight(1, weight);
}
void OnDisable()
{
// Destroy all Playables and Outputs created by the graph.
graph.Destroy();
}
}
In the Script component, select the animation clip for input node 0 and the Animator Controller for input node 1.
Specify a weight for the Animator Controller in relation to the animation clip.
The script adjusts the weight of input node 0 in relation to input node 1 so that the combined weights equal 1.0. For example, a weight of 0.4 sets the Animator Controller to 40% weight and the animation clip to 60% weight.
Select Play to switch the Editor to Play mode.
In the Script component, experiment with different weights.
If you have installed the PlayableGraph Visualizer package, select BlendClipWithController
to display the PlayableGraph.
BlendClipWithController
script