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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question How to fade in/out ui image using a key ?

Discussion in 'Scripting' started by JhonBrucing1999, Jan 16, 2023.

  1. JhonBrucing1999

    JhonBrucing1999

    Joined:
    Jan 16, 2023
    Posts:
    4
    This is working for the FadeOut but when pressing the F key again it's fading in very fast. i set the fadeTime to 3 in the editor but the FadeIn is too fast.

    also, how can i interrupt the coroutine in the middle , when pressing f while it's fading in or out it will change the direction at once and will fade the other way ?

    Code (csharp):
    1.  
    2. using System;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6. using UnityEngine.UI;
    7.  
    8. public class Fader : MonoBehaviour
    9. {
    10.     public Image image;
    11.  
    12.     private bool isFading = false;
    13.  
    14.     private void Start()
    15.     {
    16.  
    17.     }
    18.  
    19.     private void Update()
    20.     {
    21.         if (Input.GetKeyDown(KeyCode.F))
    22.         {
    23.             isFading = !true;
    24.  
    25.             if (isFading)
    26.             {
    27.                 StartCoroutine(FadeOut(image, 3f));
    28.             }
    29.             else
    30.             {
    31.                 StartCoroutine(FadeIn(image, 3f));
    32.             }
    33.  
    34.  
    35.         }
    36.     }
    37.  
    38.     private YieldInstruction fadeInstruction = new YieldInstruction();
    39.     IEnumerator FadeOut(Image image, float fadeTime)
    40.     {
    41.         float elapsedTime = 0.0f;
    42.         Color c = image.color;
    43.         while (elapsedTime < fadeTime)
    44.         {
    45.             yield return fadeInstruction;
    46.             elapsedTime += Time.deltaTime;
    47.             c.a = 1.0f - Mathf.Clamp01(elapsedTime / fadeTime);
    48.             image.color = c;
    49.         }
    50.     }
    51.  
    52.     IEnumerator FadeIn(Image image, float fadeTime)
    53.     {
    54.         float elapsedTime = 0.0f;
    55.         Color c = image.color;
    56.         while (elapsedTime < fadeTime)
    57.         {
    58.             yield return fadeInstruction;
    59.             elapsedTime += Time.deltaTime;
    60.             c.a = Mathf.Clamp01(elapsedTime / fadeTime);
    61.             image.color = c;
    62.         }
    63.     }
    64. }
    65.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,947
    Oh my goodness, toss out those coroutines. :)

    Fading, simple and easy:

    https://forum.unity.com/threads/how...color-with-duration-time.969864/#post-6311295

    Totally reversible (or even stoppable) at any point.

    This is the general principle:

    Smoothing movement between any two particular values:

    https://forum.unity.com/threads/beginner-need-help-with-smoothdamp.988959/#post-6430100

    You have currentQuantity and desiredQuantity.
    - only set desiredQuantity
    - the code always moves currentQuantity towards desiredQuantity
    - read currentQuantity for the smoothed value

    Works for floats, Vectors, Colors, Quaternions, anything continuous or lerp-able.

    The code: https://gist.github.com/kurtdekker/fb3c33ec6911a1d9bfcb23e9f62adac4
     
    JhonBrucing1999 likes this.
  3. JhonBrucing1999

    JhonBrucing1999

    Joined:
    Jan 16, 2023
    Posts:
    4

    I tried this code :

    Code (csharp):
    1.  
    2. using System;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6. using UnityEngine.UI;
    7.  
    8. public class SmoothMovement : MonoBehaviour
    9. {
    10.     public Image image;
    11.  
    12.     float currentQuantity;
    13.     float desiredQuantity;
    14.     Color c;
    15.  
    16.     const float MovementPerSecond = 2.0f;
    17.  
    18.     void Start ()
    19.     {
    20.         // TODO: set up your initial values, if any
    21.         currentQuantity = 1;
    22.  
    23.         // match desired
    24.         desiredQuantity = currentQuantity;
    25.  
    26.         c = image.color;
    27.     }
    28.  
    29.     void AcceptUserInput()
    30.     {
    31.         if (Input.GetKeyDown( KeyCode.Alpha1))
    32.         {
    33.             desiredQuantity = 1;
    34.         }
    35.         if (Input.GetKeyDown( KeyCode.Alpha2))
    36.         {
    37.             desiredQuantity = 2;
    38.         }
    39.         if (Input.GetKeyDown( KeyCode.Alpha3))
    40.         {
    41.             desiredQuantity = 3;
    42.         }
    43.     }
    44.  
    45.     void ProcessMovement()
    46.     {
    47.         // Every frame without exception move the currentQuantity
    48.         // towards the desiredQuantity, by the movement rate:
    49.         currentQuantity = Mathf.MoveTowards(
    50.             currentQuantity,
    51.             desiredQuantity,
    52.             MovementPerSecond * Time.deltaTime);
    53.  
    54.         c.a = currentQuantity;
    55.         image.color = c;
    56.     }
    57.  
    58.     public UnityEngine.UI.Text TextOutput;
    59.     void DisplayResults()
    60.     {
    61.         TextOutput.text = "Press 1, 2 or 3 to select desiredQuantity.\n\n" +
    62.             "desiredQuantity = " + desiredQuantity + "\n" +
    63.             "currentQuantity = " + currentQuantity + "\n";
    64.     }
    65.  
    66.     void Update ()
    67.     {
    68.         AcceptUserInput();
    69.  
    70.         ProcessMovement();
    71.  
    72.         DisplayResults();
    73.     }
    74. }
    75.  
    but having some issues.

    in the editor the camera i changed it to Clear Flags to Skybox
    then i added ui image and change it's color to black and set the alpha to 0.

    but when i'm running the game it's changing the image alpha color value to 255 and then when i'm pressing either on any of the keys it's not fading and does nothing.

    The camera settings :



    The canvas settings :



    The image settings :

     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,947
    My reading of your code sets the quantity to 1, 2 or 3

    Alpha is only from 0.0f to 1.0f.

    It feels like you randomly combined a little from each of those links. Don't do that unless you know what you're doing. The first one directly applies to fading. Use that.
     
    JhonBrucing1999 likes this.