123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- using UnityEngine;
- using UnityEngine.Animations;
- using UnityEngine.Playables;
- namespace TriLibCore.Playables
- {
- /// <summary>Represents a Playable used to play Animations from its Animation Clip List using names or indices as parameters.</summary>
- public class SimpleAnimationPlayer : MonoBehaviour
- {
- /// <summary>
- /// Source animation clips.
- /// </summary>
- public AnimationClip[] AnimationClips;
- private PlayableGraph _playableGraph;
- private AnimationPlayableOutput _playableOutput;
- private AnimationClipPlayable _clipPlayable;
- private Animator _animator;
- private void Awake()
- {
- _animator = GetComponent<Animator>();
- }
- private void OnDestroy()
- {
- if (_playableGraph.IsValid())
- {
- _playableGraph.Destroy();
- }
- }
- /// <summary>Plays the Animation Clip with the given index.</summary>
- /// <param name="index">The Animation Clip index.</param>
- public void PlayAnimation(int index)
- {
- if (_animator == null || AnimationClips == null || index < 0 || index >= AnimationClips.Length)
- {
- return;
- }
- var animationClip = AnimationClips[index];
- if (_clipPlayable.IsValid())
- {
- _clipPlayable.Destroy();
- }
- _clipPlayable = AnimationPlayableUtilities.PlayClip(_animator, animationClip, out _playableGraph);
- _clipPlayable.SetApplyFootIK(false);
- _clipPlayable.SetApplyPlayableIK(false);
- }
- /// <summary>Plays the Animation Clip with the given index.</summary>
- /// <param name="name">The Animation Clip name.</param>
- public void PlayAnimation(string name)
- {
- if (_animator == null || AnimationClips == null)
- {
- return;
- }
- for (var i = 0; i < AnimationClips.Length; i++)
- {
- var animationClip = AnimationClips[i];
- if (animationClip.name == name)
- {
- PlayAnimation(i);
- }
- }
- }
- }
- }
|