Preview Anim

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
using UnityEngine.Playables;
using UnityEngine;
using EasyButtons;
using UnityEngine.Animations;
using Network.Proto;
using UnityEditor;
using Unity.Entities.UniversalDelegates;
using System.Collections.Generic;

[ExecuteInEditMode]
public class SimpleAnimationPlayer : MonoBehaviour
{
private PlayableGraph _graph;
private AnimationClipPlayable _Playable;
public IList<AnimationClip> clips;
private Animator _animator;
private AnimationClipPlayable _clipPlayable;

private void Start()
{
_animator = GetComponent<Animator>();
clips = _animator.runtimeAnimatorController.animationClips;
Debug.Log(clips);
}

public void StopPreview()
{
if (_graph.IsValid())
{
_graph.Destroy();
}
}

public void PlayAnimation(int index)
{
if (_animator == null || clips == null || index < 0 || index >= clips.Count)
{
return;
}
var animationClip = clips[index];
if (_clipPlayable.IsValid())
{
_clipPlayable.Destroy();
}
if (_graph.IsValid())
{
_graph.Destroy();
}
_clipPlayable = AnimationPlayableUtilities.PlayClip(_animator, animationClip, out _graph);
}

}
[CustomEditor(typeof(SimpleAnimationPlayer))]
public class SimpleAnimationPlayerEditor : UnityEditor.Editor
{
Vector2 scrollPosition;
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
var simpleAnimationPlayer = (SimpleAnimationPlayer)target;
if (simpleAnimationPlayer.clips != null)
{
scrollPosition = GUILayout.BeginScrollView(scrollPosition);

for (var i = 0; i < simpleAnimationPlayer.clips.Count; i++)
{
var animationClip = simpleAnimationPlayer.clips[i];
if (animationClip != null && GUILayout.Button(animationClip.name))
{
simpleAnimationPlayer.PlayAnimation(i);
}
}
GUILayout.EndScrollView();
}
GUILayout.Space(5);
if (GUILayout.Button("Stop Preview", GUILayout.Height(25)))
{
simpleAnimationPlayer.StopPreview();
}


}
}