Tween
Here we have some examples on how to build Tweens
Basic
- Creating the tween with a time duration of 10s
- Setting the loop type to PingPong(back and forth)
- Setting the loop count to 4
- Setting a delay of 4 seconds
- Adding an event that is executed before the start of the tween
- Adding an event that is executed after the completion of the tween
- Starting the tween
Tween tween = new Tween(10f)
.SetLoopType(LoopType.PingPong)
.SetLoopCount(4)
.SetDelay(4f)
.OnStarting(() => Debug.Log("Before start."))
.OnCompleted(() => Debug.Log("After completion!"))
.Start();
Simple Motion
- Creating the tween with a time duration of 1s
- Selecting the object to be animated
- Applying the Move motion
- Starting the tween
Tween tween = new Tween(1f)
.For(transform)
.Move(Vector3.one)
.Start();
Multiple Motions
- Creating the tween with a time duration of 1s
- Selecting the object to be animated
- Applying the Move motion
- Applying the Rotate motion
- Applying the Scale motion
- Starting the tween
Note: All 3 motions are applied at the same time, for the same object. They are all part of the same tween
Tween tween = new Tween(1f)
.For(transform)
.Move(Vector3.one)
.Rotate(Vector3.up * 360)
.Scale(Vector3.one * 3)
.Start();
Multiple Objects
- Creating the tween with a time duration of 1s
- Setting the easing to EaseInSine
- Selecting the first object to be animated
- Applying the Move motion
- Selecting the second object to be animated
- Applying the Move motion
- Selecting the third object to be animated
- Applying the Rotate motion
- Applying the Scale motion
- Starting the tween
Note: The tween settings are applied to all the motions for all the objects
Tween tween = new Tween(1f)
.SetEasing(Easing.EaseInSine)
.For(transform1)
.Move(Vector3.one)
.For(transform2)
.Move(Vector3.one * 2)
.For(transform3)
.Rotate(Vector3.up * 360)
.Scale(Vector3.one * 3)
.Start();
Extensions
In order to apply a tween to an object straight away, you can use the "Tween" extension method.
- Selecting the object to be animated
- Applying the extension method(available for any object) and setting the time
- Applying the Move motion
- Applying the Rotate motion
- Applying the Scale motion
- Starting the tween
Tween tween = transform
.Tween(1f)
.SetTimeScale(2f)
.Move(Vector3.one)
.Rotate(Vector3.up * 360)
.Scale(Vector3.one * 3)
.Start();
Async
In the first example, a tween is being awaited upon staring it.
In the second example, a tween is started, something else is done, and if the tween is not yet finished, it can be awaited to finish.
In the second example, a tween is started, something else is done, and if the tween is not yet finished, it can be awaited to finish.
await new Tween(1f)
.For(transform)
.SetSkipFrames(20)
.Move(Vector3.one)
.StartAsync();
Tween tween = new Tween(1f)
.For(transform)
.SetSkipFrames(20)
.Move(Vector3.one)
.Start();
await DoSomethingElseAsync();
await tween.AsAsync();