-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommon.SkeletonAnimation.cs
179 lines (156 loc) · 7.07 KB
/
Common.SkeletonAnimation.cs
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
using System;
using System.Collections.Generic;
using System.Linq;
#if VIRTUESKY_SKELETON
using Spine;
using Spine.Unity;
using UnityEngine;
using VirtueSky.Core;
namespace VirtueSky.Misc
{
public static partial class Common
{
public static float Duration(this SkeletonAnimation skeletonAnimation, string animationName)
{
var animation =
skeletonAnimation.AnimationState.Data.SkeletonData.Animations.Items.FirstOrDefault(animation =>
animation.Name.Equals(animationName));
if (animation == null) return 0;
return animation.Duration;
}
public static float Duration(this SkeletonAnimation skeletonAnimation, int track = 0)
{
var animation = skeletonAnimation.AnimationState.GetCurrent(track);
if (animation == null) return 0;
return animation.Animation.Duration;
}
public static SkeletonAnimation OnComplete(this SkeletonAnimation skeletonAnimation, Action onComplete,
int trackIndex = 0, MonoBehaviour target = null)
{
App.Delay(target, skeletonAnimation.Duration(trackIndex), () =>
{
if (skeletonAnimation != null)
{
onComplete?.Invoke();
}
});
return skeletonAnimation;
}
public static SkeletonAnimation OnUpdate(this SkeletonAnimation skeletonAnimation, Action<float> onUpdate,
int trackIndex = 0, MonoBehaviour target = null)
{
App.Delay(target, skeletonAnimation.Duration(trackIndex), null, onUpdate);
return skeletonAnimation;
}
public static TrackEntry OnComplete(this TrackEntry trackEntry, Action onComplete)
{
trackEntry.Complete += HandleComplete;
void HandleComplete(TrackEntry track)
{
trackEntry.Complete -= HandleComplete;
onComplete?.Invoke();
}
return trackEntry;
}
public static SkeletonAnimation Play(this SkeletonAnimation skeletonAnimation, string animationName,
bool loop = false, int trackIndex = 0)
{
skeletonAnimation.ClearState();
skeletonAnimation.AnimationName = animationName;
skeletonAnimation.loop = loop;
skeletonAnimation.AnimationState.SetAnimation(trackIndex, animationName, loop);
skeletonAnimation.LateUpdate();
skeletonAnimation.Initialize(true);
return skeletonAnimation;
}
public static SkeletonAnimation PlayOnly(this SkeletonAnimation skeletonAnimation, string animationName,
bool loop = false, int trackIndex = 0)
{
skeletonAnimation.AnimationName = animationName;
skeletonAnimation.AnimationState.SetAnimation(trackIndex, animationName, loop);
return skeletonAnimation;
}
public static SkeletonAnimation AddAnimation(this SkeletonAnimation skeletonAnimation, int trackIndex,
string animationName, bool loop, float timeDelay = 0f, float mixDuration = 0f)
{
var track = skeletonAnimation.AnimationState.AddAnimation(trackIndex, animationName, loop, timeDelay);
track.MixDuration = mixDuration;
return skeletonAnimation;
}
public static SkeletonAnimation SetSkin(this SkeletonAnimation skeletonAnimation, string skinName)
{
var skin = new Skin("temp");
skin.AddSkin(skeletonAnimation.skeleton.Data.FindSkin(skinName));
skeletonAnimation.initialSkinName = "temp";
skeletonAnimation.skeleton.SetSkin(skin);
skeletonAnimation.skeleton.SetSlotsToSetupPose();
skeletonAnimation.LateUpdate();
skeletonAnimation.AnimationState.Apply(skeletonAnimation.skeleton);
return skeletonAnimation;
}
public static SkeletonAnimation SetSkin(this SkeletonAnimation skeletonAnimation, List<string> skinNames)
{
var skin = new Skin("temp");
var skeletonData = skeletonAnimation.Skeleton.Data;
foreach (string skinName in skinNames)
{
skin.AddSkin(skeletonData.FindSkin(skinName));
}
skeletonAnimation.initialSkinName = "temp";
skeletonAnimation.skeleton.SetSkin(skin);
skeletonAnimation.skeleton.SetSlotsToSetupPose();
skeletonAnimation.LateUpdate();
skeletonAnimation.AnimationState.Apply(skeletonAnimation.skeleton);
return skeletonAnimation;
}
public static SkeletonAnimation ChangeAttachment(this SkeletonAnimation skeletonAnimation, string slotName,
string attachmentName)
{
var slotIndex = skeletonAnimation.skeleton.Data.FindSlot(slotName).Index;
var attachment = skeletonAnimation.skeleton.GetAttachment(slotIndex, attachmentName);
var skin = new Skin("temp");
skin.SetAttachment(slotIndex, slotName, attachment);
skeletonAnimation.skeleton.SetSkin(skin);
skeletonAnimation.skeleton.SetSlotsToSetupPose();
skeletonAnimation.LateUpdate();
return skeletonAnimation;
}
public static SkeletonAnimation ChangeAttachment(this SkeletonAnimation skeletonAnimation, string slotName,
List<string> attachmentNames)
{
var slotIndex = skeletonAnimation.skeleton.Data.FindSlot(slotName).Index;
var skin = new Skin("temp");
foreach (var attachmentName in attachmentNames)
{
var attachment = skeletonAnimation.skeleton.GetAttachment(slotIndex, attachmentName);
skin.SetAttachment(slotIndex, slotName, attachment);
}
skeletonAnimation.skeleton.SetSkin(skin);
skeletonAnimation.skeleton.SetSlotsToSetupPose();
skeletonAnimation.LateUpdate();
return skeletonAnimation;
}
public static SkeletonAnimation MixSkin(this SkeletonAnimation skeletonAnimation, string mixSkinName)
{
Skin skin = new Skin("temp");
skin.AddSkin(skeletonAnimation.Skeleton.Data.FindSkin(mixSkinName));
skeletonAnimation.Skeleton.SetSkin(skin);
skeletonAnimation.Skeleton.SetSlotsToSetupPose();
skeletonAnimation.AnimationState.Apply(skeletonAnimation.Skeleton);
return skeletonAnimation;
}
public static SkeletonAnimation MixSkin(this SkeletonAnimation skeletonAnimation, List<string> mixSkinNames)
{
Skin skin = new Skin("temp");
foreach (var mixSkinName in mixSkinNames)
{
skin.AddSkin(skeletonAnimation.Skeleton.Data.FindSkin(mixSkinName));
}
skeletonAnimation.Skeleton.SetSkin(skin);
skeletonAnimation.Skeleton.SetSlotsToSetupPose();
skeletonAnimation.AnimationState.Apply(skeletonAnimation.Skeleton);
return skeletonAnimation;
}
}
}
#endif