- Home /
How to get all Sprites used in a 2D Animator?
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
How about putting all the sprites in a folder and then you can get all of the sprites from it?
because that is not the way i want to do it :P
Answer by AgeTDev · Sep 19, 2016 at 02:29 PM
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 :)
Your answer
Follow this Question
Related Questions
2D Animation does not start 1 Answer
After animation completion transition to default animation 1 Answer
Animator warning in editor, animation is preventing script from working 1 Answer
Setting animator parameter on a single instance of a prefab sets the parameter for all instances 3 Answers
Object not moving after changing animator parameter 1 Answer