Search Unity

  1. Get all the Unite Berlin 2018 news on the blog.
    Dismiss Notice
  2. Unity 2018.2 has arrived! Read about it here.
    Dismiss Notice
  3. We're looking for your feedback on the platforms you use and how you use them. Let us know!
    Dismiss Notice
  4. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Learn more.
    Dismiss Notice
  6. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  7. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  8. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Change The Alpha Of A Button In C# Script?

Discussion in 'Unity UI & TextMesh Pro' started by JeZxLee, Aug 12, 2018 at 5:18 AM.

  1. JeZxLee

    JeZxLee

    Joined:
    Jul 24, 2016
    Posts:
    135
    Hi,

    I am trying to change the alpha of a button in C# script.
    Is the above possible, if yes then please provide an example...
    Thanks!

    Jesse
     
  2. JeZxLee

    JeZxLee

    Joined:
    Jul 24, 2016
    Posts:
    135
  3. JeZxLee

    JeZxLee

    Joined:
    Jul 24, 2016
    Posts:
    135
    Here is the script I have attached to the button(it compiles, but does nothing?):
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class ScreenFadeScript : MonoBehaviour {
    5.    // Use this for initialization
    6.    void Start () {
    7.        Globals.ScreenFadeAlpha = 255;
    8.        Globals.ScreenFadeStatus = Globals.ScreenFadingIn;
    9.    }
    10.    // Update is called once per frame
    11.    void Update () {
    12.        if (Globals.ScreenFadeStatus == Globals.ScreenFadingIn)
    13.        {
    14.            if (Globals.ScreenFadeAlpha > 0)
    15.            {
    16.                Globals.ScreenFadeAlpha-=1;
    17.                Color tmp = GetComponent<UnityEngine.UI.Image>().color;
    18.                tmp.a = Globals.ScreenFadeAlpha;
    19.                GetComponent<SpriteRenderer>().color = tmp;
    20.            }
    21.        }
    22.    }
    23. }
    24.  
     
  4. Hosnkobf

    Hosnkobf

    Joined:
    Aug 23, 2016
    Posts:
    344
    hi,

    please not that the Color struct is using float values, not bytes.
    For Alpha it means:
    - a value of 0 is invisible
    - a value of 1 is opaque
    - everything greater than 1 is clamped to one.