El Unity Script Reference (API) proporciona una manera de crear herramientas, efectos y otros mecanismos de gameplay al organizar y evaluar fuentes de datos en una estructura parecida a un árbol. Esta estructura parecida a un árbol le permite a usted mezclar, fusionar, y modificar varias fuentes de datos y reproducirlas a través de un solo output. El Playable API soporta gráficas de animación, por lo que puede interactuar con la animación vía scripting.
Tenga en cuenta que la Playable API es parte del namespace “experimental”, y los scripts creados con ella podrían no ser compatible con versiones futuras de Unity.
Si usted ha utilizado Playable API en versiones antes de 5.4, usted debería revisar la Guía de Actualización para detalles acerca de cómo migrar a 5.4.
El siguiente MonoBehaviour crea un simple árbol con un solo nodo, que reproduce un solo clip.
El AnimationClipPlayable
envuelve un AnimationClip
para hacerlo compatible con la Playable API.
La operación Play()
conecta el Playable con el Animator para que la raíz del gráfico (en este caso el AnimationClipPlayable
) se pueda reproducir.
using UnityEngine;
using UnityEngine.Experimental.Director;
[RequireComponent (typeof (Animator))]
public class PlayAnimation : MonoBehaviour
{
public AnimationClip clip;
void Start ()
{
// Wrap the clip in a playable
var clipPlayable = AnimationClipPlayable.Create(clip);
// Bind the playable to the player
GetComponent<Animator>().Play(clipPlayable);
}
}
El AnimationMixerPlayable
mezcla dos o más AnimationClipPlayable
s, o puede fusionar otros mezcladores, que por sí solos fusionan otros clips, etc. El método SetInputs()
crea los nodos AnimationClipPlayable
subyacentes implícitamente (este método también puede tomar un arreglo de AnimationPlayable
s directamente).
El peso de cada clip en la fusión se ajusta dinámicamente mediante SetInputWeight()
, creando un árbol de fusión que suavemente cambie de una animación a otra en el tiempo.
using UnityEngine;
using UnityEngine.Experimental.Director;
[RequireComponent (typeof (Animator))]
public class MixAnimation : MonoBehaviour
{
public AnimationClip clip1;
public AnimationClip clip2;
private AnimationMixerPlayable m_Mixer;
void Start ()
{
// Create the mixer and connect it to clips
// (AnimationClipPlayables are created implicitly)
m_Mixer = AnimationMixerPlayable.Create();
m_Mixer.SetInputs(new[] {clip1, clip2});
// Bind the playable graph to the player
GetComponent<Animator>().Play(m_Mixer);
}
void Update()
{
// Adjust the weight of each clip in the blend tree based on time
float weight = (Time.time % 10) / 10;
m_Mixer.SetInputWeight(0, weight);
m_Mixer.SetInputWeight(1, 1 - weight);
}
}
Tal como AnimationClipPlayable envuelve un AnimationClip, el AnimatorControllerPlayable
envuelve un AnimatorController
para que pueda fusionarlos con otros AnimationPlayables.
using UnityEngine;
using UnityEngine.Experimental.Director;
[RequireComponent (typeof (Animator))]
public class BlendAnimatorController : MonoBehaviour
{
public AnimationClip clip;
public RuntimeAnimatorController animController;
void Start ()
{
// Wrap the clip and the controller in playables
var clipPlayable = AnimationClipPlayable.Create(clip);
var controllerPlayable = AnimatorControllerPlayable.Create(animController);
var mixer = AnimationMixerPlayable.Create();
mixer.SetInputs(new Playable[] {clipPlayable, controllerPlayable});
// Bind the playable graph to the player
GetComponent<Animator>().Play(mixer);
}
}
Por defecto, el método Play()
en PlayableController
maneja todo el tiempo de la reproducción del árbol. No obstante, para un control adicional, usted puede explícitamente configurar el tiempo local de un Playable
. Este tiempo local será pasado a los nodos hijos.
En este ejemplo, usted puede pausar la reproducción, y avanzar y rebobinar la animación manualmente utilizando las flechas del teclado.
using UnityEngine;
using UnityEngine.Experimental.Director;
[RequireComponent (typeof (Animator))]
public class PlayWithTimeControl : MonoBehaviour
{
public AnimationClip clip;
private Playable m_Root;
private const float k_SpeedFactor = 1f;
void Start ()
{
m_Root = AnimationClipPlayable.Create(clip);
// Bind the playable to the player
GetComponent<Animator>().Play(m_Root);
m_Root.state = PlayState.Paused;
}
void Update ()
{
// Control the time manually based on the input
float horizontalInput = Input.GetAxis("Horizontal");
m_Root.time += horizontalInput * k_SpeedFactor * Time.deltaTime;
}
}
Usted puede configurar el estado del árbol, o una rama del árbol, al cambiar el parámetro Playable.state
. Este estado pasará todos los hijos del nodo, sin importar su estado previo. Si un nodo hijo explícitamente fue pausado, configurar el padre a un estado Playing también configurará el hijo a un estado Playing.