Instantly share code, notes, and snippets.

Embed
What would you like to do?
Timelineで動的なバインド (トラック用)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Playables;
using System.Linq;
public class BindObjectOnDemand : MonoBehaviour
{
[SerializeField] Animator character;
[SerializeField] PlayableDirector director;
Coroutine coroutine;
public IEnumerator PlayAnimation ()
{
// キャラクターを生成して、Animatorを取得
var instantiateCharacter = GameObject.Instantiate (character.gameObject).GetComponent<Animator> ();
// 取ってきたAnimatorをPlayableDirectorにバインド
Bind(instantiateCharacter);
// 再生して、再生終了後に生成したキャラクターを殺す
director.Play ();
yield return new WaitUntil (() => director.playableGraph.isDone);
coroutine = null;
Destroy (instantiateCharacter.gameObject);
}
void Bind(Animator animator)
{
// TimelineからCharacterAnimationTrackのトラックへの参照を取得して
// 生成したAnimator情報を流し込む
var binding = director.playableAsset.outputs.First (c => c.streamName == "CharacterAnimationTrack");
director.SetGenericBinding (binding.sourceObject, animator);
}
public void Play ()
{
if (coroutine == null)
coroutine = StartCoroutine (PlayAnimation ());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment