-
Notifications
You must be signed in to change notification settings - Fork 125
1. GoKit Code Basics
GoKit lets you code your tweens in quite a few different ways to provide maximum flexibility. In this wiki we will show some examples of a few of the different coding styles so that you can decide which one works best for you. The examples will cover two common situations: tweening an object to a new position and tweening the scale and rotation of an object simultaneously. Both tweens will last 4 seconds and have no delay.
Each style has its own advantages and disadvantages. Using Extension methods is the least amount of code. Going the object oriented route lets you reuse your tween properties and TweenConfigs. Each situation has different requirements so use the code style that fits best for your situation.
someTransform.positionTo( 4f, new Vector3( 10, 10, 10 ) );
Go.to( someTransform, 4f, new GoTweenConfig().position( new Vector3( 10, 10, 10 ) ) );
var positionProperty = new PositionTweenProperty( new Vector3( 10, 10, 10 ) );
var config = new GoTweenConfig();
config.addTweenProperty( positionProperty );
var tween = new GoTween( someTransform, 4f, config );
Extension methods do not cover this scenario. They are only useful in the case of when you want to tween a single property on an object.
Go.to( someTransform, 4f, new GoTweenConfig().position( new Vector3( 10, 10, 10 ) ).scale( 3f ) );
var positionProperty = new PositionTweenProperty( new Vector3( 10, 10, 10 ) );
var scaleProperty = new ScaleTweenProperty( new Vector3( 3f, 3f, 3f ) );
var config = new GoTweenConfig();
config.addTweenProperty( positionProperty );
config.addTweenProperty( scaleProperty );
var tween = new GoTween( someTransform, 4f, config );
Go.addTween( tween );
All Tweens, TweenChains and TweenFlows can be controlled once they are created. In the last example above, a reference to the tween was stored in the "tween" variable. Any of the methods below can be used to control the playback and lifecycle of of the tween:
- destroy
- pause
- play
- playForward
- playBackwards
- rewind
- restart
- reverse
- complete
- goTo
- goToAndPlay
All Tweens (including TweenChains and TweenFlows) offer callbacks to let you know what they are up to.
- onInit
- onBegin
- onIterationStart
- onUpdate
- onIterationEnd
- onComplete
Callbacks can be set directly on the Tween or with a TweenConfig object. In addition, if you prefer to monitor the lifecycle via Coroutines there is a waitForCompletion method that you can yield on in a Coroutine.
GoKit lets you set some defaults that will be used for all Tweens created. The default set of defaults is listed below:
public static GoEaseType defaultEaseType = GoEaseType.Linear;
public static LoopType defauLoopType = LoopType.RestartFromBeginning;
public static UpdateType defaultUpdateType = UpdateType.Update;
public static DuplicatePropertyRuleType duplicatePropertyRule = DuplicatePropertyRuleType.None;
public static bool validateTargetObjectsEachTick = true;
You can change the defaults by simply setting the desired field value:
Go.defaultEaseType = GoEaseType.BackInOut
The default loop type, ease type and update type are pretty self explanatory. If validateTargetObjectsEachTick is set to true, each tick of the loop GoKit will see if your object still exists. If it doesn't the tween will automatically be removed. Even if this is set to true it is still recommended that you follow good coding practices and properly remove any running tweens before destroying an object.
The duplicate property rule deserves some explanation. But first, let's define a duplicate property. A duplicate property is any AbstractTweenProperty that has the same target object and property type that is not a part of a TweenChain or a TweenFlow. For example, a ScaleTweenProperty on someTransform and a ScalePathTweenProperty on the same someTransform object is a duplicate. They are two properties affecting the underlying localScale value. They would fight with each other if they both ran together. The available rule types and what they mean are listed below:
- None: in the event of a duplicate, do nothing. In fact, don't even bother checking for duplicates.
- RemoveRunningProperty: in the event of a duplicate, remove the currently running tween property and continue to add the current one
- DontAddCurrentProperty: in the event of a duplicate, do not add the current property
Great care must be taken when using a value other than None. There are scenarios which could cause bugs that are hard to track down. Here are a series of events as an example that may cause unexpected behavior if the duplicate property rule is set to something other than None:
- you add a position tween on someObject
- that position tween is set to not be auto removed on completion because you intend to use it later
- you add a new position tween on someObject. This will cause the duplicate property rule to kick in
In the future if the need arises we may make the duplicate property system a bit more flexible though it will be at the added expense of complexity.