Tanoda
SimpleAnimationPlayer.cs
Go to the documentation of this file.
1using System.Collections.Generic;
2using UnityEngine;
3using UnityEngine.Animations;
4using UnityEngine.Playables;
5
7{
8
10 public class SimpleAnimationPlayer : MonoBehaviour
11 {
15 public IList<AnimationClip> AnimationClips;
16
17 private PlayableGraph _playableGraph;
18 private AnimationPlayableOutput _playableOutput;
19 private AnimationClipPlayable _clipPlayable;
20 private Animator _animator;
21
22 private void Awake()
23 {
24 _animator = GetComponent<Animator>();
25 }
26
27 private void OnDestroy()
28 {
29 if (_playableGraph.IsValid())
30 {
31 _playableGraph.Destroy();
32 }
33 }
34
37 public void PlayAnimation(int index)
38 {
39 if (_animator == null || AnimationClips == null || index < 0 || index >= AnimationClips.Count)
40 {
41 return;
42 }
43 var animationClip = AnimationClips[index];
44 if (_clipPlayable.IsValid())
45 {
46 _clipPlayable.Destroy();
47 }
48 _clipPlayable = AnimationPlayableUtilities.PlayClip(_animator, animationClip, out _playableGraph);
49 _clipPlayable.SetApplyFootIK(false);
50 _clipPlayable.SetApplyPlayableIK(false);
51 }
52
55 public void PlayAnimation(string name)
56 {
57 if (_animator == null || AnimationClips == null)
58 {
59 return;
60 }
61 for (var i = 0; i < AnimationClips.Count; i++)
62 {
63 var animationClip = AnimationClips[i];
64 if (animationClip.name == name)
65 {
67 }
68 }
69 }
70 }
71}
Represents a Playable used to play Animations from its Animation Clip List using names or indices as ...
IList< AnimationClip > AnimationClips
Source animation clips.
void PlayAnimation(string name)
Plays the Animation Clip with the given index.
void PlayAnimation(int index)
Plays the Animation Clip with the given index.