Skip to content

Instantly share code, notes, and snippets.

@Protonz
Last active February 26, 2018 19:46
Show Gist options
  • Select an option

  • Save Protonz/1f90f03d9255bdbc7603a5b6de5ed5f6 to your computer and use it in GitHub Desktop.

Select an option

Save Protonz/1f90f03d9255bdbc7603a5b6de5ed5f6 to your computer and use it in GitHub Desktop.

Revisions

  1. Protonz revised this gist Feb 23, 2018. 1 changed file with 12 additions and 0 deletions.
    12 changes: 12 additions & 0 deletions TweenObservable.cs
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,7 @@
    using System.Collections;
    using UniRx;
    using UnityEngine;
    using System.Threading;

    namespace UniRx {

    @@ -16,12 +17,23 @@ public static IObservable<Vector2> Tween( Vector2 from, Vector2 to, float second
    return UniRx.Observable.FromMicroCoroutine<Vector2>(( observer, cancellationToken ) => TweenEveryCycleCore(observer, from, to, seconds, lerpFunc, cancellationToken), FrameCountType.Update);
    }

    public static IObservable<Vector3> Tween( Vector3 from, Vector3 to, float seconds ) {
    Vector3 delta = to - from;
    Func<float, Vector3> lerpFunc = ( progress ) => { return from + (delta * progress); };
    return UniRx.Observable.FromMicroCoroutine<Vector3>(( observer, cancellationToken ) => TweenEveryCycleCore(observer, from, to, seconds, lerpFunc, cancellationToken), FrameCountType.Update);
    }

    public static IObservable<float> Tween( float from, float to, float seconds ) {
    float delta = to - from;
    Func<float, float> lerpFunc = ( progress ) => { return from + (delta * progress); };
    return UniRx.Observable.FromMicroCoroutine<float>(( observer, cancellationToken ) => TweenEveryCycleCore(observer, from, to, seconds, lerpFunc, cancellationToken), FrameCountType.Update);
    }

    public static IObservable<Quaternion> Tween( Quaternion from, Quaternion to, float seconds ) {
    Func<float, Quaternion> lerpFunc = ( progress ) => { return Quaternion.Lerp(from, to, progress); };
    return UniRx.Observable.FromMicroCoroutine<Quaternion>(( observer, cancellationToken ) => TweenEveryCycleCore(observer, from, to, seconds, lerpFunc, cancellationToken), FrameCountType.Update);
    }

    static IEnumerator TweenEveryCycleCore<T>( IObserver<T> observer, T from, T to, float seconds, Func<float,T> lerpFunc, CancellationToken cancellationToken ) {
    if( cancellationToken.IsCancellationRequested ) yield break;
    if( seconds <= 0 ) {
  2. Protonz created this gist Mar 7, 2017.
    52 changes: 52 additions & 0 deletions TweenObservable.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,52 @@
    using System;
    using System.Collections;
    using UniRx;
    using UnityEngine;

    namespace UniRx {

    public static partial class CustomObservable {

    /// <summary>
    /// Tween in seconds
    /// </summary>
    public static IObservable<Vector2> Tween( Vector2 from, Vector2 to, float seconds ) {
    Vector2 delta = to - from;
    Func<float, Vector2> lerpFunc = ( progress ) => { return from + (delta * progress); };
    return UniRx.Observable.FromMicroCoroutine<Vector2>(( observer, cancellationToken ) => TweenEveryCycleCore(observer, from, to, seconds, lerpFunc, cancellationToken), FrameCountType.Update);
    }

    public static IObservable<float> Tween( float from, float to, float seconds ) {
    float delta = to - from;
    Func<float, float> lerpFunc = ( progress ) => { return from + (delta * progress); };
    return UniRx.Observable.FromMicroCoroutine<float>(( observer, cancellationToken ) => TweenEveryCycleCore(observer, from, to, seconds, lerpFunc, cancellationToken), FrameCountType.Update);
    }

    static IEnumerator TweenEveryCycleCore<T>( IObserver<T> observer, T from, T to, float seconds, Func<float,T> lerpFunc, CancellationToken cancellationToken ) {
    if( cancellationToken.IsCancellationRequested ) yield break;
    if( seconds <= 0 ) {
    observer.OnNext(to);
    observer.OnCompleted();
    yield break;
    }
    float totalTime = 0f;
    observer.OnNext(from);
    while( true ) {
    yield return null;
    if( cancellationToken.IsCancellationRequested ) yield break;

    totalTime += UnityEngine.Time.deltaTime;
    if( totalTime >= seconds ) {
    observer.OnNext(to);
    observer.OnCompleted();
    yield break;
    }
    else {
    float normalizedTime = totalTime / seconds;
    observer.OnNext(lerpFunc(normalizedTime));
    }
    }
    }
    }

    }