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 fade out color.alpha in the UI component? RawImage.color.a

Discussion in 'UGUI & TextMesh Pro' started by AlanMattano, Feb 18, 2016.

  1. AlanMattano

    AlanMattano

    Joined:
    Aug 22, 2013
    Posts:
    1,500
    I'm using Unity 5.3.2 and I'm trying to fade out the alpha color of my rawImage.
    I'm using UnityEngine.UI;

    I try
    Code (csharp):
    1.  actualValeu = 100f;
    2. this.GetComponent<RawImage>().color = new Color(147f, 244f, 196f, actualValeu);
    3.  


    and

    Code (csharp):
    1. myMatrixRawImage.color = new Color(147f, 0f, 196f, 100f);


    but do not work. it do not go to 100

    Any idea, is it a known bug?
    What I'm making wrong?


    Just in case full script

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4. using System.Collections;
    5.  
    6.  
    7.  
    8. public class FadeMatrix : MonoBehaviour {
    9.  
    10.  
    11.    
    12.     public float finalValeu = 28f;
    13.     public float fadeValue = 0.1f;
    14.  
    15.  
    16.     private RawImage MatrixRawImage;
    17.     private Color rawColor;
    18.     public float actualValeu = 255f;
    19.     private bool fadeOut = true;
    20.  
    21.     void Awake()
    22.     {
    23.         MatrixRawImage = this.GetComponent<RawImage>();
    24.         rawColor = MatrixRawImage.color;
    25.     }
    26.  
    27.     // Use this for initialization
    28.     void Start () {
    29.         fadeOut = true;
    30.         actualValeu = 255f;
    31.     }
    32.    
    33.     // Update is called once per frame
    34.     void Update () {
    35.        
    36.     }
    37.  
    38.     void OnGUI ()
    39.     {
    40.         if (fadeOut)
    41.         {
    42.             FadeOut();
    43.         }
    44.     }
    45.  
    46.  
    47.  
    48.     void FadeOut()
    49.     {
    50.         if (actualValeu >= finalValeu)
    51.         {
    52.             actualValeu = actualValeu - fadeValue;
    53.             //MatrixRawImage.color = new Color(147f, 244f, 196f, 100f);
    54.             this.GetComponent<RawImage>().color = new Color(147f, 244f, 196f, actualValeu);
    55.         } else
    56.         {
    57.             fadeOut = false;
    58.         }
    59.     }
    60. }
    61. [/I]
    62.  
    63.  


     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    gildanielfred likes this.
  3. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @LeftyRighty / @AlanMattano - And then there is also less mentioned Color32, that seems to be working (I never use it) and it takes byte values 0-255 for each color component, if you really want to set values between 0-255. I've no idea if it has negative/positive effects on performance.
     
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    looking at the documentation, material's color attribute appears to only take the "color" type, although color32 have implicit conversions. So you could create a color32 and apply it to the material's color attribute and unity would need to do a conversion step. Probably small fries on the performance impact but it's an extra step :p :)
     
  5. AlanMattano

    AlanMattano

    Joined:
    Aug 22, 2013
    Posts:
    1,500