Change color for disabled button
I want to be able to change the disabled colour of my button programmatically in c#. Can anyone please help, as all the answers I've seen online do not work for me. Could someone help incorporate it into my code? Thanks, in advance!
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement;
public class o2aPress : MonoBehaviour {
public Button yourButton;
public GameObject button;
void Start ()
{
Button btn = yourButton.GetComponent<Button>();
btn.onClick.AddListener(TaskOnClick);
}
void TaskOnClick()
{
PlayerPrefs.SetInt ("Option2aDone", 1);
}
void Update()
{
if (PlayerPrefs.GetInt ("Option2aDone") == 1) {
//would like to change the disabled colour here!
yourButton.interactable = false;
PlayerPrefs.SetInt ("o2b", 1);
}
if (PlayerPrefs.GetInt ("o2a") == 1) {
yourButton.interactable = false;
}
}
}
Comment
Best Answer
Answer by TonyLi · Jun 29, 2017 at 06:11 PM
Assign a new ColorBlock to yourButton.colors:
var newColorBlock = yourButton.colors;
newColorBlock.disabledColor = /*your color here*/;
yourButton.colors = newColorBlock;
thank you so much, but would format should i write the color in. normal letters like 'Red' or "0,1,1,0'?
It's up to you.
newColorBlock.disabledColor = Color.red; // Solid red.
or
newColorBlock.disabledColor = new Color(1,0,0,0.5); // 50% red.