Skip to content

Instantly share code, notes, and snippets.

@andrew-raphael-lukasik
Last active September 28, 2023 18:51
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save andrew-raphael-lukasik/69c7858e39e22f197ca51b318b218cc7 to your computer and use it in GitHub Desktop.
Save andrew-raphael-lukasik/69c7858e39e22f197ca51b318b218cc7 to your computer and use it in GitHub Desktop.
UI Toolkit Button class that can be disabled via style sheet or uxml inline property

Button class that can be disabled via style sheet or uxml inline property

how to disable a Button from UI Builder

// web* src: https://gist.github.com/andrew-raphael-lukasik/69c7858e39e22f197ca51b318b218cc7
using UnityEngine.UIElements;
[UnityEngine.Scripting.Preserve]
public class ButtonThatCanBeDisabled : Button
{
public bool enabled {
get => enabledSelf;
set => SetEnabled(value);
}
public new class UxmlFactory : UxmlFactory<ButtonThatCanBeDisabled,UxmlTraits> {}
public new class UxmlTraits : Button.UxmlTraits
{
UxmlBoolAttributeDescription enabledAttr = new UxmlBoolAttributeDescription{ name="enabled" , defaultValue=true };
public override void Init ( VisualElement ve , IUxmlAttributes attributes , CreationContext context )
{
base.Init( ve , attributes , context );
ButtonThatCanBeDisabled instance = (ButtonThatCanBeDisabled) ve;
instance.enabled = enabledAttr.GetValueFromBag( attributes , context );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment