public override void OnEnter()
        {
            if (spriteRenderer == null)
            {
                Continue();
                return;
            }

            CameraController cameraController = CameraController.GetInstance();

            if (waitUntilFinished)
            {
                cameraController.waiting = true;
            }

            SpriteFader.FadeSprite(spriteRenderer, targetColor, duration, Vector2.zero, delegate {
                if (waitUntilFinished)
                {
                    cameraController.waiting = false;
                    Continue();
                }
            });

            if (!waitUntilFinished)
            {
                Continue();
            }
        }
        /// <summary>
        /// Attaches a SpriteFader component to a sprite object to transition its color over time.
        /// </summary>
        public static void FadeSprite(SpriteRenderer spriteRenderer, Color targetColor, float duration, Vector2 slideOffset, Action onComplete = null)
        {
            if (spriteRenderer == null)
            {
                Debug.LogError("Sprite renderer must not be null");
                return;
            }

            // Fade child sprite renderers
            var spriteRenderers = spriteRenderer.gameObject.GetComponentsInChildren <SpriteRenderer>();

            for (int i = 0; i < spriteRenderers.Length; i++)
            {
                var sr = spriteRenderers[i];
                if (sr == spriteRenderer)
                {
                    continue;
                }
                FadeSprite(sr, targetColor, duration, slideOffset);
            }

            // Destroy any existing fader component
            SpriteFader oldSpriteFader = spriteRenderer.GetComponent <SpriteFader>();

            if (oldSpriteFader != null)
            {
                Destroy(oldSpriteFader);
            }

            // Early out if duration is zero
            if (Mathf.Approximately(duration, 0f))
            {
                spriteRenderer.color = targetColor;
                if (onComplete != null)
                {
                    onComplete();
                }
                return;
            }

            // Set up color transition to be applied during update
            SpriteFader spriteFader = spriteRenderer.gameObject.AddComponent <SpriteFader>();

            spriteFader.fadeDuration   = duration;
            spriteFader.startColor     = spriteRenderer.color;
            spriteFader.endColor       = targetColor;
            spriteFader.endPosition    = spriteRenderer.transform.position;
            spriteFader.slideOffset    = slideOffset;
            spriteFader.onFadeComplete = onComplete;
        }
        /**
         * Attaches a SpriteFader component to a sprite object to transition its color over time.
         */
        public static void FadeSprite(SpriteRenderer spriteRenderer, Color targetColor, float duration, Vector2 slideOffset, Action onComplete = null)
        {
            if (spriteRenderer == null)
            {
                Debug.LogError("Sprite renderer must not be null");
                return;
            }

            // Fade child sprite renderers
            SpriteRenderer[] children = spriteRenderer.gameObject.GetComponentsInChildren <SpriteRenderer>();
            foreach (SpriteRenderer child in children)
            {
                if (child == spriteRenderer)
                {
                    continue;
                }

                FadeSprite(child, targetColor, duration, slideOffset);
            }

            // Destroy any existing fader component
            SpriteFader oldSpriteFader = spriteRenderer.GetComponent <SpriteFader>();

            {
                Destroy(oldSpriteFader);
            }

            // Early out if duration is zero
            if (duration == 0f)
            {
                spriteRenderer.color = targetColor;
                if (onComplete != null)
                {
                    onComplete();
                }
                return;
            }

            // Set up color transition to be applied during update
            SpriteFader spriteFader = spriteRenderer.gameObject.AddComponent <SpriteFader>();

            spriteFader.fadeDuration   = duration;
            spriteFader.startColor     = spriteRenderer.color;
            spriteFader.endColor       = targetColor;
            spriteFader.endPosition    = spriteRenderer.transform.position;
            spriteFader.slideOffset    = slideOffset;
            spriteFader.onFadeComplete = onComplete;
        }
Beispiel #4
0
        public override void OnEnter()
        {
            if (spriteRenderer == null)
            {
                Continue();
                return;
            }

            SpriteFader.FadeSprite(spriteRenderer, _targetColor.Value, _duration.Value, Vector2.zero, delegate {
                if (waitUntilFinished)
                {
                    Continue();
                }
            });

            if (!waitUntilFinished)
            {
                Continue();
            }
        }