Browse Source
The new way to fade a sprite is to attach a SpriteFader component to the sprite at runtime (instead of doing in at design time in the editor).master
8 changed files with 138 additions and 132 deletions
@ -1,108 +0,0 @@ |
|||||||
using UnityEngine; |
|
||||||
using System.Collections; |
|
||||||
|
|
||||||
namespace Fungus |
|
||||||
{ |
|
||||||
// Extends sprite functionality with support for fading |
|
||||||
[RequireComponent (typeof (SpriteRenderer))] |
|
||||||
public class SpriteController : MonoBehaviour |
|
||||||
{ |
|
||||||
[HideInInspector] |
|
||||||
public bool isShown; |
|
||||||
|
|
||||||
private float spriteAlpha; |
|
||||||
|
|
||||||
private float fadeDuration; |
|
||||||
private float fadeTimer; |
|
||||||
private float startAlpha; |
|
||||||
private float endAlpha; |
|
||||||
private Vector3 startPosition; |
|
||||||
private Vector3 startSlideOffset; |
|
||||||
|
|
||||||
void Start() |
|
||||||
{ |
|
||||||
SpriteRenderer spriteRenderer = renderer as SpriteRenderer; |
|
||||||
spriteAlpha = spriteRenderer.color.a; |
|
||||||
} |
|
||||||
|
|
||||||
void Update() |
|
||||||
{ |
|
||||||
if (fadeDuration > 0f) |
|
||||||
{ |
|
||||||
fadeTimer += Time.deltaTime; |
|
||||||
if (fadeTimer > fadeDuration) |
|
||||||
{ |
|
||||||
spriteAlpha = endAlpha; |
|
||||||
fadeDuration = 0; |
|
||||||
} |
|
||||||
else |
|
||||||
{ |
|
||||||
float t = Mathf.SmoothStep(0, 1, fadeTimer / fadeDuration); |
|
||||||
spriteAlpha = Mathf.Lerp(startAlpha, endAlpha, t); |
|
||||||
transform.position = Vector3.Lerp(startPosition + startSlideOffset, startPosition, t); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
UpdateSpriteColor(); |
|
||||||
} |
|
||||||
|
|
||||||
void UpdateSpriteColor() |
|
||||||
{ |
|
||||||
isShown = (spriteAlpha == 1f); |
|
||||||
|
|
||||||
SpriteRenderer spriteRenderer = renderer as SpriteRenderer; |
|
||||||
|
|
||||||
Color color = spriteRenderer.material.color; |
|
||||||
color.a = spriteAlpha; |
|
||||||
spriteRenderer.material.color = color; |
|
||||||
|
|
||||||
SpriteRenderer[] children = gameObject.GetComponentsInChildren<SpriteRenderer>(); |
|
||||||
foreach (SpriteRenderer child in children) |
|
||||||
{ |
|
||||||
Color childColor = child.material.color; |
|
||||||
childColor.a = spriteAlpha; |
|
||||||
child.material.color = childColor; |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
public void Fade(float targetAlpha, float duration) |
|
||||||
{ |
|
||||||
if (duration == 0f) |
|
||||||
{ |
|
||||||
spriteAlpha = targetAlpha; |
|
||||||
return; |
|
||||||
} |
|
||||||
|
|
||||||
fadeDuration = duration; |
|
||||||
fadeTimer = 0; |
|
||||||
|
|
||||||
startAlpha = spriteAlpha; |
|
||||||
endAlpha = targetAlpha; |
|
||||||
|
|
||||||
startPosition = transform.position; |
|
||||||
startSlideOffset = Vector3.zero; |
|
||||||
|
|
||||||
SpriteController[] children = gameObject.GetComponentsInChildren<SpriteController>(); |
|
||||||
foreach (SpriteController child in children) |
|
||||||
{ |
|
||||||
if (child == this) |
|
||||||
{ |
|
||||||
continue; |
|
||||||
} |
|
||||||
|
|
||||||
child.Fade(targetAlpha, duration); |
|
||||||
} |
|
||||||
|
|
||||||
} |
|
||||||
|
|
||||||
public void SlideFade(float targetAlpha, float duration, Vector2 slideOffset) |
|
||||||
{ |
|
||||||
Fade(targetAlpha, duration); |
|
||||||
|
|
||||||
if (duration > 0) |
|
||||||
{ |
|
||||||
startSlideOffset = slideOffset; |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
@ -0,0 +1,97 @@ |
|||||||
|
using UnityEngine; |
||||||
|
using System.Collections; |
||||||
|
|
||||||
|
namespace Fungus |
||||||
|
{ |
||||||
|
// Transitions a sprite from its current color to a target color. |
||||||
|
// An offset can be applied to slide the sprite in while changing color. |
||||||
|
[RequireComponent (typeof (SpriteRenderer))] |
||||||
|
public class SpriteFader : MonoBehaviour |
||||||
|
{ |
||||||
|
float fadeDuration; |
||||||
|
float fadeTimer; |
||||||
|
Color startColor; |
||||||
|
Color endColor; |
||||||
|
Vector2 slideOffset; |
||||||
|
Vector3 endPosition; |
||||||
|
|
||||||
|
SpriteRenderer spriteRenderer; |
||||||
|
|
||||||
|
// 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) |
||||||
|
{ |
||||||
|
if (spriteRenderer == null) |
||||||
|
{ |
||||||
|
Debug.LogError("Sprite renderer must not be null"); |
||||||
|
return; |
||||||
|
} |
||||||
|
|
||||||
|
// Destroy any existing fader component |
||||||
|
SpriteFader oldSpriteFader = spriteRenderer.GetComponent<SpriteFader>(); |
||||||
|
{ |
||||||
|
Destroy(oldSpriteFader); |
||||||
|
} |
||||||
|
|
||||||
|
// Early out if duration is zero |
||||||
|
if (duration == 0f) |
||||||
|
{ |
||||||
|
spriteRenderer.color = targetColor; |
||||||
|
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; |
||||||
|
|
||||||
|
// Fade child sprite renderers |
||||||
|
SpriteRenderer[] children = spriteRenderer.gameObject.GetComponentsInChildren<SpriteRenderer>(); |
||||||
|
foreach (SpriteRenderer child in children) |
||||||
|
{ |
||||||
|
if (child == spriteRenderer) |
||||||
|
{ |
||||||
|
continue; |
||||||
|
} |
||||||
|
|
||||||
|
FadeSprite(child, targetColor, duration, slideOffset); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
void Start() |
||||||
|
{ |
||||||
|
spriteRenderer = renderer as SpriteRenderer; |
||||||
|
} |
||||||
|
|
||||||
|
void Update() |
||||||
|
{ |
||||||
|
fadeTimer += Time.deltaTime; |
||||||
|
if (fadeTimer > fadeDuration) |
||||||
|
{ |
||||||
|
// Snap to final values |
||||||
|
spriteRenderer.color = endColor; |
||||||
|
if (slideOffset.magnitude > 0) |
||||||
|
{ |
||||||
|
transform.position = endPosition; |
||||||
|
} |
||||||
|
|
||||||
|
// Remove this component when transition is complete |
||||||
|
Destroy(this); |
||||||
|
} |
||||||
|
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); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -1,5 +1,5 @@ |
|||||||
fileFormatVersion: 2 |
fileFormatVersion: 2 |
||||||
guid: fc308840c2cad4e02af85d79616861fd |
guid: ccc9787d88a4648e69e076f72ee540dc |
||||||
MonoImporter: |
MonoImporter: |
||||||
serializedVersion: 2 |
serializedVersion: 2 |
||||||
defaultReferences: [] |
defaultReferences: [] |
Binary file not shown.
Loading…
Reference in new issue