Example #1
0
        private static IEnumerator TweenAlphaRoutine(Sprite sprite, float from, float to, int duration, int delay,
                                                     OnTweenComplete onComplete,
                                                     Easing.Equation equation)
        {
            //TODO: remove this call to WaitForMilliSeconds and implement it in the while loop to prevent another instantiation of a yield
            if (delay > 0)
            {
                yield return(new WaitForMilliSeconds(delay));
            }

            int time = 0;

            sprite.alpha = from;

            float fromDir = from > to ? from : 0;
            float toDir   = from > to ? 0 : from;

            while (time < duration)
            {
                sprite.alpha = toDir + Easing.Ease(equation, time, fromDir, to - from, duration);

                time += Time.deltaTime;
                yield return(null);
            }

            sprite.alpha = to;

            if (onComplete != null)
            {
                onComplete.Invoke(sprite);
            }
        }
 public static void TweenColorAlpha(IHasColor hasColor, float from, float to, int duration,
                                    Easing.Equation easing,
                                    int delay = 0, ITweener tweener = null)
 {
     CoroutineManager.StartCoroutine(
         TweenColorAlphaRoutine(hasColor, from, to, duration, easing, delay, tweener), null);
 }
        static IEnumerator TweenSpriteScaleRoutine(GameObject g, Vector2 from, Vector2 to, int duration,
                                                   Easing.Equation easing,
                                                   int delay = 0, OnFinished onFinished = null)
        {
            if (delay > 0)
            {
                yield return(new WaitForMilliSeconds(delay));
            }

            float durationF = duration * 0.001f;
            float time      = 0;

            g.SetScaleXY(from.x, from.y);

            while (time < durationF)
            {
                float easeVal = Easing.Ease(easing, time, 0, 1, durationF);

                float easeValMapX = Mathf.Map(easeVal, 0, 1, from.x, to.x);
                float easeValMapY = Mathf.Map(easeVal, 0, 1, from.y, to.y);

                float scaleX = easeValMapX;
                float scaleY = easeValMapY;

                g.SetScaleXY(scaleX, scaleY);

                time += Time.delta;

                yield return(null);
            }

            onFinished?.Invoke();
        }
        static IEnumerator TweenColorAlphaRoutine(IHasColor hasColor, float from, float to, int duration,
                                                  Easing.Equation easing,
                                                  int delay = 0, ITweener tweener = null)
        {
            if (delay > 0)
            {
                yield return(new WaitForMilliSeconds(delay));
            }

            float durationF = duration * 0.001f;
            float time      = 0;

            hasColor.Alpha = from;
            var childs = hasColor.children;

            for (int i = 0; i < childs.Count; i++)
            {
                if (childs[i] is IHasColor)
                {
                    ((IHasColor)childs[i]).Alpha = from;
                }
            }

            while (time < durationF)
            {
                hasColor.Alpha = Easing.Ease(easing, time, from, to, durationF);

                for (int i = 0; i < childs.Count; i++)
                {
                    if (childs[i] is IHasColor)
                    {
                        ((IHasColor)childs[i]).Alpha = Easing.Ease(easing, time, from, to, durationF);
                    }
                }

                time += Time.deltaTime * 0.001f;

                yield return(null);
            }

            hasColor.Alpha = to;
            for (int i = 0; i < childs.Count; i++)
            {
                if (childs[i] is IHasColor)
                {
                    ((IHasColor)childs[i]).Alpha = to;
                }
            }

            if (tweener != null)
            {
                tweener.OnTweenEnd(hasColor);
            }
        }
Example #5
0
        public static void TweenColor(Sprite sprite, uint from, uint to, int duration = 400,
                                      Easing.Equation equation = Easing.Equation.QuadEaseOut)
        {
            if (_colorRoutinesMap.ContainsKey(sprite))
            {
                CoroutineManager.StopCoroutine(_colorRoutinesMap[sprite]);
                _colorRoutinesMap.Remove(sprite);
            }

            var ie = CoroutineManager.StartCoroutine(TweenColorRoutine(sprite, from, to, duration, equation), null);

            _colorRoutinesMap.Add(sprite, ie);
        }
Example #6
0
        public static IEnumerator TweenColorRoutine(Sprite sprite, uint from, uint to, int duration,
                                                    Easing.Equation equation)
        {
            int time = 0;

            sprite.color = from;

            //float fromA = ((byte) (from >> 24)) / 255f;
            float fromR = ((byte)(from >> 16)) / 255f;
            float fromG = ((byte)(from >> 8)) / 255f;
            float fromB = ((byte)(from >> 0)) / 255f;

            //float toA = ((byte) (to >> 24)) / 255f;
            float toR = ((byte)(to >> 16)) / 255f;
            float toG = ((byte)(to >> 8)) / 255f;
            float toB = ((byte)(to >> 0)) / 255f;

            float redFromDir   = fromR > toR ? fromR : 0;
            float greenFromDir = fromG > toG ? fromG : 0;
            float blueFromDir  = fromB > toB ? fromB : 0;

            float redToDir   = fromR > toR ? 0 : fromR;
            float greenToDir = fromG > toG ? 0 : fromG;
            float blueToDir  = fromB > toB ? 0 : fromB;

            while (time < duration)
            {
                float r = redToDir + Easing.Ease(equation, time, redFromDir, toR - fromR,
                                                 duration);
                float g = greenToDir +
                          Easing.Ease(equation, time, greenFromDir, toG - fromG,
                                      duration);
                float b = blueToDir +
                          Easing.Ease(equation, time, blueFromDir, toB - fromB,
                                      duration);

                sprite.SetColor(r, g, b);

                time += Time.deltaTime;
                yield return(null);
            }

            sprite.color = to;
        }
        static IEnumerator TweenSpriteAlphaRoutine(Sprite s, float from, float to, int duration, Easing.Equation easing,
                                                   int delay = 0, ITweener tweener = null)
        {
            if (delay > 0)
            {
                yield return(new WaitForMilliSeconds(delay));
            }

            float durationF = duration * 0.001f;
            float time      = 0;

            s.alpha = from;
            var childs = s.GetChildren();

            for (int i = 0; i < childs.Count; i++)
            {
                if (childs[i] is Sprite)
                {
                    ((Sprite)childs[i]).alpha = from;
                }
            }

            while (time < durationF)
            {
                s.alpha = Easing.Ease(easing, time, from, to, durationF);
                //Console.WriteLine($"{s.name} - alpha: {s.alpha}");

                for (int i = 0; i < childs.Count; i++)
                {
                    if (childs[i] is Sprite)
                    {
                        ((Sprite)childs[i]).alpha = Easing.Ease(easing, time, from, to, durationF);
                    }
                }

                time += Time.deltaTime * 0.001f;

                yield return(null);
            }

            if (tweener != null)
            {
                tweener.OnTweenEnd(s);
            }
        }
 public static void TweenSpriteAlpha(Sprite s, float from, float to, int duration, Easing.Equation easing,
                                     int delay = 0, ITweener tweener = null)
 {
     CoroutineManager.StartCoroutine(TweenSpriteAlphaRoutine(s, from, to, duration, easing, delay, tweener), null);
 }
        public static void TweenInteger(IHasTweenInteger tweened, int from, int to, int duration = 400, int delay = 0, Easing.Equation equation = Easing.Equation.QuadEaseOut)
        {
            if (_tweenMap.TryGetValue(tweened, out var ieRunning))
            {
                CoroutineManager.StopCoroutine(ieRunning);
                _tweenMap.Remove(tweened);
            }

            var ie = CoroutineManager.StartCoroutine(TweenIntegerRoutine(tweened, from, to, duration, delay, equation), null);

            _tweenMap.Add(tweened, ie);
        }
Example #10
0
        private static IEnumerator TweenIntegerRoutine(IHasTweenInteger tweened, int @from, int to, int duration, int delay, Easing.Equation equation)
        {
            if (delay > 0)
            {
                yield return(new WaitForMilliSeconds(delay));
            }

            int time = 0;

            float fromDir = from > to ? from : 0;
            float toDir   = from > to ? 0 : from;

            while (time < duration)
            {
                tweened.IntValue = Mathf.Round(toDir + Easing.Ease(equation, time, fromDir, to - from, duration));

                time += Time.deltaTime;
                yield return(null);
            }

            tweened.IntValue = to;

            if (_tweenMap.ContainsKey(tweened))
            {
                _tweenMap.Remove(tweened);
            }
        }
        static IEnumerator TweenSpriteAlphaRoutine(Sprite s, float from, float to, int duration, Easing.Equation easing,
                                                   int delay = 0, OnFinished onFinished = null)
        {
            if (delay > 0)
            {
                yield return(new WaitForMilliSeconds(delay));
            }

            float durationF = duration * 0.001f;
            float time      = 0;

            s.alpha = from;
            var childs = s.GetChildrenRecursive();

            for (int i = 0; i < childs.Count; i++)
            {
                if (childs[i] is Sprite)
                {
                    ((Sprite)childs[i]).alpha = from;
                }
            }

            while (time < durationF)
            {
                float easeVal    = Easing.Ease(easing, time, 0, 1, durationF);
                float easeValMap = Mathf.Map(easeVal, 0, 1, from, to);
                s.alpha = easeValMap;

                for (int i = 0; i < childs.Count; i++)
                {
                    if (childs[i] is Sprite)
                    {
                        ((Sprite)childs[i]).alpha = easeValMap;
                    }
                }

                time += Time.delta;

                yield return(null);
            }

            onFinished?.Invoke();
        }
 public static void TweenSpriteAlpha(Sprite s, float from, float to, int duration, Easing.Equation easing,
                                     int delay = 0, OnFinished onFinished = null)
 {
     CoroutineManager.StartCoroutine(TweenSpriteAlphaRoutine(s, from, to, duration, easing, delay, onFinished),
                                     s);
 }
 public static void TweenSpriteAlpha(Sprite s, float from, float to, int duration, Easing.Equation easing = Easing.Equation.QuadEaseOut)
 {
     TweenSpriteAlpha(s, from, to, duration, easing, 0, null);
 }
 public static void TweenScale(GameObject g, Vector2 from, Vector2 to, int duration, Easing.Equation easing,
                               int delay = 0, OnFinished onFinished = null)
 {
     CoroutineManager.StartCoroutine(TweenSpriteScaleRoutine(g, from, to, duration, easing, delay, onFinished),
                                     g);
 }
Example #15
0
        public static void TweenScalePingPong(Sprite sprite, float from, float to, int duration,
                                              OnTweenComplete onComplete = null, int delay = 0, Easing.Equation equation = Easing.Equation.QuadEaseOut)
        {
            if (sprite == null || sprite.Destroyed)
            {
                return;
            }

            SpriteTweener.TweenScale(sprite, from, to, duration, go =>
            {
                SpriteTweener.TweenScale(sprite, to, from, duration, go1 =>
                {
                    TweenScalePingPong(sprite, from, to, duration, null, delay, equation);
                });
            });
        }
Example #16
0
        public static void TweenAlpha(Sprite sprite, float from, float to, int duration,
                                      OnTweenComplete onComplete = null, int delay = 0, Easing.Equation equation = Easing.Equation.QuadEaseOut)
        {
            if (_alphaRoutinesMap.ContainsKey(sprite))
            {
                CoroutineManager.StopCoroutine(_alphaRoutinesMap[sprite]);
                _alphaRoutinesMap.Remove(sprite);
            }

            var ie = CoroutineManager.StartCoroutine(
                TweenAlphaRoutine(sprite, from, to, duration, delay, onComplete, equation), null);

            _alphaRoutinesMap.Add(sprite, ie);
        }