I'm working on a 2D game, using spritesheets as the basis for four AnimationClip
s. (Every frame from the spritesheet is a keyframe in the animation.) I'm trying to step through the frames of these AnimationClip
s and programmatically create Collider2D
s 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?