Skip to content

Commit

Permalink
Added some support for sprite color switching
Browse files Browse the repository at this point in the history
  • Loading branch information
afauch committed Dec 8, 2017
1 parent a9aa0bb commit 15d36b9
Show file tree
Hide file tree
Showing 65 changed files with 4,024 additions and 22 deletions.
26 changes: 18 additions & 8 deletions crayon-dev/Assets/Crayon/Scripts/Library/CrayonRouter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,26 @@ public static void Fade(GameObject gameObject, FadeDirection fadeDirection, floa
/// </summary>
public static void TweenColor(GameObject gameObject, Color targetColor, float duration, Easing easing, string cubicBezier)
{
// Create instance of material
Renderer r = gameObject.GetComponent<Renderer>();
if (r == null)

// Is this a sprite renderer?
SpriteRenderer sr = gameObject.GetComponent<SpriteRenderer>();
if (sr != null)
{
Debug.LogWarning ("Is a sprite");
CrayonRunner.Instance.Run (CrayonTweenCoroutines.TweenSpriteCoroutine(gameObject, sr.color, targetColor, duration, easing, cubicBezier));
}
else
{
Debug.LogWarningFormat ("{0} needs a renderer component.", gameObject.name);
return;
// Create instance of material
Renderer r = gameObject.GetComponent<Renderer> ();
if (r == null) {
Debug.LogWarningFormat ("{0} needs a renderer component.", gameObject.name);
return;
}
Material targetMaterial = Object.Instantiate (r.material);
targetMaterial.SetColor ("_Color", targetColor);
CrayonRunner.Instance.Run (CrayonTweenCoroutines.TweenMaterialCoroutine (gameObject, null, targetMaterial, duration, easing, cubicBezier));
}
Material targetMaterial = Object.Instantiate(r.material);
targetMaterial.SetColor("_Color", targetColor);
CrayonRunner.Instance.Run (CrayonTweenCoroutines.TweenMaterialCoroutine (gameObject, null, targetMaterial, duration, easing, cubicBezier));
}

/// <summary>
Expand Down
44 changes: 44 additions & 0 deletions crayon-dev/Assets/Crayon/Scripts/Library/CrayonTweenCoroutines.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public static IEnumerator FadeCoroutine(GameObject gameObject, FadeDirection fad
/// </summary>
public static IEnumerator TweenMaterialCoroutine(GameObject gameObject, Material startMaterial, Material endMaterial, float duration, Easing easing, string cubicBezier) {
float elapsedTime = 0;

// get the starting material and color
Renderer r = gameObject.GetComponent<Renderer>();
// this is the material we'll tween
Expand Down Expand Up @@ -111,6 +112,49 @@ public static IEnumerator TweenMaterialCoroutine(GameObject gameObject, Material

}

/// <summary>
/// Coroutine for tweening material or color-only properties.
/// </summary>
public static IEnumerator TweenSpriteCoroutine(GameObject gameObject, Color startColor, Color endColor, float duration, Easing easing, string cubicBezier) {
float elapsedTime = 0;

// get the starting material and color
SpriteRenderer sr = gameObject.GetComponent<SpriteRenderer>();

// If start material is null, assume we're tweening FROM the current material
if (startColor == null)
{
startColor = sr.color;
}

// If end material is null, assume we're tweening TO the current material
if (endColor == null)
{
endColor = sr.color;
}

// Skip ahead if this is meant to be an immediate transition.
if (duration < 0.0001f)
{
sr.color = endColor;
}
else
{
while (elapsedTime < duration)
{
float t = elapsedTime / duration;
// shift 't' based on the easing function
t = Utils.GetT (t, easing, cubicBezier);
elapsedTime += Time.deltaTime;
Color currentColor = Color.Lerp (startColor, endColor, t);
sr.color = currentColor;
yield return null;
}
sr.color = endColor;
}

}

/// <summary>
/// Coroutine for tweening position.
/// </summary>
Expand Down
Loading

0 comments on commit 15d36b9

Please sign in to comment.