Skip to content

Instantly share code, notes, and snippets.

@karljj1
Created August 5, 2019 10:22
Show Gist options
  • Save karljj1/94760e1d01d281c002f8cf08a9cbe34f to your computer and use it in GitHub Desktop.
Save karljj1/94760e1d01d281c002f8cf08a9cbe34f to your computer and use it in GitHub Desktop.
using UnityEngine.UIElements;
public class SelectableVisualElement : VisualElement
{
internal new class UxmlFactory : UxmlFactory<SelectableVisualElement> { }
static SelectableVisualElement CurrentSelected;
public virtual string HoverStyle { get => "selectable__hover"; }
public virtual string SelectedStyle { get => "selectable__selected"; }
public bool Selected { get; set; }
public SelectableVisualElement()
{
RegisterCallback<MouseEnterEvent>((evt) => EnableInClassList(HoverStyle, true));
RegisterCallback<MouseLeaveEvent>((evt) => EnableInClassList(HoverStyle, false));
RegisterCallback<MouseDownEvent>((evt) => Select());
}
void Select()
{
if (CurrentSelected != null)
{
CurrentSelected.EnableInClassList(SelectedStyle, false);
Selected = false;
CurrentSelected = null;
}
EnableInClassList(SelectedStyle, true);
CurrentSelected = this;
}
}
@karljj1
Copy link
Author

karljj1 commented Aug 5, 2019

A simple way to create a VisualElement that can be selected.

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