본문 바로가기
Works/Unity 3D

[Unity] Playables API

by Vader87 2020. 9. 22.
반응형

Playables API

 

Unity - Manual: Playables API

Playables API The Playables API provides a way to create tools, effects or other gameplay mechanisms by organizing and evaluating data sources in a tree-like structure known as the PlayableGraph. The PlayableGraph allows you to mix, blend, and modify multi

docs.unity3d.com

  • Unity 에서 Timeline 을 만드는데 사용
  • PlayableGraph Visualizer 와 함께 사용하면 시각적으로 디버깅 가능

Playable API 에는 다음 3가지 주요 기능이 사용된다.

  • PlayableGraph
    • 기본 Playable 그룹, PlayableOutput 과 Playable 을 관리한다.
  • PlayableOutput
    • 그래프의 마지막 노드
    • AnimationPlayableOutput, AUdioPlayableOutput 등 여러 종류가 있다.
  • Playable
    • 실제 동작이 이루어 지는 사용자 정의 가능한 런타임 개채
    • AnimationClipPlayable, AnimaionMixerPlayable, AudioClipPlayable, ScriptPlayable 등 여러 종류가 있다.

Creating an animation blend tree 예제를 통한 기능 설명

using UnityEngine;
using UnityEngine.Playables;
using UnityEngine.Animations;

[RequireComponent(typeof(Animator))]
public class MixAnimationSample : MonoBehaviour
{

    public AnimationClip clip0;
    public AnimationClip clip1;
    public float weight;
    PlayableGraph playableGraph;
    AnimationMixerPlayable mixerPlayable;

    void Start()
    {
        // Creates the graph, the mixer and binds them to the Animator.
        playableGraph = PlayableGraph.Create();
        var playableOutput = AnimationPlayableOutput.Create(playableGraph, "Animation", GetComponent<Animator>());
        mixerPlayable = AnimationMixerPlayable.Create(playableGraph, 2);
        playableOutput.SetSourcePlayable(mixerPlayable);
        
        // Creates AnimationClipPlayable and connects them to the mixer.
        var clipPlayable0 = AnimationClipPlayable.Create(playableGraph, clip0);
        var clipPlayable1 = AnimationClipPlayable.Create(playableGraph, clip1);

        playableGraph.Connect(clipPlayable0, 0, mixerPlayable, 0);
        playableGraph.Connect(clipPlayable1, 0, mixerPlayable, 1);

        // Plays the Graph.
        playableGraph.Play();
    }

    void Update()
    {
        weight = Mathf.Clamp01(weight);
        
        mixerPlayable.SetInputWeight(0, 1.0f-weight);
        mixerPlayable.SetInputWeight(1, weight);
    }

    void OnDisable()
    {
        // Destroys all Playables and Outputs created by the graph.
        playableGraph.Destroy();
    }
}

MixAnimationSample 은 weight 값 설정에 따라 clip0 과 clip1 의 애니메이션을 블랜드해 출력하는 예제 이다.

예제에서 PlayableGraph 는 Start 에 재생 시작해서 Disable 될때 Destroy 된다.

위 코드를 에디터에서 Play 시키고 PlayableGraph Visualizer 를 통해 확인해 보자.

PlayableGraph Visualizer 의 설치 및 사용 방법에 대해선 다음 링크에 간략히 정리해 두었다.

ukprog.tistory.com/88

 

[Unity] PlayableGraph Visualizer

PlayableGraph Visualizer Setup Unity Editor > Windows > Package Manager > Add package from git URL Git URL: https://github.com/Unity-Technologies/graph-visualizer.git Package Manager > In Project (..

ukprog.tistory.com

우선 실행시키고 창을 키면 다음과 같이 나온다.

1. Animation Output

그래프의 마지막 값이다

playableGraph = PlayableGraph.Create();
var playableOutput = AnimationPlayableOutput.Create(playableGraph, "Animation", GetComponent<Animator>());

2. Animation Mixer

코드에서 Output 의 SourcePlayable 로 설정해 Animation Output 과 연결되어 있고 Create 설정 값 처럼 2개의 Input 이 존재한다.

mixerPlayable = AnimationMixerPlayable.Create(playableGraph, 2);
playableOutput.SetSourcePlayable(mixerPlayable);

3, 4. AnimationClip

AnimationClipPlayable 들이며 코드에서 Connect 를 통해 AnimationMixer 와 연결해 주었기 때문에 그래프 상에도 연결된 것으로 표기되고 있다. clip0 은 Animation Mixer의 0번 Input 과 clip1 은 1번 Input 과 연결 되어 있다.

 

var clipPlayable0 = AnimationClipPlayable.Create(playableGraph, clip0);
var clipPlayable1 = AnimationClipPlayable.Create(playableGraph, clip1);

playableGraph.Connect(clipPlayable0, 0, mixerPlayable, 0);
playableGraph.Connect(clipPlayable1, 0, mixerPlayable, 1);

창 왼쪽 상단의 Unnamed  는 예제의 PlayableGraph 를 의미한다. 생성시 이름을 별도로 지정하지 않아 자동으로 Unnamed 로 된 것이다.

Animation Clip Node를 하나 선택해 보면 상세 정보가 나오는데 여기에서 Clip_A 라는 Animation Clip 을 사용중 임을 알 수 있다. 이런식으로 각 Node 가 어떤 기능을 하는지는 해당 Node 선택 후 상세보기를 통해 알 수 있다.

위에서는 Weight 값이 0이기 때문에 그래프가 위와 연결되는 것으로 나온다. Weight 값과 연결 관계는 아래 코드 값이다. Animation Mixer 에 0번 Input 은 Weight 값이 0 이면 1, 1이면 0이고, 1번 Input 은 그 반대 값이다.

mixerPlayable.SetInputWeight(0, 1.0f - weight);
mixerPlayable.SetInputWeight(1, weight);

 

Weight 값을 1로 변경하면 그래프가 다음과 같이 아래와 연결되는 것으로 변경되고 실제 아래 Node 에 연결된 Animation Clip 이 재생된다.

ScriptPlayable

PlayableBehaviour Cycle

 

반응형

'Works > Unity 3D' 카테고리의 다른 글

[Unity] Firebase Crashlytics  (0) 2020.09.22
[Unity] PlayableGraph Visualizer  (0) 2020.09.22
[Unity] OpenUPM 사용법  (0) 2020.05.30
[Unity] 최적화에 관한 정보들  (0) 2020.05.06
[Unity3D] RenderTexture RawImage 출력 이슈  (1) 2020.02.13

댓글