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.
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.
<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 );
}
}
}
You can use a c# script, just find a button and use SetEnabled(false);
public UIDocument yourUXML;
private void OnEnable()
{
// Find the button
var btn = yourUXML.rootVisualElement.Q<Button>("Btn_Name");
// Disable it
btn.SetEnabled(false);
}
// And you can enable it later
private void EnableBtn()
{
btn.SetEnabled(true);
}
It works on every VisualElement
type.