Hello, hoping to find some assistance here with what might be a configuration issue.
When I implement a UI Toolkit Drop Down Select, it doesn’t pop up the list of options. I was butting my head up against a lot of possible solutions to this, but I’m rather certain these issues didn’t describe what I’m dealing with.
The basic misbehavior can be described as “the highlight color flickers and the dropdown list appears and disappears rapidly.”
I attached some event listeners for various events to the dropdown, and the FocusEvent showed that after clicking the dropdown, it was repeatedly fired instead of being fired once, which suggests something odd going on with the project in regards to the event system (or something of this sort.)
(The suggestion to do this came from this thread: DropdownField options popup missing as an idea of assuming that Unity is bugged right now and the field won’t open on its own.)
Since the field’s option list does appear to normally open on focus, the fact that the focus event is repeating as if the object is losing and regaining focus repeatedly does explain the option list not appearing (or blinking in and out and being unusable.)
In order to stop this behavior I have to click outside the dropdownfield’s label/dropdown button area, which among the issues I’m having is the most logical thing.
To confirm there’s some incorrect behavior going on I attached a blur event as well
...
pickGraph.RegisterCallback<FocusEvent>(onFocus);
pickGraph.RegisterCallback<BlurEvent>(onBlur);
}
public void onFocus(FocusEvent evt) {
Debug.Log("focused");
var elem = uiDoc.rootVisualElement.focusController.focusedElement;
if (elem == null)
{
return;
}
if (elem is DropdownField)
{
var ev = KeyDownEvent.GetPooled(default(char), KeyCode.KeypadEnter, EventModifiers.None);
elem.SendEvent(ev);
}
}
public void onBlur(BlurEvent evt) {
Debug.Log("blurred");
}
And the result confirms this (there are hundreds of these in the Editor log)
focused
UnityEngine.Debug:Log (object)
StatusUI:hushed:nFocus (UnityEngine.UIElements.FocusEvent) (at Assets/Scripts/StatusGraphs/StatusUI.cs:55)
UnityEngine.EventSystems.EventSystem:Update () (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/EventSystem.cs:501)
blurred
UnityEngine.Debug:Log (object)
StatusUI:hushed:nBlur (UnityEngine.UIElements.BlurEvent) (at Assets/Scripts/StatusGraphs/StatusUI.cs:70)
UnityEngine.UIElements.UIElementsRuntimeUtilityNative:UpdateRuntimePanels ()
focused
UnityEngine.Debug:Log (object)
StatusUI:hushed:nFocus (UnityEngine.UIElements.FocusEvent) (at Assets/Scripts/StatusGraphs/StatusUI.cs:55)
UnityEngine.EventSystems.EventSystem:Update () (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/EventSystem.cs:501)
blurred
UnityEngine.Debug:Log (object)
StatusUI:hushed:nBlur (UnityEngine.UIElements.BlurEvent) (at Assets/Scripts/StatusGraphs/StatusUI.cs:70)
UnityEngine.UIElements.UIElementsRuntimeUtilityNative:UpdateRuntimePanels ()
Any idea what might cause such behavior? There is an EventSystem in the Scene, which has an active Standalone Input Module. One of the properties of it is “Input Actions per Second”, which if I reduce to 1, causes the blinking to slow down a lot. If I disable it however, the select does not seem to receive any events, which I guess makes sense if indeed the Standalone Input Module is what the event system is using to catch and propagate input events.
Any help here would be appreciated, I was not expecting to need to learn the guts of the Unity input handling system just to get my dropdown to work.
EDIT: I created a new project and created the minimal working UI Toolkit UI with just a select, populated the select with some options like this
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
public class UIBehavior : MonoBehaviour
{
public UIDocument uidoc;
// Start is called before the first frame update
void Start()
{
DropdownField select = uidoc.rootVisualElement.Q<DropdownField>("SampleDropdown");
select.choices.Clear();
select.choices = new List<string>() { "Choice A", "Choice B" };
select.SetValueWithoutNotify("Choice A");
}
// Update is called once per frame
void Update()
{
}
}
And on run, I get the same bizarre behavior. The sample scene has just a camera and the UIDocument.
EDIT: with further testing, the issue is slightly different, I don’t get the repeated blinking but the select simply doesn’t open. Version is 2021.3.15f1 which is the recommended version right now.