- Home /
LeanTween alpha problems
Hello! I'm trying to use LeanTween to tween the alpha of a Game Object and having som trouble. here's my code:
using UnityEngine;
public class Fade : MonoBehaviour {
public float duration = 1f;
public float transparent = 0f;
public float opaque = 1f;
void Start ()
{
}
void OnMouseDown ()
{
FadeObjectIn ();
}
void FadeObjectIn ()
{
print ("Fading color in");
// Fade GO in from 0 to 1, but it doesn't work?
LeanTween.alpha(gameObject, opaque, duration);
}
}
Basically, when the player clicks the Gme Object, it's supposed to fade in from transparent to Opaque and it doesn't work. If I try to fade in from Opaque to transparent, it works but the speed is super slow, like 20 times slower than it should be. Anyone know what's up?
LeanTween version: 2.14 Unity Version: 4.3.4f1 Shader: Transparent/Bumped Defuse
Answer by Suddan · Apr 26, 2014 at 11:48 PM
I haven't used LeanTween yet.
But for now, you can try the following code which uses a coroutine:
void OnMouseDown()
{
// you might want to make sure at this point that coroutines of the same type will be stopped first
StartCoroutine(FadeIn(duration)); // duration variable from your code sample
}
IEnumerator FadeIn(float timer)
{
Color temp = new Color();
while (renderer.material.color.a < opaque) // opaque variable from your code sample
{
temp = renderer.material.color;
temp.a += Time.deltaTime/timer;
renderer.material.color = temp;
yield return null;
}
temp.a = 1f; // set it back to exactly 1f
renderer.material.color = temp;
}
Interesting solution but have you tried the code? it doesn't seem to fade in but ins$$anonymous$$d immediately turns opaque (no matter what number you set the duration to)
It should work, try to set the color's alpha (shader should be a type of transparent one) to anything less than 1 before you call the function. If you do not so, the function wont' do anything because its made for the fade in only. You can change this though.
Answer by dentedpixel · Apr 27, 2014 at 01:47 PM
Hey Roman!
I am not sure what the problem is, I just tried tweening that shader and it seems to be working fine. Check out the AlphaTesting scene from the github page: https://github.com/dentedpixel/LeanTween to see it in action.
Maybe you are calling the tweening code multiple times? I find it's best to do things on MouseUp, so that you can insure that it is only fired once...
Answer by pencilking2002 · Apr 28, 2014 at 04:54 PM
I think the problem might have been that I didn't know about and therefore didn't follow the instructions for setting up LeanTween, which are located on github: https://github.com/dentedpixel/LeanTween
Answer by silentreaver · Apr 30, 2021 at 04:59 PM
The alphaFade was causing me issues where it would stop half way or not even start, so I rewrote it as a coroutine to guarantee it would always work for me:
StartCoroutine(FadeToAlpha(canvasGroup, 0, 1));
IEnumerator FadeToAlpha(CanvasGroup canvasGroup, float targetAlpha, float fadeTime)
{
float startingAlpha = canvasGroup.alpha;
for (float i = 0; i < 1; i+= Time.deltaTime/fadeTime)
{
canvasGroup.alpha = Mathf.Lerp(startingAlpha, targetAlpha, i);
yield return new WaitForFixedUpdate();
}
canvasGroup.alpha = targetAlpha;
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613143649im_/https://answers.unity.com/themes/thub/images/avi.jpg)