Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by pencilking2002 · Apr 26, 2014 at 09:57 PM · alphatweening

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
0

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;
     }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image pencilking2002 · Apr 27, 2014 at 01:42 PM 0
Share

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)

avatar image Suddoha · Apr 28, 2014 at 08:02 PM 0
Share

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.

avatar image
0

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...

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Convert to .tiff, with fbx export, removes the alpha channel? 1 Answer

GUI.DrawTexture - Slightly Transparent? 1 Answer

Bad slowdown with particles on iOS 1 Answer

Calling "Generate Alpha From Grayscale" from scripting?? 2 Answers

Transparent Diffuse Specular Normal Shader problem 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges