Skip to content

3. Tweening Custom Properties

Mike edited this page Apr 23, 2015 · 5 revisions

ZestKit has built in tweens for all of the most commonly tweened properties. Sometimes you may want to tween a property that is custom to your class or that ZestKit doesn't have a built in tween for. You have a few options to do this. Let's go over the pros and cons of each.

  • PropertyTweens class: this is the easiest way to go.

    • Upsides: the PropertyTweens class can tween any property straight away and it does it really fast
    • Downsides: under the hood, it uses reflection so it does allocate a bit of memory (but only at creation time. Each tick after that does not reflect). Not a big deal on desktop but on mobile avoiding allocations is key.
  • Implement ITweenTarget: add one method to your class and you can make it a tween target ready for use.

    • Upsides: doesn't allocate like the previously mentioned PropertyTweens, easy to implement, great for custom properties in custom classes
    • Downsides: none really
  • Subclass AbstractTweenTarget: create a tiny subclass of AbstractTweenTarget and you have a reusable tween target for your next project

    • Upsides: this is how ZestKit handles it's tween targets internally, easy to implement, great for tweening Unity objects due to it's reusability
    • Downsides: allocates a tiny class (it just has a single pointer) so if you need several hundred thousand for whatever reason you may want to cache the class instances if you are on mobile

Let's have a look at examples of each of these so that we can best determine which to use and so that we have a roadmap for creating our own tween targets.

PropertyTweens Class

PropertyTweens can tween any int, float, Vector2/3/4, Quaternion or Color/32 property anywhere. Take a look at the 2 examples below. The first one tweens the Unity LensFlare color property and the second tweens a custom class property.

PropertyTweens.colorPropertyTo( lensFlare, "color", lensFlare.color, Color.blue, 0.5f )
	.start();
public class MyClass
{
    public Vector2 myVector { get; set; }
}

PropertyTweens.vector2PropertyTo( myClassInstance, "myVector", myClassInstance.myVector, Vector2.one, 0.5f )
	.start();

Implement ITweenTarget

We will extend the MyClass example above to illustrate how we can implement ITweenTarget to make it tweenable. All we have to do is implement the interface's single method:

public class MyClass : ITweenTarget<Vector2>
{
    public Vector2 myVector { get; set; }

    public void setTweenedValue( Vector2 value )
    {
        myVector = value;
    }
}

Subclass AbstractTweenTarget

As previously mentioned, this is how ZestKit handles it's tween targets internally. This method is usually the best choice if you are working with any Unity objects. It produces the kind of code that you can add to your own reusable library or even send a pull request if its for a fairly commonly tweened item.

AbstractTweenTarget makes adding new tween targets super easy by letting the IDE do the work for us. Just right-click -> Refactor -> Implement abstract members. When subclassing AbstractTweenTarget there are two generic types: the first is the object being tweened and the second is the type of the object (ScrollRect and Vector2 in the example below). Stick in an optional constructor to set the _target at object creation time and you're all done.

public class ScrollRectTarget : AbstractTweenTarget<ScrollRect,Vector2>
{
	public override void setTweenedValue( Vector2 value )
	{
		_target.normalizedPosition = value;
	}

	public ScrollRectTarget( ScrollRect scrollRect )
	{
		_target = scrollRect;
	}
}

Clone this wiki locally