Created
December 16, 2015 18:09
-
-
Save unitycoder/68676f7a3094ee3c029a to your computer and use it in GitHub Desktop.
Lerp Material Color inside Update Loop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
using UnityEngine.UI; | |
public class LerpColor : MonoBehaviour { | |
public float lerpDuration = 2; | |
public Slider slider; | |
public Text textTime; | |
Material mat; | |
Color originalColor; | |
Color targetColor = Color.clear; | |
float lerpCounter = 0; | |
bool isLerping = false; | |
float timeCounter=0; | |
void Start () | |
{ | |
mat = GetComponent<Renderer> ().material; | |
originalColor = mat.color; | |
} | |
void Update () | |
{ | |
if (Input.GetKeyDown (KeyCode.Space)) | |
{ | |
lerpCounter = 0; | |
isLerping = !isLerping; | |
slider.value = lerpCounter; | |
timeCounter = 0; | |
textTime.text = "Lerp: " + lerpCounter + " | Time: " + timeCounter; | |
mat.color = originalColor; | |
} | |
if (isLerping) | |
{ | |
lerpCounter += Time.deltaTime/lerpDuration; | |
mat.color = Color.Lerp (originalColor, targetColor, lerpCounter); | |
timeCounter += Time.deltaTime; | |
slider.value = lerpCounter; | |
textTime.text = "Lerp: " + lerpCounter + " | Time: " + timeCounter; | |
if (lerpCounter >= 1) { | |
isLerping = false; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment