Search Unity

Preview Playable in editor

Discussion in 'Animation' started by Baste, Sep 21, 2017.

  1. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    Hi all!

    I'm trying to cobble together an animation system built on Playables, and I've hit a snag on trying to preview blend trees and transitions.

    For a single clip, it's relatively easy to show a preview in the editor; simply use the AnimationMode API. That doesn't allow me to preview ie. a blend between an idle and walk animation, though.

    I've tried to make a Playable graph as I do runtime, and manually update it's time, but that doesn't seem to have any effect in the editor, the model is stuck in it's bind pose.

    I know this is possible internally, as the Animator shows this kind of previews. I just need to figure out if it can be done with the Playable system.
     
    ModLunar likes this.
  2. Mecanim-Dev

    Mecanim-Dev

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    Hi baste,

    Can you show us what you did try exactly?
    I don't know if you did try this but I would use the animator component to actually update the scene, so basically you manually update the time of your playable and then call Animator.Update(0) to write the result on the scene transform.
     
    ModLunar likes this.
  3. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    Hi!

    My bad, actually. I went back to try calling Animator.Update, and realized that I hadn't set the weight on any of my
    AnimationMixerPlayable. Whoops!

    Calling Animator.Update is not necessary, I just need to call graph.Evaluate on a graph that's got the Animator as a target. Here's the working code:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.Animations;
    4. using UnityEngine.Playables;
    5. #if UNITY_EDITOR
    6. using UnityEditor;
    7. #endif
    8.  
    9. public class RuntimePlayable : MonoBehaviour {
    10.  
    11.    public AnimationClip clipA;
    12.    public AnimationClip clipB;
    13.  
    14.    private PlayableGraph graph;
    15.    private BlenderPlayableBehaviour blendBehaviour;
    16.  
    17.    public void StartPlaying () {
    18.       graph = PlayableGraph.Create();
    19.  
    20.       //ensureComponent adds the component if it can't be found
    21.       var animOutput = AnimationPlayableOutput.Create(graph, "AnimationOutput", gameObject.EnsureComponent<Animator>());
    22.  
    23.       var mixerPlayable = AnimationMixerPlayable.Create(graph, 2, true);
    24.  
    25.       var clipPlayableA = AnimationClipPlayable.Create(graph, clipA);
    26.       var clipPlayableB = AnimationClipPlayable.Create(graph, clipB);
    27.    
    28.       graph.Connect(clipPlayableA, 0, mixerPlayable, 0);
    29.       graph.Connect(clipPlayableB, 0, mixerPlayable, 1);
    30.    
    31.       mixerPlayable.SetInputWeight(0, .5f);
    32.       mixerPlayable.SetInputWeight(1, .5f);
    33.  
    34.    
    35.       animOutput.SetSourcePlayable(mixerPlayable);
    36.  
    37.       graph.Play();
    38.    }
    39.  
    40.    public void Tick()
    41.    {
    42.       graph.Evaluate(.1f);
    43.    }
    44.  
    45.    private void OnDestroy()
    46.    {
    47.       graph.Destroy();
    48.    }
    49. }
    50.  
    51. #if UNITY_EDITOR
    52. [CustomEditor(typeof(RuntimePlayable))]
    53. public class RuntimePlayableEditor : Editor {
    54.  
    55.    private RuntimePlayable script;
    56.  
    57.    void OnEnable() {
    58.       script = (RuntimePlayable) target;
    59.    }
    60.  
    61.    public override void OnInspectorGUI() {
    62.       base.OnInspectorGUI();
    63.  
    64.       if (GUILayout.Button("Start"))
    65.       {
    66.          script.StartPlaying();
    67.       }
    68.  
    69.       if (GUILayout.Button("Tick"))
    70.       {
    71.          script.Tick();
    72.       }
    73.    }
    74.  
    75. }
    76. #endif


    By the way, the AnimationMode API's example states:
    That's no longer the case, as I found out when testing it. I guess you fixed that bug! So that part can be removed from the docs.
     
    ModLunar and Mecanim-Dev like this.