You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
118 lines
4.0 KiB
118 lines
4.0 KiB
8 years ago
|
// This code is part of the Fungus library (http://fungusgames.com) maintained by Chris Gregan (http://twitter.com/gofungus).
|
||
|
// It is released for free under the MIT open source license (https://github.com/snozbot/fungus/blob/master/LICENSE)
|
||
9 years ago
|
|
||
11 years ago
|
using UnityEngine;
|
||
10 years ago
|
using System;
|
||
11 years ago
|
|
||
|
namespace Fungus
|
||
|
{
|
||
8 years ago
|
/// <summary>
|
||
|
/// Transitions a sprite from its current color to a target color.
|
||
|
/// An offset can be applied to slide the sprite in while changing color.
|
||
|
/// </summary>
|
||
8 years ago
|
[RequireComponent (typeof (SpriteRenderer))]
|
||
|
public class SpriteFader : MonoBehaviour
|
||
|
{
|
||
|
protected float fadeDuration;
|
||
|
protected float fadeTimer;
|
||
|
protected Color startColor;
|
||
|
protected Color endColor;
|
||
|
protected Vector2 slideOffset;
|
||
|
protected Vector3 endPosition;
|
||
11 years ago
|
|
||
8 years ago
|
protected SpriteRenderer spriteRenderer;
|
||
11 years ago
|
|
||
8 years ago
|
protected Action onFadeComplete;
|
||
10 years ago
|
|
||
8 years ago
|
/// <summary>
|
||
|
/// Attaches a SpriteFader component to a sprite object to transition its color over time.
|
||
|
/// </summary>
|
||
8 years ago
|
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;
|
||
|
}
|
||
11 years ago
|
|
||
8 years ago
|
// Fade child sprite renderers
|
||
|
SpriteRenderer[] children = spriteRenderer.gameObject.GetComponentsInChildren<SpriteRenderer>();
|
||
|
foreach (SpriteRenderer child in children)
|
||
|
{
|
||
|
if (child == spriteRenderer)
|
||
|
{
|
||
|
continue;
|
||
|
}
|
||
|
|
||
|
FadeSprite(child, targetColor, duration, slideOffset);
|
||
|
}
|
||
11 years ago
|
|
||
8 years ago
|
// Destroy any existing fader component
|
||
|
SpriteFader oldSpriteFader = spriteRenderer.GetComponent<SpriteFader>();
|
||
9 years ago
|
if (oldSpriteFader != null)
|
||
8 years ago
|
{
|
||
|
Destroy(oldSpriteFader);
|
||
|
}
|
||
11 years ago
|
|
||
8 years ago
|
// Early out if duration is zero
|
||
|
if (duration == 0f)
|
||
|
{
|
||
|
spriteRenderer.color = targetColor;
|
||
|
if (onComplete != null)
|
||
|
{
|
||
|
onComplete();
|
||
|
}
|
||
|
return;
|
||
|
}
|
||
11 years ago
|
|
||
8 years ago
|
// 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;
|
||
|
}
|
||
11 years ago
|
|
||
8 years ago
|
protected virtual void Start()
|
||
|
{
|
||
|
spriteRenderer = GetComponent<Renderer>() as SpriteRenderer;
|
||
|
}
|
||
11 years ago
|
|
||
8 years ago
|
protected virtual void Update()
|
||
|
{
|
||
|
fadeTimer += Time.deltaTime;
|
||
|
if (fadeTimer > fadeDuration)
|
||
|
{
|
||
|
// Snap to final values
|
||
|
spriteRenderer.color = endColor;
|
||
|
if (slideOffset.magnitude > 0)
|
||
|
{
|
||
|
transform.position = endPosition;
|
||
|
}
|
||
11 years ago
|
|
||
8 years ago
|
// Remove this component when transition is complete
|
||
|
Destroy(this);
|
||
10 years ago
|
|
||
8 years ago
|
if (onFadeComplete != null)
|
||
|
{
|
||
|
onFadeComplete();
|
||
|
}
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
float t = Mathf.SmoothStep(0, 1, fadeTimer / fadeDuration);
|
||
|
spriteRenderer.color = Color.Lerp(startColor, endColor, t);
|
||
|
if (slideOffset.magnitude > 0)
|
||
|
{
|
||
|
Vector3 startPosition = endPosition;
|
||
|
startPosition.x += slideOffset.x;
|
||
|
startPosition.y += slideOffset.y;
|
||
|
transform.position = Vector3.Lerp(startPosition, endPosition, t);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
11 years ago
|
}
|