DEV Community

breadnone
breadnone

Posted on

Zero Allocation Tweening Library for Unity3D

If you've been doing gamedev or webdev, it's 100% guaranteed you'll need to interpolate somethings in your project, and it can be a repetitive and boring tasks.

What is Tweening?
Basically it's just a bunch of numerical interpolation functions we use on primitive types(in c#) or numbers. They're not some sort of hidden magic or anything and there's so little to do in terms of optimization. So how interpolation be usefule? or at least looking more appealing? The most certain answer is "easing function".
public static float EaseInOutQuint(float val)
{
return val < 0.5001f ? 16f * val * val * val * val * val : clamp1(1f - pow(-2f * val + 2f, 5) * 0.5f);
}

You may heard of it before and they're not out of ordinary either other than just a pretty simple maths with different shapes. as a matter of fact, if you've heard about "easing functions" before, you'll be surprised that for decades they all look the same in terms of the formulas used.

source : easings.net

What is STween?
Well, long story short, it was due to a heart broken. No, seriously, the awesome tweening library I've been using for years has been abandoned by the creator for almost 7 years now, so I said, I'll invest some time to make my own, that's when STween born.

Lotta tweens!

Zero Allocation Tweening?
Well yes, sort of. Here's the deal, if you have an option to allocate ahead of time AND only that time the allocation occurs and won't for the rest of the game or project your're working on would you take it? if you ask me, then heck yeah!
That said, under the hood it's just an object pooling and weakReferences as a fallback scenario whenever there's not enough resources can be taken from the pool, even if that fails for example, the worst you can get is by allocating 128 bytes and that's it.

But why 128 bytes? why not? it's 6x smaller than the much popular nextdoor neighbor that you might be using right now ;). Again, that's for the worst case scenario which is super duper rare unless you're tweening hundreds of them at once then that might occur.

Enough said, here use it in your game and don't worry it's MIT :)
https://github.com/breadnone/STween

Top comments (0)