Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question for CrossFadeAlpha

Discussion in 'Getting Started' started by vegetamaker, Jun 22, 2017.

  1. vegetamaker

    vegetamaker

    Joined:
    Feb 28, 2017
    Posts:
    37
    Hello! I just discovered this command (and it was relly usefull!). But I have a problem using it. Let me explain:

    I did a script for use it for all my UI components and it is working perfectly for fade out (alpha 1 to 0). But if I want the reverse (alpha 0 to 1) it isn't working if I disabled my UI element before (SetActive(false)).

    The only way that I find to fix it was disable GraphicRaycaster after set alpha to 0, so UI element is there, but player can't interact or see it. Of course I guess this isn't a good way to do it if game have alot more UI elemnts, but was enough good for a simple project.

    So the question is... is there a way to do a CrossFadeAlpha working from 0 to 1 alpha value just after set a UI element active with an alpha 0 (that it was set previously)?

    Thanks so much ^^

    Code (CSharp):
    1.     //=============================================
    2.  
    3.     /// <summary>
    4.     /// Use this for check all Children or specific GameObject (remember use checkchildren to false for this feature).
    5.     /// </summary>
    6.     /// <param name="gameObject">Game object.</param>
    7.     /// <param name="checkchildren">If set to <c>true</c> checkchildren.</param>
    8.     /// <param name="newAlpha">0 for transparent, 255 opaque.</param>
    9.     /// <param name="duration">Duration.</param>
    10.     /// <param name="ignoreTimeScale">If set to <c>true</c> ignore time scale.</param>
    11.     public void FadingItems(GameObject gameObject, bool checkchildren, float newAlpha, float duration, bool ignoreTimeScale, bool enableGameObject)
    12.     {
    13.  
    14.         if (newAlpha < 0f)
    15.             newAlpha = 0f;
    16.         if (newAlpha > 1f)
    17.             newAlpha = 1f;
    18.  
    19.         if (enableGameObject) {
    20.             StartCoroutine (EnableGameObject (gameObject, 0f));
    21.         } else {
    22.             StartCoroutine (DisableGameObject (gameObject, duration + 0.01f));
    23.         }
    24.  
    25.         if (checkchildren) {
    26.             foreach (Transform reference in gameObject.GetComponentsInChildren<Transform>()) {            
    27.                 Fade(reference.gameObject, newAlpha, duration, ignoreTimeScale);
    28.             }
    29.  
    30.         } else {
    31.             Fade(gameObject, newAlpha, duration, ignoreTimeScale);
    32.         }
    33.  
    34.  
    35.         foreach (Transform reference in gameObject.GetComponentsInChildren<Transform>()) {
    36.             if (reference.gameObject.GetComponent<Button> ()) {
    37.                 if (enableGameObject) {
    38.                     reference.gameObject.GetComponent<Button> ().enabled = true;
    39.                     reference.gameObject.GetComponent<Image> ().enabled = true;
    40.                 } else {
    41.                     reference.gameObject.GetComponent<Button> ().enabled = false;
    42.                     reference.gameObject.GetComponent<Image> ().enabled = false;
    43.                 }
    44.             }
    45.         }
    46.  
    47.  
    48.  
    49.     }
    50.     //===========Fading or Unfading Objects=================================================================================
    51.  
    52.     private void Fade(GameObject reference, float newAlpha, float duration, bool ignoreTimeScale)
    53.     {
    54.         if (reference.gameObject.GetComponent<Text> ()) {
    55.             reference.gameObject.GetComponent<Text> ().CrossFadeAlpha (newAlpha, duration, ignoreTimeScale);
    56.         } else {
    57.             if (reference.gameObject.GetComponent<Image> ()) {
    58.                 reference.gameObject.GetComponent<Image> ().CrossFadeAlpha (newAlpha, duration, ignoreTimeScale);
    59.             }
    60.         }
    61.  
    62.     }
    63.  
    64.     //===================================================================================================================
    65.  
    66.     IEnumerator DisableGameObject(GameObject reference, float delay)
    67.     {
    68.         yield return new WaitForSeconds (delay);
    69.         //reference.gameObject.SetActive (false);
    70.         try{
    71.         reference.gameObject.GetComponentInChildren<GraphicRaycaster>().enabled = false;
    72.         }catch{
    73.         };
    74.     }
    75.  
    76.     //===================================================================================================================
    77.  
    78.     IEnumerator EnableGameObject(GameObject reference, float delay)
    79.     {
    80.         yield return new WaitForSeconds (delay);
    81.         //reference.gameObject.SetActive (true);
    82.         try{
    83.         reference.gameObject.GetComponentInChildren<GraphicRaycaster>().enabled = true;
    84.         }catch{
    85.         };
    86.  
    87.     }
    88.  
    89.  
    90.  
    91.  
    92.  
    93.  
    94.  
    95.  
    96.  
    97.  
    98.  
    99.  
    100.  
    101.  
     
  2. Taorcb

    Taorcb

    Joined:
    Mar 15, 2015
    Posts:
    39
    CrossFadeAlpha() can't have an initial alpha of zero. The function works by multiplying the values similar to a Lerp() function, so if you start with zero you'll end up multiplying by zero and the function will always return zero. Try setting the graphic's alpha to something really small (I'd suggest 0.00392156863, which is an alpha value of 1 in RGB-speak).

    Also this belongs in the Scripting or UI forums, not the Getting Started one.
     
    vegetamaker likes this.
  3. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Yeah, this is a known bug with CrossFadeAlpha (and a really annoying one, IMHO — it looks like it's doing a Lerp, but it's not).

    @Taorcb's work-around should work, though.
     
    vegetamaker likes this.
  4. vegetamaker

    vegetamaker

    Joined:
    Feb 28, 2017
    Posts:
    37
    Thanks @Taorcb and @JoeStrout . I'll edit my script to follow the instructions.

    Sorry for it. I am still very novice with Unity, so I thought that my forum is this one yet. I'll post my questions in the correct forum next time.

    Thanks again!
     
  5. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Actually, I think this was a fine Getting Started question. But Scripting or UI would have also been fine.
     
    vegetamaker likes this.