-
Notifications
You must be signed in to change notification settings - Fork 23
2. The Details
We should know how to setup a tween at this point. The extension methods make that super easy. In reality, they are just taking care of some minor housekeeping for us. The way ZestKit is setup provides some nice flexibility and if you want to get into tinkering with its internals there are only a few objects you should know about.
-
ZestKit: the only MonoBehaviour subclass present. It is very simple class with a simple mind. All it nows about is calling a tweenstickmethod. When the tween is done (tick will return true indicating completion) it removes the tween from it's internal List. That is all it does. -
Tween<T>: the state holder. TheTweenclass manages the tween's state (running, paused, complete). It knows how to count from 0 to the duration of the tween. It doesn't, however, know which object or property it is tweening. It only cares about the type of the tween (int, float, Vector2, etc). -
TweenTarget<T>: the simplest of all the ZestKit players. All it does is set the tweened value. -
Zest: this is a special little helper. It can be used with or without ZestKit (very handy for making quick coroutine tweens in just a few lines of code for example). It consists of a series of methods that handle the tween math including easing.
This class/interface structure lets us separate out the responsibilities. All of the tweenable types are already written and ready for use (Tween<T> subclasses). Anytime you need to tween something new the only thing required is to make a new TweenTarget<T> which consists of just a single method to set the final tweened value.
Now lets have a look at what is happening under the hood in one of the extension methods. We'll look at the ZKalphaTo method. The comments below break down what is going on.
// the MaterialAlphaTarget class implements ITweenTarget<float>. All it needs to know how to do is
// set the value on the object being tweened (in this case, a Material color property)
var tweenTarget = new MaterialAlphaTarget();
// prepareForUse sets the Material (the object) and the propertyName so that it knows which value to set
tweenTarget.prepareForUse( self, propertyName );
// alpha is a float so we are going to need the built in FloatTween to handle the tweening. There are
// built in *Tween classes for all tweenable values.
var tween = new FloatTween();
// set the initial state of the tween and pass along the tweenTarget so it knows where to send the tweened value
tween.initialize( tweenTarget, self.GetColor( propertyName ).a, to, duration );