Evenly distributing sprites in an animation

Is there a way to evenly distribute sprites along the timeline in an animation?

I know that I can change the sample rate to speed up/slow down the sprite animation, but this won’t work for my purposes. I need to be able to create an animation of a given length, drop my sprite in that animation and ensure that they’re evenly distributed within that amount of time (in order to ensure synchronization with the animation of other gameobjects being animated from the same animator).

See this related question for more info.

If I understand what you’re asking, this is simple division.

Say you have 100 sprites in an animation, n = 100; And you have an animation that is 2 seconds long, t = 2;

In order to even distribute n sprites along an animation of t length, you have to find the dt (change in t) in which you distribute this. In order to find that period of time per sprite, you would divide t by n:

t / n = 2 (seconds) / 100 (sprites)
2 / 100 = 0.02/1 (seconds/sprites) or dt = 0.02.

Start your first sprite at t = 0, then increment t by dt for each sprite in your animation and place that sprite at that time.

e.g. sprite 5 should be at t = dt * 4 = 0.08

EDIT:

You can do math in code. The following code is basically pseudo code since it doesn’t use Unity’s API whatsoever, but you can apply it to whatever system you’re using probably.

Since animations use a frame system, you’d have to find out a framerate:

rate = n / t = 100 / 2 = 50 fps;

and this can be applied in code:

struct Animation {
	float rate;
	Sprite[] frames;
	public Animation(float rate, params Sprite[] frames) {
		this.rate = rate;
		this.frames = frames;
	}
}

Animation CreateAnimationFromSprites(Sprite[] sprites, float animationLength) {
	float rate = (sprites.Length / animationLength);

	return new Animation(rate, sprites);
}

and all you’ll have to supply is your collection of sprites, and the length of the animation.