Uni Tween by worldreaver - 4

Tween

Rx Tween Animation Library for Unity

Unknown VersionUnknown LicenseUpdated 1 year agoCreated on March 15th, 2020
Go to source

Readme

What

  • Rx Tween Animation Library for Unity

Requirements

Unity 2019.2+ .Net 2.0 Scripting Runtime

Examples

Basic

sample1

Move from (-5,0,0) to (5,0,0) at 4m / s.

Tweener.Play(new Vector3(-5f, 0f, 0f), new Vector3(5f, 0f, 0f), Motion.Uniform(4f))
    .Subscribe(x => cube.transform.position = x);
    //.SubscribeToPosition(cube);

Method Chain

sample2

Move from (-5,0,0) to (5,0,0) and then move to (0,3,0) at a constant speed. When this method is used, the change in value stops for one frame between the first movement and the second movement. Use the following Path method to move smoothly.

var tween = Motion.Uniform(5f);
Tweener.Play(new Vector3(-5f, 0f, 0f), new Vector3(5f, 0f, 0f), tween)
    .Play(new Vector3(0f, 3f, 0f), tween)
    .SubscribeToPosition(cube);

Easing

sample3

Move in 2 seconds with EaseOutQuad.

Tweener.Play(new Vector3(-5f, 0f, 0f), new Vector3(5f, 0f, 0f), Easing.OutQuad(2f))
    .SubscribeToPosition(cube);

Sleep

sample10

After moving, it starts again after 1 second.

Tweener.Play(new Vector3(-5f, 0f, 0f), new Vector3(0f, 0f, 0f), Easing.OutExpo(2f))
    .Sleep(1f)
    .Play(new Vector3(5f, 0f, 0f), Easing.OutExpo(2f))
    .SubscribeToPosition(cube);

Path

sample4

Move to the specified position in order.

var positions = new[]
{
    new Vector3(-5f, 0f, 0f),
    new Vector3(0f, 3f, 0f),
    new Vector3(5f, 0f, 0f),
    new Vector3(0f, -3f, 0f),
    new Vector3(-5f, 0f, 0f),
};

Tweener.Play(positions, Easing.InOutSine(6f))
    .SubscribeToPosition(cube);

Combine

sample5

Each x, y, z coordinate is animated separately and synthesized.

var x = Tweener.Play(-5f, 5f, Easing.InOutSine(3f));

var y = Tweener.Play(0f, 3f, Easing.InOutSine(1.5f))
    .Play(0f, Easing.InOutSine(1.5f));

var z = Tweener.Stay(0f);

Observable.CombineLatest(x, y, z)
    .SubscribeToPosition(cube);

AnimationCurve

sample11

Move using UnityEngine.AnimationCurve.

Tweener.Play(new Vector3(-5f, 0f, 0f), new Vector3(5f, 0f, 0f), Motion.From(curve, 3f))
    .SubscribeToPosition(cube);

Extensions

sample6

Move from cube.transform.position to (3,3,0).

cube.transform.position
    .Play(new Vector3(3f, 3f, 0f), Easing.OutBack(2f))
    .SubscribeToPosition(cube);

Circle

sample8

Convert IObservble to circular motion.

Tweener.Play(0f, Mathf.PI * 2f, Easing.OutCubic(3f))
    .Select(x => new Vector3(Mathf.Sin(x), Mathf.Cos(x), 0.0f))
    .Select(x => x * 3f)
    .SubscribeToPosition(cube);

Range / Lerp

sample17

Follow along halfway. Follow only a specific range.

var flow = Tweener.Play(Easing.InOutExpo(2.5f))
    .Stop(0.5f)
    .Play(1.0f, 0.0f, Easing.InOutExpo(2.5f));

flow
    .Range(0.0f, 0.5f)
    .Lerp(new Vector3(-5f, 0f, 0f), new Vector3(0f, 0f, 0f))
    .SubscribeToPosition(cube2);

flow
    .Lerp(new Vector3(-5f, -1f, 0f), new Vector3(5f, -1f, 0f))
    .SubscribeToPosition(cube);

PlayIn/PlayOut/PlayInOut

sample24

From animation to constant speed movement.

Tweener.PlayIn(-5f, 0f, 5f, Easing.InCubic(1f))
    .SubscribeToPositionX(cube);

Delay

sample18

Observable.Delay in UniRx

var circle = Tweener.Play(0f, Mathf.PI * 2f, Easing.OutCubic(3f))
    .Select(x => new Vector3(Mathf.Sin(x), Mathf.Cos(x), 0.0f))
    .Select(x => x * 3f);

circle
    .SubscribeToPosition(cube);

circle
    .Delay(0.3f)
    .SubscribeToPosition(cube2);

circle
    .Delay(0.55f)
    .SubscribeToPosition(cube3);

Blend

sample20

Add the two moves together.

var circle = Tweener
    .Play(Mathf.PI, Mathf.PI * 2f * 3f, Easing.InOutSine(3f))
    .Select(x => new Vector3(Mathf.Sin(x), Mathf.Cos(x), 0f));

var straight = Tweener
    .Play(-3f, 3f, Easing.InOutSine(3f))
    .Select(x => new Vector3(0f, x, 0f));

Observable.CombineLatest(circle, straight)
    .Sum()
    .SubscribeToPosition(cube);

WhenAll

sample9

Use WhenAll to synchronize the animation.

var leftCube1 = Tweener
    .Play(new Vector3(-5f, 0f, 0f), new Vector3(-0.5f, 0f, 0f), Easing.Linear(2.5f))
    .DoToPosition(cube);

var rightCube1 = Tweener
    .Play(new Vector3(5f, 0f, 0f), new Vector3(0.5f, 0f, 0f), Easing.OutCubic(1f))
    .DoToPosition(cube2);

var leftCube2 = Tweener
    .Play(new Vector3(-0.5f, 0f, 0f), new Vector3(-0.5f, 3f, 0f), Easing.OutCubic(1f))
    .DoToPosition(cube);

var rightCube2 = Tweener
    .Play(new Vector3(0.5f, 0f, 0f), new Vector3(0.5f, 3f, 0f), Easing.OutCubic(1f))
    .DoToPosition(cube2);

Observable.WhenAll(leftCube1, rightCube1)
    .ContinueWith(Observable.WhenAll(leftCube2, rightCube2))
    .Subscribe();

Subscribe onComplete

Tracking Tween complete using DoOnCompleted or override of method ex SubscribeToPosition, DoToPosition

        var tween = TweenMotion.Uniform(5f);
        Tweener.Play(new Vector3(-5f, 0f, 0f), new Vector3(5f, 0f, 0f), tween).DoOnCompleted(() => Debug.Log("Complete part 1"))
            .Play(new Vector3(5f, 3f, 0f), tween).DoOnCompleted(() => Debug.Log("Complete part 2"))
            .SubscribeToPosition(cube);
        var leftCube1 = Tweener
            .Play(new Vector3(-5f, 0f, 0f), new Vector3(-0.5f, 0f, 0f), Easing.Linear(2.5f))
            .DoToPosition(cube, () => Debug.Log("Complete left cube move x to -0.5f")).DoOnCompleted(() => Debug.Log("Do on complete left cube move x to -0.5f"));

        var rightCube1 = Tweener
            .Play(new Vector3(5f, 0f, 0f), new Vector3(0.5f, 0f, 0f), Easing.OutCubic(1f))
            .DoToPosition(cube2, () => Debug.Log("Complete right cube move x to 0.5f"));

        var leftCube2 = Tweener
            .Play(new Vector3(-0.5f, 0f, 0f), new Vector3(-0.5f, 3f, 0f), Easing.OutCubic(1f))
            .DoToPosition(cube);

        var rightCube2 = Tweener
            .Play(new Vector3(0.5f, 0f, 0f), new Vector3(0.5f, 3f, 0f), Easing.OutCubic(1f))
            .DoToPosition(cube2);

        Observable.WhenAll(leftCube1, rightCube1).DoOnCompleted(() => Debug.Log("Compelete both left cube and right cube"))
            .ContinueWith(Observable.WhenAll(leftCube2, rightCube2)).DoOnCompleted(() => Debug.Log("COMPLETE"))
            .Subscribe();

Dependencies

  • UniRx

  • MoreLINQ

  • UniRxEx

  • Utility

License

  • Under the MIT license
  • Some code is borrowed from @AnimeRx
  • Thanks for kyubuns
Show all projects by worldreaver