Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/tomblind/unity-async-tweens
Tween Extension to Unity-AsyncRoutines
https://github.com/tomblind/unity-async-tweens
animation async await coroutines routines tween unity unity3d
Last synced: 3 months ago
JSON representation
Tween Extension to Unity-AsyncRoutines
- Host: GitHub
- URL: https://github.com/tomblind/unity-async-tweens
- Owner: tomblind
- License: mit
- Created: 2018-11-24T00:15:18.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2019-10-31T21:42:35.000Z (about 5 years ago)
- Last Synced: 2024-04-28T02:18:08.090Z (8 months ago)
- Topics: animation, async, await, coroutines, routines, tween, unity, unity3d
- Language: C#
- Size: 13.7 KB
- Stars: 17
- Watchers: 4
- Forks: 5
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Unity AsyncTweens
Tween Extension to [Unity-AsyncRoutines](https://github.com/tomblind/unity-async-routines)## Basic Usage
```cs
using AsyncRoutines;
using AsyncTweens;public class Foo : MonoBehaviour
{
public RoutineManagerBehavior routineManager;public void Start()
{
routineManager.Run(Bar());
}public async Routine Bar()
{
//Tween self to 1,1,0 over 2 seconds
await Tween.Position.To(transform, new Vector3(1, 1, 0), 2);//Tween to a relative position from the current
await Tween.Position.ToOffset(transform, new Vector3(-1, -1, 0), 2);//Tween from a position to the current
await Tween.Position.From(transform, new Vector3(2, 2, 0), 2);
}
}
```Aside from position, there are built-in tweeners for other Transform properties (eulerAngles, localScale, etc...) and properties on other objects as well (SpriteRenderer.color, RectTransform.anchoredPosition, etc...).
## Easings
Tweeners optionally take an easing function to control the speed throughout the animation. For convenience, a number of standard curves have built-in functions. You may also write your own custom easing function. Another option is to use a Unity AnimationCurve and pass its `Evaluate` method.```cs
public class Foo : MonoBehaviour
{
public AnimationCurve curve;public async Routine Bar()
{
//Built-in curve function
await Tween.Position.To(transform, new Vector3(0, 0, 0), 2, Easing.QuadInOut);//Custom easing function
await Tween.Position.To(transform, new Vector3(1, 1, 0), 2, MyCustomEasing);//AnimationCurve
await Tween.Position.To(transform, new Vector3(2, 2, 0), 2, curve.Evaluate);
}public static float MyCustomEasing(float i)
{
return i * i;
}
}
```You can also use the `Easing` type to enable setting the function from Unity's Inspector window.
```cs
public class Foo : MonoBehaviour
{
public Easing easing; //Allows you to select a built-in easing function or set an animation curvepublic async Routine Bar()
{
await Tween.Position.To(transform, new Vector3(2, 2, 0), 2, easing);
}
}
```## Custom Tweeners
More tweeners for common Unity properties will be added over time, but you can add your own easily.
```cs
using AsyncRoutines;
using AsyncTweens;
using UnityEngine.Tilemaps;public class Foo : MonoBehaviour
{
public static Tweener tilemapColorTweener = new Tweener(
(tilemap) => tilemap.color, // getter
(tilemap, color) => tilemap.color = color //setter
);public Tilemap tilemap;
public async Routine Bar()
{
await tilemapColorTweener.To(tilemap, Color.black, 1);
}
}
```