1
\$\begingroup\$

I'm working on a 2D game, using spritesheets as the basis for four AnimationClips. (Every frame from the spritesheet is a keyframe in the animation.) I'm trying to step through the frames of these AnimationClips and programmatically create Collider2Ds which fit the dimensions of each frame.

The problem is, I'm having an impossible time actually iterating through the frames. My character has an Animator component, with 4 different states. I've tried various inputs to my editor script, but the closest I've gotten has an AnimationClip as input:

private AnimationClip clip; // set in inspector
private GameObject targetForColliders; // set in inspector

void createColliders() {
    Animation animation = new Animation();
    AnimationUtility.SetAnimationClips(animation, new AnimationClip[] { clip });

    foreach (AnimationState state in animation) {
        // do useful things here
    }
}

Unfortunately, somehow the animation local variable is null, crazy as that sounds. I've confirmed that by both logging it and attempting to use it, which causes a NullReferenceException every time.

I can't access the Sprite containing the spritesheet directly, because I've made manual adjustments within Unity to the keyframes (removing some, duplicating others) and I don't want to have to manually sync my colliders with the modified assets.

Has anyone attempted to do something similar to this before? tl;dr: What's the easiest way to iterate through the frames of a 2D animation in an editor script?

\$\endgroup\$
1
\$\begingroup\$

If you want more control over when the sprite frame changes, you should just use a script to change the object sprite instead of using an animation. Then you would have an array of Sprites and an array of corresponding Collider2Ds set via serialization. The script could flip through the frames however you see fit.

\$\endgroup\$
  • 1
    \$\begingroup\$ I don't want control over the sprite frame changing, though. I'm perfectly happy with everything Unity gives me for 2D animation capability. I just want a way to iterate through the frames. At this point I'm pretty close to just going through the sprites individually to do it. \$\endgroup\$ – Chris Hayes Sep 7 '14 at 4:09
0
\$\begingroup\$

You don't need to add collider to each frame. You add the collider to your character.This video should clarify using sprites for animation. It is one hour, 19 minute session dealing with 2D character control and animating the character for idle,run and jump actions.

\$\endgroup\$
  • 1
    \$\begingroup\$ I'm familiar with the video. It's not really applicable here. Animating the sprite is not the problem; pulling out the frames of the animation so that I can automate creating colliders which accurately depict each frame is the goal. Unlike the video, my sprites undergo significant deformation, and just using one collider for the character is going to work poorly when certain animations are playing. \$\endgroup\$ – Chris Hayes Sep 7 '14 at 7:18
0
\$\begingroup\$

You don't need to create different collider 2D for each frame , it might be performance heavy. Add a new property to the animation clip i.e. "Box Collider 2D - Offset" and "Box Collider 2D - Size" , now your animation clip will be having three properties one sprite and other offset and size. Now change the size of the collider and offset for key frames in the animation where it is needed to fit the bounds of the sprite.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.