Skip to content

Instantly share code, notes, and snippets.

@markusrt
Created September 3, 2012 22:04
Show Gist options
  • Select an option

  • Save markusrt/3613941 to your computer and use it in GitHub Desktop.

Select an option

Save markusrt/3613941 to your computer and use it in GitHub Desktop.

Revisions

  1. markusrt revised this gist Sep 3, 2012. 1 changed file with 59 additions and 61 deletions.
    120 changes: 59 additions & 61 deletions FadeSprite.cs
    Original file line number Diff line number Diff line change
    @@ -2,69 +2,67 @@
    using System.Collections;

    public class FadeSprite : MonoBehaviour {

    public enum FadeDirection { FadeOut, FadeIn }

    public FadeDirection Direction = FadeDirection.FadeOut;

    public float Speed = 0.0f;

    public bool Loop;

    private OTSprite sprite;

    private float CurrentDirection
    {
    get { return Direction == FadeDirection.FadeOut ? -1.0f : 1.0f; }
    }

    void Start ()
    {
    sprite = gameObject.GetComponent<OTSprite>();

    if(sprite == null)
    {
    Debug.LogWarning("FadeSprite script only works with orthello sprites");
    }
    }

    void Update ()
    {
    if (sprite == null)
    {
    return;
    }

    ChangeAlpha ();
    CheckLoopCondition ();
    }

    private void ChangeAlpha ()
    {
    sprite.alpha += Time.deltaTime * Speed * CurrentDirection;
    }
    public enum FadeDirection { FadeOut, FadeIn }

    void CheckLoopCondition ()
    {
    var fadeOutCompleted = sprite.alpha < 0.0f;
    bool fadeInCompleted = sprite.alpha > 1.0f;

    if( Loop && ( fadeOutCompleted || fadeInCompleted ) )
    {
    ToggleFadeDirection();
    }
    public FadeDirection Direction = FadeDirection.FadeOut;

    public float Speed = 0.0f;

    public bool Loop;

    private OTSprite sprite;

    private float CurrentDirection
    {
    get { return Direction == FadeDirection.FadeOut ? -1.0f : 1.0f; }
    }

    void Start ()
    {
    sprite = gameObject.GetComponent<OTSprite>();

    if(sprite == null)
    {
    Debug.LogWarning("FadeSprite script only works with orthello sprites");
    }
    }

    void Update ()
    {
    if (sprite == null)
    {
    return;
    }

    if( !Loop && fadeOutCompleted )
    {
    Destroy(gameObject);
    }
    }

    private void ToggleFadeDirection()
    {
    Direction = Direction == FadeDirection.FadeOut
    ? FadeDirection.FadeIn : FadeDirection.FadeOut;
    }
    }
    ChangeAlpha ();
    CheckLoopCondition ();
    }

    private void ChangeAlpha ()
    {
    sprite.alpha += Time.deltaTime * Speed * CurrentDirection;
    }

    void CheckLoopCondition ()
    {
    var fadeOutCompleted = sprite.alpha < 0.0f;
    bool fadeInCompleted = sprite.alpha > 1.0f;

    if( Loop && ( fadeOutCompleted || fadeInCompleted ) )
    {
    ToggleFadeDirection();
    }

    if( !Loop && fadeOutCompleted )
    {
    Destroy(gameObject);
    }
    }

    private void ToggleFadeDirection()
    {
    Direction = Direction == FadeDirection.FadeOut
    ? FadeDirection.FadeIn : FadeDirection.FadeOut;
    }
    }
  2. markusrt created this gist Sep 3, 2012.
    70 changes: 70 additions & 0 deletions FadeSprite.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,70 @@
    using UnityEngine;
    using System.Collections;

    public class FadeSprite : MonoBehaviour {

    public enum FadeDirection { FadeOut, FadeIn }

    public FadeDirection Direction = FadeDirection.FadeOut;

    public float Speed = 0.0f;

    public bool Loop;

    private OTSprite sprite;

    private float CurrentDirection
    {
    get { return Direction == FadeDirection.FadeOut ? -1.0f : 1.0f; }
    }

    void Start ()
    {
    sprite = gameObject.GetComponent<OTSprite>();

    if(sprite == null)
    {
    Debug.LogWarning("FadeSprite script only works with orthello sprites");
    }
    }

    void Update ()
    {
    if (sprite == null)
    {
    return;
    }

    ChangeAlpha ();
    CheckLoopCondition ();
    }

    private void ChangeAlpha ()
    {
    sprite.alpha += Time.deltaTime * Speed * CurrentDirection;
    }

    void CheckLoopCondition ()
    {
    var fadeOutCompleted = sprite.alpha < 0.0f;
    bool fadeInCompleted = sprite.alpha > 1.0f;

    if( Loop && ( fadeOutCompleted || fadeInCompleted ) )
    {
    ToggleFadeDirection();
    }

    if( !Loop && fadeOutCompleted )
    {
    Destroy(gameObject);
    }
    }

    private void ToggleFadeDirection()
    {
    Direction = Direction == FadeDirection.FadeOut
    ? FadeDirection.FadeIn : FadeDirection.FadeOut;
    }
    }