- Home /
UI Toolkit disable button in uxml template
Been searching and found nothing, how do you start a button as disabled in the UXML?
I've tried:
<ui:Button text="Save" display-tooltip-when-elided="true" name="optionsSaveBut" class="button" enabled="false"/>
But that seems to do nothing.
Answer by andrew-lukasik · May 14 at 02:49 PM
<ButtonThatCanBeDisabled text="Save" name="optionsSaveBut" class="button" enabled="false"/>
// 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 );
}
}
}
Ok so this proves that its not built into the elements as a proper attribute yet.
Appreciate the help!
You're welcome. This is the only way of doing this I know of.
Your answer
Follow this Question
Related Questions
UIToolkit UIProgressBar methods changed 0 Answers
InputField caret too big in world space canvas 0 Answers
UI Toolkit: Is it possible to modify USS custom properties (variables) with C#? 0 Answers
UI Toolkit and Builder scaling issues when building for Android 1 Answer
In WebGL, is it possible to make UI Coordinate scale with CSS style? 1 Answer