Search Unity

AnimationLayerMixerPlayable with multiple ScriptPlayable inputs

Discussion in 'Animation' started by ABouenard, Nov 10, 2017.

  1. ABouenard

    ABouenard

    Joined:
    Nov 10, 2017
    Posts:
    11
    Hi there!

    I'm trying to use the Playable API, and more specifically mix the result of multiple ScriptPlayable with an AnimationLayerMixerPlayable.

    It unfortunately works partially, in the sense that only the first ScriptPlayable seems to be evaluated, and other ScriptPlayable have just no effect.

    Any idea? Thanks!

    Code (CSharp):
    1. using System.Collections.Generic;
    2. using UnityEngine;
    3. using UnityEngine.Animations;
    4. using UnityEngine.Playables;
    5.  
    6. public class MyPlayable : PlayableBehaviour
    7. {
    8.     string name = "";
    9.     AnimationClip clip;
    10.     AnimationClipPlayable clipPlayable;
    11.     Playable mixerPlayable;
    12.  
    13.     public void Initialize(string playableName, Playable scriptPlayableOwner, PlayableGraph playableGraph, AnimationClip animationClip)
    14.     {
    15.         name = playableName;
    16.  
    17.         // Create playable animation-clip.
    18.         clip = animationClip;
    19.         clipPlayable = AnimationClipPlayable.Create(playableGraph, clip);
    20.  
    21.         // Create playable mixer, and add playable animation-clip to mixer inputs.
    22.         mixerPlayable = AnimationMixerPlayable.Create(playableGraph);
    23.         mixerPlayable.AddInput(clipPlayable, 0);
    24.         // Configure mixer input.
    25.         mixerPlayable.SetInputWeight(0, 1.0f);
    26.  
    27.         // Add playable mixer to playable-script inputs.
    28.         scriptPlayableOwner.AddInput(mixerPlayable, 0);
    29.         // Configure playable-script input.
    30.         scriptPlayableOwner.SetInputWeight(0, 1.0f);
    31.     }
    32.  
    33.     public override void PrepareFrame(Playable owner, FrameData info)
    34.     {
    35.         Debug.Log("MyPlayable.PrepareFrame - " + name);
    36.     }
    37. }
    38.  
    39. class MyBehaviour : MonoBehaviour
    40. {
    41.     public AnimationClip[] clips;
    42.  
    43.     PlayableGraph playableGraph;
    44.     AnimationLayerMixerPlayable layerMixerPlayable;
    45.     List<ScriptPlayable<MyPlayable>> scriptPlayables = new List<ScriptPlayable<MyPlayable>>();
    46.     AnimationPlayableOutput playableOutput;
    47.  
    48.     void Start()
    49.     {
    50.         InitializeGraph(clips);
    51.     }
    52.  
    53.     void InitializeGraph(AnimationClip[] clips)
    54.     {
    55.         // Create the graph.
    56.         playableGraph = PlayableGraph.Create();
    57.         GraphVisualizerClient.Show(playableGraph, "MyPlayableGraph");
    58.  
    59.         // Create the layer mixer that will mix multiple ScriptPlayable<MyPlayable> outputs.
    60.         layerMixerPlayable = AnimationLayerMixerPlayable.Create(playableGraph);
    61.  
    62.         // Create a ScriptPlayable<MyPlayable> for each clip.
    63.         foreach (AnimationClip clip in clips)
    64.         {
    65.             // Create ScriptPlayable of MyPlayable.
    66.             ScriptPlayable<MyPlayable> myPlayableScript = ScriptPlayable<MyPlayable>.Create(playableGraph);
    67.             MyPlayable myPlayable = myPlayableScript.GetBehaviour();
    68.             string myPlayableName = "MyPlayable" + layerMixerPlayable.GetInputCount().ToString();
    69.             myPlayable.Initialize(myPlayableName, myPlayableScript, playableGraph, clip);
    70.             scriptPlayables.Add(myPlayableScript);
    71.  
    72.             // Add script-playable to mixer inputs.
    73.             layerMixerPlayable.AddInput(myPlayableScript, 0);
    74.  
    75.             // Configure mixer input.
    76.             int playableIndex = layerMixerPlayable.GetInputCount() - 1;
    77.             layerMixerPlayable.SetInputWeight(playableIndex, 1.0f);
    78.             layerMixerPlayable.SetLayerAdditive((uint)playableIndex, true);
    79.         }
    80.  
    81.         // Create output, and assign the mixer to its source.
    82.         playableOutput = AnimationPlayableOutput.Create(playableGraph, "Animation", gameObject.GetComponent<Animator>());
    83.         playableOutput.SetSourcePlayable(layerMixerPlayable);
    84.  
    85.         // Play the graph.
    86.         playableGraph.Play();
    87.     }
    88.  
    89.     void OnDestroy()
    90.     {
    91.         scriptPlayables.Clear();
    92.         playableGraph.Destroy();  
    93.     }
    94. }
    95.  
     
  2. mangax

    mangax

    Joined:
    Jul 17, 2013
    Posts:
    336
    i have this same issue when i tried to plug multiple animation clips inputs into scriptplayable then into layermixer.. only fist input animation go through from each scriptable playable.. the rest of inputs are not evaluated although they show playing when inspecting them.

    Can someone confirm if this is an intended design or a bug?
    or if there is a right way to plug multiple inputs and mix them in ScriptPlayables?