Darkening a Button when Not Clickable

Hey all. I’m totally new to using the UI Toolkit (and to any other UI builder) and I’m just trying to figure out how to make a button be darkened and unclickable unless a certain condition is met. I’ve been dredging through the manual, API, and experimenting in the editor and in scripts for the better part of two hours and I can’t figure it out. Any help?
Thanks in advance!

To make a button unclickable you should do:

// where button is a UnityEngine.UIElements.Button

button.SetEnabled(false);

You can also do this for any UI element that inherits from UnityEngine.UIElements.VisualElement.

For the styling when disabled, the default behaviour of buttons in ui toolkit is to darken it, I don’t know if this is exactly the effect you’re hoping to achieve. If not then you would need to use the :disabled style selector in a USS class that is applied to your button. Something like:

Button:disabled{
       /* your styles here */
}
2 Likes

@BigStuuu I’ll give that a shot a see how it goes. Thank you!

1 Like

@BigStuuu Just implemented your method and it worked perfectly. Thanks again!