Ejemplo n.º 1
0
        private void Update()
        {
            // We do nothing if FPS <= 0
            if (framesPerSecond <= 0)
            {
                return;
            }

            if (playing)
            {
                if (!ignoreTimeScale)
                {
                    animationTimer += Time.deltaTime;
                }
                else
                {
                    animationTimer += Time.unscaledDeltaTime;
                }

                if (1f / framesPerSecond < animationTimer)
                {
                    // Next Frame!
                    frameDurationCounter++;
                    ChangeFrame(currentAnimation.GetFrame(animationIndex));
                    SpriteAnimatorEventInfo frameInfo = new SpriteAnimatorEventInfo(currentAnimation.Name, animationIndex);
                    if (customEvents != null && customEvents.ContainsKey(frameInfo))
                    {
                        customEvents[frameInfo].Invoke(this);
                    }
                    animationTimer = 0;

                    if (frameDurationCounter >= currentAnimation.FramesDuration[animationIndex])
                    {
                        // Change frame only if have passed the desired frames
                        animationIndex       = (backwards) ? animationIndex - 1 : animationIndex + 1;
                        frameDurationCounter = 0;
                    }

                    if (animationIndex >= framesInAnimation)
                    {
                        // Last frame, reset index and stop if is one shot
                        animationIndex = (backwards) ? framesInAnimation - 1 : 0;
                        onFinish.Invoke();
                        if (oneShot)
                        {
                            Stop();
                        }
                    }
                }
            }
        }
Ejemplo n.º 2
0
        private void Update()
        {
            // We do nothing if FPS <= 0
            if (currentAnimation == null || currentAnimation.FPS <= 0 || !playing)
            {
                return;
            }

            if (!ignoreTimeScale)
            {
                animationTimer += Time.deltaTime;
            }
            else
            {
                animationTimer += Time.unscaledDeltaTime;
            }

            if (!waitingLoop && 1f / currentAnimation.FPS < animationTimer)
            {
                // Check frames duration
                frameDurationCounter++;
                animationTimer = 0;

                // Double check animation frame index
                if (frameIndex > framesInAnimation - 1 || frameIndex < 0)
                {
                    Restart();
                }

                if (frameDurationCounter >= currentAnimation.FramesDuration[frameIndex])
                {
                    // Change frame only if have passed the desired frames
                    frameIndex           = (backwards) ? frameIndex - 1 : frameIndex + 1;
                    frameDurationCounter = 0;

                    // Check last or first frame
                    if (frameIndex > framesInAnimation - 1 || frameIndex < 0)
                    {
                        // Last frame, reset index and stop if is one shot
                        onFinish.Invoke();

                        if (oneShot)
                        {
                            Stop();
                            return;
                        }
                        else
                        {
                            if (!waitingLoop)
                            {
                                waitingLoop = true;
                                loopTimer   = 0;

                                // Check delay between loops
                                if (delayBetweenLoops)
                                {
                                    SetActiveRenderer(!disableRendererOnFinish);
                                    if (maxDelayBetweenLoops > 0)
                                    {
                                        loopTimer = Random.Range(minDelayBetweenLoops, maxDelayBetweenLoops);
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        // Change sprite
                        ChangeFrame(currentAnimation.GetFrame(frameIndex));
                    }

                    // Check events
                    SpriteAnimatorEventInfo frameInfo = new SpriteAnimatorEventInfo(currentAnimation.Name, frameIndex);
                    if (customEvents != null && customEvents.ContainsKey(frameInfo))
                    {
                        customEvents[frameInfo].Invoke(this);
                    }
                }
            }

            if (waitingLoop)
            {
                // Continue looping if enought time have passed
                loopTimer -= Time.deltaTime;
                if (loopTimer <= 0)
                {
                    waitingLoop = false;
                    if (randomAnimation)
                    {
                        // Pick a random animation
                        PlayRandom(oneShot, backwards);
                        return;
                    }
                    else
                    {
                        // Continue playing the same animation
                        SetActiveRenderer(true);
                        frameIndex = (backwards) ? framesInAnimation - 1 : 0;
                        ChangeFrame(currentAnimation.GetFrame(frameIndex));
                    }
                }
            }
        }
Ejemplo n.º 3
0
        private void Update()
        {
            // We do nothing if the current FPS <= 0
            if (currentAnimation == null || currentFramerate <= 0 || !playing)
            {
                return;
            }

            // Add the delta time to the timer and the total time
            if (!ignoreTimeScale)
            {
                animationTimer      += Time.deltaTime;
                currentAnimationTime = (!currentBackwards) ? currentAnimationTime + Time.deltaTime : currentAnimationTime - Time.deltaTime;
            }
            else
            {
                animationTimer      += Time.unscaledDeltaTime;
                currentAnimationTime = (!currentBackwards) ? currentAnimationTime + Time.unscaledDeltaTime : currentAnimationTime - Time.unscaledDeltaTime;
            }

            if (!waitingLoop && animationTimer >= timePerFrame)
            {
                // Check frames duration
                frameDurationCounter++;
                animationTimer -= timePerFrame;

                // Double check animation frame index
                if (CheckLastFrame())
                {
                    Restart();
                }

                if (frameDurationCounter >= currentAnimation.FramesDuration[frameIndex])
                {
                    // Change frame only if have passed the desired frames
                    frameIndex           = (currentBackwards) ? frameIndex - 1 : frameIndex + 1;
                    frameDurationCounter = 0;

                    // Check last or first frame
                    if (CheckLastFrame())
                    {
                        // Last frame, reset index and stop if is one shot
                        onFinish.Invoke();

                        if (currentOneShot)
                        {
                            Stop();
                            return;
                        }
                        else
                        {
                            if (!waitingLoop)
                            {
                                waitingLoop = true;
                                loopTimer   = 0;

                                // Check delay between loops
                                if (delayBetweenLoops)
                                {
                                    SetActiveRenderer(!disableRendererOnFinish);
                                    if (maxDelayBetweenLoops > 0)
                                    {
                                        loopTimer = Random.Range(minDelayBetweenLoops, maxDelayBetweenLoops);
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        // Change sprite
                        ChangeFrame(currentAnimation.GetFrame(frameIndex));

                        if (frameIndex == stopAtFrame)
                        {
                            Stop();
                        }
                    }

                    // Check events
                    CheckEvents();
                }
            }

            if (waitingLoop)
            {
                // Continue looping if enought time have passed
                loopTimer -= Time.deltaTime;
                if (loopTimer <= 0)
                {
                    if (currentLoopType == LoopType.yoyo)
                    {
                        currentBackwards = !currentBackwards;
                    }

                    waitingLoop = false;
                    if (randomAnimation)
                    {
                        // Pick a random animation
                        PlayRandom(currentOneShot, currentBackwards);
                        return;
                    }
                    else
                    {
                        // Continue playing the same animation
                        SetActiveRenderer(true);
                        animationTimer       = 0;
                        currentAnimationTime = (currentBackwards) ? currentAnimation.AnimationDuration * timePerFrame : 0;
                        frameIndex           = (currentBackwards) ? framesInAnimation - 1 : 0;
                        ChangeFrame(currentAnimation.GetFrame(frameIndex));
                        CheckEvents();
                    }
                }
            }
        }