Browse Source

Replaced SpriteController with SpriteFader component.

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
chrisgregan 11 years ago
parent
commit
105f0400c8
  1. 10
      Assets/Fungus/Scripts/Button.cs
  2. 21
      Assets/Fungus/Scripts/Commands.cs
  3. 24
      Assets/Fungus/Scripts/Room.cs
  4. 108
      Assets/Fungus/Scripts/SpriteController.cs
  5. 97
      Assets/Fungus/Scripts/SpriteFader.cs
  6. 2
      Assets/Fungus/Scripts/SpriteFader.cs.meta
  7. BIN
      Assets/FungusExample/Scenes/Example.unity
  8. 8
      Assets/FungusExample/Scripts/SpritesRoom.cs

10
Assets/Fungus/Scripts/Button.cs

@ -6,19 +6,19 @@ namespace Fungus
{
// Simple button handler class.
// When the user taps on the button, the named method is called on ancestor game objects (if it exists).
[RequireComponent (typeof (SpriteController))]
[RequireComponent (typeof (Collider2D))]
[RequireComponent (typeof (SpriteRenderer))]
[RequireComponent (typeof (BoxCollider2D))]
public class Button : MonoBehaviour
{
public string methodName;
public SpriteController spriteController;
public SpriteRenderer spriteRenderer;
public bool autoDisable = false;
void Start()
{
spriteController = GetComponent<SpriteController>();
spriteRenderer = GetComponent<SpriteRenderer>();
}
void OnMouseUpAsButton()
@ -29,7 +29,7 @@ namespace Fungus
}
// Ignore button press if button is not fully visible
if (!spriteController.isShown)
if (spriteRenderer.color.a != 1f)
{
return;
}

21
Assets/Fungus/Scripts/Commands.cs

@ -316,31 +316,34 @@ namespace Fungus
// Fades a sprite to a given alpha value over a period of time
public class FadeSpriteCommand : CommandQueue.Command
{
SpriteController spriteController;
float targetAlpha;
SpriteRenderer spriteRenderer;
Color targetColor;
float fadeDuration;
Vector2 slideOffset = Vector2.zero;
public FadeSpriteCommand(SpriteController _spriteController,
float _targetAlpha,
public FadeSpriteCommand(SpriteRenderer _spriteRenderer,
Color _targetColor,
float _fadeDuration,
Vector2 _slideOffset)
{
if (_spriteController == null)
if (_spriteRenderer == null)
{
Debug.LogError("Sprite controller must not be null.");
Debug.LogError("Sprite renderer must not be null.");
return;
}
spriteController = _spriteController;
targetAlpha = _targetAlpha;
spriteRenderer = _spriteRenderer;
targetColor = _targetColor;
fadeDuration = _fadeDuration;
slideOffset = _slideOffset;
}
public override void Execute(CommandQueue commandQueue, Action onComplete)
{
spriteController.SlideFade(targetAlpha, fadeDuration, slideOffset);
SpriteFader.FadeSprite(spriteRenderer, targetColor, fadeDuration, slideOffset);
// Fade is asynchronous, but command completes immediately.
// If you need to wait for the fade to complete, just use an additional Wait() command
if (onComplete != null)
{
onComplete();

24
Assets/Fungus/Scripts/Room.cs

@ -294,27 +294,35 @@ namespace Fungus
}
// Sets sprite alpha to 0 immediately
public void HideSprite(SpriteController spriteController)
public void HideSprite(SpriteRenderer spriteRenderer)
{
commandQueue.AddCommand(new FadeSpriteCommand(spriteController, 0f, 0f, Vector2.zero));
Color color = spriteRenderer.color;
color.a = 0f;
commandQueue.AddCommand(new FadeSpriteCommand(spriteRenderer, color, 0f, Vector2.zero));
}
// Sets sprite alpha to 1 immediately
public void ShowSprite(SpriteController spriteController)
public void ShowSprite(SpriteRenderer spriteRenderer)
{
commandQueue.AddCommand(new FadeSpriteCommand(spriteController, 1f, 0f, Vector2.zero));
Color color = spriteRenderer.color;
color.a = 1f;
commandQueue.AddCommand(new FadeSpriteCommand(spriteRenderer, color, 0f, Vector2.zero));
}
// Fades a sprite to a given alpha value over a period of time
public void FadeSprite(SpriteController spriteController, float targetAlpha, float duration)
public void FadeSprite(SpriteRenderer spriteRenderer, float targetAlpha, float duration)
{
commandQueue.AddCommand(new FadeSpriteCommand(spriteController, targetAlpha, duration, Vector2.zero));
Color color = spriteRenderer.color;
color.a = targetAlpha;
commandQueue.AddCommand(new FadeSpriteCommand(spriteRenderer, color, duration, Vector2.zero));
}
// Fades a sprite to a given alpha value over a period of time, and applies a sliding motion to the sprite transform
public void FadeSprite(SpriteController spriteController, float targetAlpha, float duration, Vector2 slideOffset)
public void FadeSprite(SpriteRenderer spriteRenderer, float targetAlpha, float duration, Vector2 slideOffset)
{
commandQueue.AddCommand(new FadeSpriteCommand(spriteController, targetAlpha, duration, slideOffset));
Color color = spriteRenderer.color;
color.a = targetAlpha;
commandQueue.AddCommand(new FadeSpriteCommand(spriteRenderer, color, duration, slideOffset));
}
// Sets an animator trigger to change the animation state for an animated sprite

108
Assets/Fungus/Scripts/SpriteController.cs

@ -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;
}
}
}
}

97
Assets/Fungus/Scripts/SpriteFader.cs

@ -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);
}
}
}
}
}

2
Assets/Fungus/Scripts/SpriteController.cs.meta → Assets/Fungus/Scripts/SpriteFader.cs.meta

@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: fc308840c2cad4e02af85d79616861fd
guid: ccc9787d88a4648e69e076f72ee540dc
MonoImporter:
serializedVersion: 2
defaultReferences: []

BIN
Assets/FungusExample/Scenes/Example.unity

Binary file not shown.

8
Assets/FungusExample/Scripts/SpritesRoom.cs

@ -6,10 +6,14 @@ public class SpritesRoom : Room
{
public Room menuRoom;
public Animator blueAlienAnim;
public SpriteController blueAlienSprite;
public SpriteRenderer blueAlienSprite;
public SpriteRenderer redMushroomSprite;
void OnEnter()
{
HideSprite(redMushroomSprite);
FadeSprite(redMushroomSprite, 1f, 2f);
ShowSprite(blueAlienSprite);
Say("Pink Alien says to Blue Alien...");
@ -19,6 +23,8 @@ public class SpritesRoom : Room
Say("Blue Alien starts to dance.");
Say("Tap on Blue Alien to stop him dancing.");
FadeSprite(redMushroomSprite, 0f, 2f);
}
// This method is called from the Button component on the BlueAlien object

Loading…
Cancel
Save