Unity 스크립트 레퍼런스(API)의 CustomAnimationPlayable
클래스를 사용하여 커스텀 애니메이션 Playables를 생성할 수 있습니다.
노드가 필요할 경우 스크립트 레퍼런스(API)의 PrepareFrame
메서드에 오버로드하여 노드를 처리하시기 바랍니다.
아래 스크립트 예제에서의 목표는 애니메이션 클립을 연속해서 재생하는 것입니다. 노드 가중치를 변경하여 한번에 한 클립만 재생할 수 있으며, 클립의 로컬 타임을 조정하면 활성화하자마자 재생 가능합니다.
커스텀 플레이어블은 또한 OnSetPlayState
메서드와 OnSetTime
메서드를 사용하여 플레이어블의 상태 또는 로컬 타임 변경 시 커스텀 동작을 지정할 수 있습니다.
using UnityEngine;
using UnityEngine.Experimental.Director;
public class PlayQueuePlayable : CustomAnimationPlayable
{
public int m_CurrentClipIndex = -1;
public float m_TimeToNextClip;
public AnimationMixerPlayable m_Mixer;
public PlayQueuePlayable()
{
m_Mixer = AnimationMixerPlayable.Create();
AddInput(m_Mixer);
}
public void SetInputs(AnimationClip[] clipsToPlay)
{
foreach (AnimationClip clip in clipsToPlay)
{
m_Mixer.AddInput(AnimationClipPlayable.Create(clip));
}
}
override public void PrepareFrame(FrameData info)
{
// Advance to next clip if necessary
m_TimeToNextClip -= (float)info.deltaTime;
if (m_TimeToNextClip <= 0.0f)
{
m_CurrentClipIndex++;
if (m_CurrentClipIndex < m_Mixer.inputCount)
{
var currentClip = m_Mixer.GetInput(m_CurrentClipIndex).CastTo<AnimationClipPlayable>();
// Reset the time so that the next clip starts at the correct position
currentClip.time = 0;
m_TimeToNextClip = currentClip.clip.length;
}
else
{
// Pause when queue is complete
state = PlayState.Paused;
}
}
// Adjust the weight of the inputs
for (int a = 0; a < m_Mixer.inputCount; a++)
{
if (a == m_CurrentClipIndex)
m_Mixer.SetInputWeight(a, 1.0f);
else
m_Mixer.SetInputWeight(a, 0.0f);
}
}
}
[RequireComponent (typeof (Animator))]
public class PlayQueue : MonoBehaviour
{
public AnimationClip[] clipsToPlay;
void Start ()
{
var playQueue = Playable.Create<PlayQueuePlayable>();
playQueue.SetInputs(clipsToPlay);
// Bind the queue to the player
GetComponent<Animator>().Play(playQueue);
}
}
Unity 스크립트 레퍼런스(API)를 사용하여 플레이어블을 생성할 경우 Unity 내부적으로 해당 플레이어블과의 연결을 추적합니다. 새로운 씬이 로드됐을 때 Unity는 자동으로 모든 플레이어에 할당된 리소스를 릴리스합니다.
특히 한 씬에 있는 특정 플레이어블을 완성했을 때 Playable.Destroy()
를 호출하는 것이 좋습니다. 이 경우 Unity는 내부 리소스를 재사용할 수 있기 때문에 프로젝트의 불필요한 속도 둔화를 피할 수 있습니다.
참고: Object.Destroy()
와 유사한 방식으로 Playable.Destroy()
를 사용해야 합니다.