Skip to content

Instantly share code, notes, and snippets.

@karljj1
Created February 1, 2024 15:50
Show Gist options
  • Save karljj1/5a20fb64ae98efa2cb8f6a8d92a8d1f9 to your computer and use it in GitHub Desktop.
Save karljj1/5a20fb64ae98efa2cb8f6a8d92a8d1f9 to your computer and use it in GitHub Desktop.
UI Toolkit version of the IMGUI DropDownButton
abstract class DropDownButton : BaseField<string>
{
TextElement m_TextElement;
VisualElement m_ArrowElement;
public DropDownButton(string label)
: base(label, null)
{
AddToClassList(EnumField.ussClassName);
labelElement.AddToClassList(EnumField.labelUssClassName);
visualInput.AddToClassList(EnumField.inputUssClassName);
m_TextElement = new TextElement();
m_TextElement.AddToClassList(EnumField.textUssClassName);
m_TextElement.pickingMode = PickingMode.Ignore;
visualInput.Add(m_TextElement);
m_ArrowElement = new VisualElement();
m_ArrowElement.AddToClassList(EnumField.arrowUssClassName);
m_ArrowElement.pickingMode = PickingMode.Ignore;
visualInput.Add(m_ArrowElement);
var click = new Clickable(OnClick);
visualInput.pickingMode = PickingMode.Position;
visualInput.AddManipulator(click);
}
// Show your menu
protected abstract void OnClick();
public override void SetValueWithoutNotify(string newValue)
{
base.SetValueWithoutNotify(newValue);
m_TextElement.text = newValue;
}
}
@karljj1
Copy link
Author

karljj1 commented May 8, 2025

visualInput is internal but can be replaced with a Query for the element with the classname inputUssClassName:

var visualInput = this.Q(className: BaseField<string>.inputUssClassName);

@karljj1
Copy link
Author

karljj1 commented May 8, 2025

We also have EditorToolbarDropdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment