|
1 | 1 | # Documentation |
2 | 2 | It is recommended to view this file in a markdown viewer. |
3 | | -View on [GitHub](https://github.com/DavidF-Dev/Unity-TweenAnimation/blob/main/DOCUMENTATION.md). |
| 3 | +View on [GitHub](https://github.com/DavidF-Dev/Unity-TweenAnimation/blob/main/DOCUMENTATION.md). |
| 4 | + |
| 5 | +## Usage |
| 6 | +The tweening manager will set itself up automatically in the background, so no activation is required by you. Simply press 'play' and the systems will have activated. To access the scripts, include the ``DavidFDev.Tweening`` namespace at the top of your file. |
| 7 | + |
| 8 | +### Creating a tweening animation |
| 9 | +The method signature for creating a new tweening animation is as follows:</br> |
| 10 | +```cs |
| 11 | +Tween.Create(start, end, duration, easingFunction, begin, onUpdate, onComplete) : Tween |
| 12 | +``` |
| 13 | +- ``start``: the initial value of the animation (A). |
| 14 | +- ``end``: the destination value of the animation (B). |
| 15 | +- ``duration``: the total time that the animation will take, in seconds (t). |
| 16 | +- ``easingFunction``: the easing function to use - usually one of the built-in options (see below for more details). |
| 17 | +- ``begin``: a boolean for whether the tweening animation should start playing straight away (otherwise it will need to be started manually). |
| 18 | +- ``onUpdate``: a callback that is invoked when the tweening animation updates itself. The callback provides the current tweened value. |
| 19 | +- ``onComplete``: a callback that is invoked when the tweening animation finishes playing. |
| 20 | +- Returns a ``Tween`` instance that can be used to control playback. |
| 21 | + |
| 22 | +### Easing functions |
| 23 | +Easing functions specify the rate of change of the tweened value over time. I have included various built-in easing functions that can be accessed through the ``Ease`` static class. For example: ``Ease.SineIn``, ``Ease.Linear``, ``Ease.ExpoInOut``, etc.</br> |
| 24 | + |
| 25 | +The easing functions all implement the ``EasingFunction`` delegate. This allows you to seamlessly create your own easing functions if needed: |
| 26 | +```cs |
| 27 | +public delegate float EasingFunction(float t); |
| 28 | +// ... |
| 29 | +EasingFunction myEasingFunction = t => t * t; |
| 30 | +``` |
| 31 | + |
| 32 | +Furthermore, there is also an ``EaseType`` enum which can be used if you need to expose an easing function in the unity inspector. Use ``Ease.GetEasingFunction()`` to convert between enum value and easing function. |
| 33 | + |
| 34 | +### Controlling playback |
| 35 | +A tween instance contains properties and methods for querying & controlling playback: |
| 36 | +- ``IsActive``: Whether the tween is actively being updated. Set via ``Start()`` and ``Stop()``. |
| 37 | +- ``IsPaused``: Whether the tween animation is paused. |
| 38 | +- ``Start()``: Begin or restart the tween. |
| 39 | +- ``Stop()``: End the tween prematurely. |
| 40 | + |
| 41 | +There are other properties and methods which you can see when viewing the code. |
| 42 | + |
| 43 | +### Examples |
| 44 | +```cs |
| 45 | +Tween.Create( |
| 46 | + start: 0f, |
| 47 | + end: 10f, |
| 48 | + duration: 5f, |
| 49 | + easingFunction: Ease.SineInOut, |
| 50 | + begin: true, |
| 51 | + onUpdate: x => Debug.Log(x), |
| 52 | + onComplete: () => Debug.Log("Finished!")); |
| 53 | +``` |
| 54 | +This call will begin tweening (A = 0) to (B = 10) over (t = 5) seconds using a sine easing function. At each step, the current value will be displayed to the unity console. |
| 55 | +</br></br> |
| 56 | +```cs |
| 57 | +Tween tween = Tween.Create( |
| 58 | + start: Vector2.zero, |
| 59 | + end: Vector2.one * 5f, |
| 60 | + duration: 2.5f, |
| 61 | + easingFunction: Ease.ElasticOut, |
| 62 | + begin: false, |
| 63 | + onUpdate: x => transform.position = x, |
| 64 | + onComplete: null); |
| 65 | + |
| 66 | +tween.Start(); |
| 67 | +``` |
| 68 | +This call will create a dormant tween set to animate from (A = (0,0)) to (B = (5, 5)) over (t = 2.5) seconds using an elastic easing function. At each step, the game object's transform will be updated to the current value (moving the game object). However, because ``begin`` is false, the tween will not start playing until ``Start()`` is called. |
| 69 | +</br></br> |
| 70 | +```cs |
| 71 | +transform.TweenX( |
| 72 | + start: transform.position.x, |
| 73 | + end: transform.position.x + 10f, |
| 74 | + duration: 4f); |
| 75 | +``` |
| 76 | +This call uses one of the built-in extension methods that tween's an object's x position 10 units to the right over (t = 4) seconds. The remainder of the arguments are omitted as they are using default values. This means the easing function will default to ``Ease.Linear``. |
| 77 | +</br></br> |
| 78 | +```cs |
| 79 | +SpriteRenderer renderer = GetComponent<SpriteRenderer>(); |
| 80 | +Tween.Create<Color>( |
| 81 | + start: Color.white, |
| 82 | + end: Color.green, |
| 83 | + duration: 2f, |
| 84 | + lerpFunction: Color.LerpUnclamped, |
| 85 | + easingFunction: Ease.Spike, |
| 86 | + begin: true, |
| 87 | + onUpdate: x => renderer.color = x, |
| 88 | + onComplete: () => renderer.enabled = false); |
| 89 | +``` |
| 90 | +This example shows how to create a generic tweening animation (note that this example is arbitrary as there is already a built-in overload for colours). The method signature is almost identical to the previous examples, however there is a new required parameter: ``lerpFunction``. This of the delegate type, ``LerpFunction`` - and allows you to create custom lerpable tweening types. |
| 91 | +</br></br> |
| 92 | +```cs |
| 93 | +Light light = GetComponent<Light>(); |
| 94 | +Tween.Create<float>( |
| 95 | + target: light, |
| 96 | + propertyName: nameof(light.intensity), |
| 97 | + start: 1f, |
| 98 | + end: 0f, |
| 99 | + duration: 10f, |
| 100 | + lerpFunction: Mathf.LerpUnclamped, |
| 101 | + easingFunction: Ease.CubicInOut); |
| 102 | +``` |
| 103 | +This call uses an advanced overload for tweening a property on an object reference. The ``target`` parameter is an object reference and ``propertyName`` is the name of a declared property on the target type. This example will begin tweening a light's intensity from (A = 1) to (B = 0) over (t = 10) seconds using a cubic easing function (note that an equivalent extension method already exists: ``light.TweenIntensity()``.) |
0 commit comments