AgeTDev
September 19, 2016, 8:44am
1
Hello,
Has anyone yet tried to extract all sprites that are used in a 2D Animator ? My goal is to do this in the Editor, by code. Anyone got a suggestion on how to do this ?
I jeed all the sprites used by all the animations inside a single Animator
Thanks,
T
AgeTDev
September 19, 2016, 2:29pm
2
Okay i figured out how to do it, so if anyone here is interested in this , there you go :
#if UNITY_EDITOR
public static List<Sprite> GetSpritesFromAnimator(Animator anim)
{
List<Sprite> _allSprites = new List<Sprite> ();
foreach(AnimationClip ac in anim.runtimeAnimatorController.animationClips)
{
_allSprites.AddRange(GetSpritesFromClip(ac));
}
return _allSprites;
}
private static List<Sprite> GetSpritesFromClip(AnimationClip clip)
{
var _sprites = new List<Sprite> ();
if (clip != null)
{
foreach (var binding in AnimationUtility.GetObjectReferenceCurveBindings (clip))
{
ObjectReferenceKeyframe[] keyframes = AnimationUtility.GetObjectReferenceCurve (clip, binding);
foreach (var frame in keyframes) {
_sprites.Add ((Sprite)frame.value);
}
}
}
return _sprites;
}
#endif
And then you can simply call GetSpritesFromAnimator(_anim)
with your animator of choice as argument and you will get a list of Sprites back