示例#1
0
        private static IObservable <float> Execute(float start, float end, float duration, Easing.Type easeType, float delayBefore, float delayAfter)
        {
            var easingMethod = typeof(Easing).GetMethod(easeType.ToString());

            object[] methodParams = { 0, start, end - start, duration };

            IEnumerator TweenEnumerator(IObserver <float> observer, CancellationToken ct)
            {
                if (delayBefore > 0)
                {
                    yield return(new WaitForSeconds(delayBefore));
                }

                if (ct.IsCancellationRequested)
                {
                    observer.OnCompleted();
                    yield break;
                }

                if (ct.IsCancellationRequested)
                {
                    observer.OnCompleted();
                    yield break;
                }

                float elapsedTime = 0;
                float p           = 0;

                while (elapsedTime < duration)
                {
                    if (ct.IsCancellationRequested)
                    {
                        observer.OnCompleted();
                        yield break;
                    }

                    elapsedTime    += Time.deltaTime;
                    methodParams[0] = Math.Min(elapsedTime, duration);
                    p = Convert.ToSingle(easingMethod?.Invoke(null, methodParams));;

                    observer.OnNext(p);

                    yield return(null);
                }

                if (delayAfter > 0)
                {
                    yield return(new WaitForSeconds(delayAfter));
                }

                observer.OnCompleted();
            }

            return(Observable.FromCoroutine <float>(TweenEnumerator));
        }
示例#2
0
        private static IObservable <float> Execute(
            GameObject gameObject, float start, float end, float duration, Easing.Type easeType, float delayBefore = 0, float delayAfter = 0)
        {
            var easingMethod = typeof(Easing).GetMethod(easeType.ToString());

            object[] methodParams = { 0, start, end - start, duration };

            return
                //Observable.Empty<double>()
                //.StartWith(() => Time.time)
                //.SelectMany(startTime => observable.Select(_ => Time.time - startTime))

                (gameObject.UpdateAsObservable()
                 .Select(_ => Time.deltaTime)
                 .Scan(0f, (a, b) => a + b)
                 .TakeWhile(elapsedTime => elapsedTime <= duration)
                 .Last()
                 .Select(currentTime =>
            {
                methodParams[0] = Math.Min(currentTime, duration);
                return Convert.ToSingle(easingMethod?.Invoke(null, methodParams));
            }));
        }