/// <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; }
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(); } }