Updating Button.colors doesn't propegate until another touch
I'm currently making some pseudo tabs in my UI and using Button.colors to darken UI Buttons when they are "selected". I'm able to change the Button colors, but they only update on the screen when I give additional input to the device, not OnClicked() like I would expect.
Has anyone run into anything like this? Canvas.ForceUpdateCanvases() does nothing. I've recreated the behavior in the following simplified code:
public class Controller : MonoBehaviour {
public Button b;
private bool pressed;
void Start () {
b.onClick.AddListener(onButtonClick);
pressed = false;
}
public void onButtonClick()
{
ColorBlock cb = b.colors;
if (pressed)
{
cb.normalColor = Color.white;
}
else
{
cb.normalColor = Color.black;
}
b.colors = cb;
pressed = !pressed;
}
}
Answer by Dman00Cman · Oct 26, 2017 at 02:12 AM
A work around here that I just found is that if I toggle the enabled flag on the buttons, it will display correctly. This seems like a framework bug but this is a stopgap for now.
Answer by yukunlinykl · Dec 27, 2021 at 07:18 PM
After you click the button, the button is "selected". After you click other areas, the button is "unselected" and returns to normal.
You can do this:
if (pressed) {
colors.normalColor = Color.white;
colors.selectedColor = Color.white;
} else {
colors.normalColor = Color.black;
colors.selectedColor = Color.black;
}
Your answer
Follow this Question
Related Questions
Drawing UI canvas image over GUI.button. 1 Answer
UnityEvents for UI updating, generic way of removing repeated calls in same frame. 0 Answers
Panel content size fitter not working 2 Answers
Limit maximum resolution of device / Force it to stretch 0 Answers
Is it possible to create a 3D Building modelling software with Unity? 1 Answer