Peter
Peter

Reputation: 952

How to Change transparency of button image in Unity 5.0?

I would like to change the transparency of button image from the script (written in C#). What is the best way to do it?

Upvotes: 0

Views: 10324

Answers (3)

Olivia
Olivia

Reputation: 1581

You can modify color property directly

var color = button.targetGraphic.color;
color.a = 125; //higher than 0 otherwise it is invisible
button.targetGraphic.color = color;

or use CrossFadeAlpha

button.targetGraphic.CrossFadeAlpha(0, 1, false);

Upvotes: 2

Mario
Mario

Reputation: 179

Here the script version!

            using UnityEngine;
            using System.Collections;
            using UnityEngine.UI;

            public class SetTransparancy : MonoBehaviour {
                public Button myButton;
                // Use this for initialization
                void Start () {
                    myButton.image.color = new Color(255f,0f,0f,.5f);
                }

                // Update is called once per frame
                void Update () {

                }
            }

I tested it in Unity5

Upvotes: 3

Mario
Mario

Reputation: 179

Have you tried this, this could solve your problem

http://answers.unity3d.com/questions/46158/how-to-create-a-transparent-button.html

Sorry about that(you are right that was for 4 etc),

you can go to the button select the color of the image or the normal color and then where you set the color you can also set the alpha if you set that to say 125 then you have transparency on the button

Upvotes: 1

Related Questions