Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How to Tween alpha of an image?

Discussion in 'UGUI & TextMesh Pro' started by jerryberry, Aug 22, 2014.

  1. jerryberry

    jerryberry

    Joined:
    Feb 12, 2014
    Posts:
    6
    On NGUI it was done with TweenAlpha.Begin().
    How to fade in an image in new (awesome btw.) UI?
     
  2. Breyer

    Breyer

    Joined:
    Nov 10, 2012
    Posts:
    412
    maybe Graphics.CrossFadeAlpha?
     
  3. AntFitch

    AntFitch

    Joined:
    Jan 31, 2012
    Posts:
    243
    Bumping this. What if you have a group of objects? I'm using CanvasGroup to change the alpha of a message box & its contents, and this does not seem to be an option.

    (my bad, I was referencing the wrong object. this works.)
     
    Last edited: Feb 14, 2015
  4. Kogar

    Kogar

    Joined:
    Jun 27, 2013
    Posts:
    80
    Would also be interested in a clean solution.
    Currently i am getting every CanvasRenderer at the start. Then i am changing their alpha by looping trough them.
    Code (csharp):
    1. cr=GetComponentsInChildren<CanvasRenderer>();
    2. for(int i=0;i<cr.Length;i++)
    3. cr[i].SetAlpha(alphaValue);
    There should be an easier way to fadein/out a group of nested gameobjects.
     
  5. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,733
    I'm not sure what "this" refers to, since CanvasGroup does precisely what you describe.

    @Kogar use a CanvasGroup, that's exactly what they do.
     
    Zimbres and rakkarage like this.
  6. Kogar

    Kogar

    Joined:
    Jun 27, 2013
    Posts:
    80
    @StarManta Exactly what i needed. I had not tried CanvasGroup yet.
    Would be nice to have a fading included, but an IEnumerator does it too. I based mine on //http://answers.unity3d.com/questions/286453/quick-change-over-time-math.html
    Code (csharp):
    1. public float alphaFadeSeconds = 0.1f;
    2. private CanvasGroup cg; //get the component in start()
    3.  
    4. IEnumerator alphaFade(float alpha)
    5. {
    6.    float start = Time.time;
    7.    while(cg.alpha != alpha)
    8.    {
    9.      float elapsed = Time.time - start;
    10.      float normalisedTime = Mathf.Clamp((elapsed / alphaFadeSeconds)*Time.deltaTime, 0, 1);
    11.      cg.alpha = Mathf.Lerp(cg.alpha, alpha, normalisedTime) ;
    12.      yield return 0;  
    13.    }
    14.    yield return true;
    15. }
    16.  
    17. void setVisibility(float alpha = 0)
    18. {
    19.    StopCoroutine("alphaFade");  //stop currently fading coroutine
    20.    StartCoroutine("alphaFade", alpha); //start from current alpha state to target alpha
    21. }
     
    AntFitch likes this.
  7. cmcpasserby

    cmcpasserby

    Joined:
    Jul 18, 2014
    Posts:
    315
    in a few weeks i should have a nice tweening soultion in for the new UI system, that allows for straight up tweening or via curves, for some effects.
     
  8. secondbreakfast

    secondbreakfast

    Joined:
    Jan 5, 2013
    Posts:
    98
    Use CanvasGroup and fade that alpha. That has the benefit of fading every game object under it. You can animate this property through script or through Animation/Animator in any way you see fit with curves and all.
     
  9. Ricks

    Ricks

    Joined:
    Jun 17, 2010
    Posts:
    650
    Just stumbled on this issue as well. You could modifiy the default iTween script like this to make it work (though it should certainly be optimized...).

    Code (CSharp):
    1.  
    2. // Somewhere at line 3345 in method GenerateColorToTargets()
    3. else if(GetComponent<CanvasRenderer>())
    4. {
    5.     colors = new Color[1,3];
    6.     colors[0,0] = colors[0,1] = GetComponent<CanvasRenderer>().GetColor();
    7. }
    8.  
    Code (CSharp):
    1.  
    2. // Somewhere at line 4128 in method ApplyColorToTargets()
    3. else if(GetComponent<CanvasRenderer>())
    4. {
    5.     GetComponent<CanvasRenderer>().SetColor(colors[0,2]);
    6. }
    7.  
    Code (CSharp):
    1.  
    2. // Somewhere at line 4150 in method ApplyColorToTargets()
    3. else if(GetComponent<CanvasRenderer>())
    4. {
    5.     GetComponent<CanvasRenderer>().SetColor(colors[0,1]);
    6. }
    7.  


    Example call to fade out a child image of the Canvas GameObject:
    iTween.ColorTo(GameObject.Find("Canvas").transform.Find("Image").gameObject, new Color(0.0F,0.0F,0,0F), 1.0F);
     
    haiduongbk and drawcode like this.
  10. haiduongbk

    haiduongbk

    Joined:
    Oct 5, 2015
    Posts:
    6
    Awesome. It work. thank you