Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Is there any way to fade in out any object on it's alpha color ?

Discussion in 'Scripting' started by SharonL75, Sep 12, 2020.

  1. SharonL75

    SharonL75

    Joined:
    Aug 13, 2020
    Posts:
    91
    This code is working for Image and if I will change the Image to Text and then it will fade in out an object with Text component.

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.SceneManagement;
    6. using UnityEngine.UI;
    7.  
    8. public class Fader : MonoBehaviour
    9. {
    10.     #region FIELDS
    11.     public GameObject[] fadeOutUIGameobjects;
    12.     public float fadeSpeed = 0.8f;
    13.     public bool fadeOnStart = false;
    14.  
    15.     private Image fadeOutUIImage;
    16.  
    17.     private void Start()
    18.     {
    19.         if (fadeOnStart == true)
    20.         {
    21.            StartCoroutine(Fade(FadeDirection.Out));
    22.         }
    23.     }
    24.  
    25.     public enum FadeDirection
    26.     {
    27.         In, //Alpha = 1
    28.         Out // Alpha = 0
    29.     }
    30.     #endregion
    31.  
    32.     #region FADE
    33.     public IEnumerator Fade(FadeDirection fadeDirection)
    34.     {
    35.         for (int i = 0; i < fadeOutUIGameobjects.Length; i++)
    36.         {
    37.             fadeOutUIGameobjects[i].SetActive(true);
    38.  
    39.             float alpha = (fadeDirection == FadeDirection.Out) ? 1 : 0;
    40.             float fadeEndValue = (fadeDirection == FadeDirection.Out) ? 0 : 1;
    41.             if (fadeDirection == FadeDirection.Out)
    42.             {
    43.                 while (alpha >= fadeEndValue)
    44.                 {
    45.                     SetColorImage(ref alpha, fadeDirection);
    46.                     yield return null;
    47.                 }
    48.                 fadeOutUIGameobjects[i].SetActive(false);
    49.             }
    50.             else
    51.             {
    52.                 fadeOutUIGameobjects[i].SetActive(true);
    53.                 while (alpha <= fadeEndValue)
    54.                 {
    55.                     SetColorImage(ref alpha, fadeDirection);
    56.                     yield return null;
    57.                 }
    58.             }
    59.         }
    60.     }
    61.     #endregion
    62.  
    63.     #region HELPERS
    64.     public IEnumerator FadeAndLoadScene(FadeDirection fadeDirection, string sceneToLoad)
    65.     {
    66.         yield return Fade(fadeDirection);
    67.         SceneManager.LoadScene(sceneToLoad);
    68.     }
    69.  
    70.     private void SetColorImage(ref float alpha, FadeDirection fadeDirection)
    71.     {
    72.         for (int i = 0; i < fadeOutUIGameobjects.Length; i++)
    73.         {
    74.             if (fadeOutUIImage == null)
    75.             {
    76.                 fadeOutUIImage = fadeOutUIGameobjects[i].GetComponent<Image>();
    77.             }
    78.         }
    79.  
    80.         fadeOutUIImage.color = new Color(fadeOutUIImage.color.r, fadeOutUIImage.color.g, fadeOutUIImage.color.b, alpha);
    81.         alpha += Time.deltaTime * (1.0f / fadeSpeed) * ((fadeDirection == FadeDirection.Out) ? -1 : 1);
    82.     }
    83.     #endregion
    84. }
    85.  
    but if for example I have 5 Text's and 10 Image's and a Cube a Cylinder and a Canvas and I want to fade in out them all the same time ?

    So I made the variable fadeOutUIGameobjects to be array but the problem is that the variable fadeOutUIImage is Image type only and if in the array there is also a Text or Canvas or a Cube it wil throw error exception since it's looking for Image.
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    5,509
    If it's all UI stuff under a canvas, just use a canvas group. You can adjust the alpha on the canvas group and anything under it will fade...as a group.

    https://docs.unity3d.com/ScriptReference/CanvasGroup.html

    Otherwise, I would suggest a tweening engine where you can pass it something and tween over whatever value you need depending on what it is you pass in.
     
  3. astracat111

    astracat111

    Joined:
    Sep 21, 2016
    Posts:
    627
    The most performant way to fade objects would be to use a shader (material), in other words access a unique materials shaders float values, at least I think.

    You can more easily give UI game objects an animation.

    Attach an animator component to the game objects inspector, and take a tutorial on YouTube.
     
unityunity