Class UITKDropdown
UI Toolkit dropdown / context menu. Dynamically builds buttons and toggles and positions itself at the mouse cursor.
public class UITKDropdown : UITKControl
- Inheritance
-
ObjectComponentBehaviourMonoBehaviourUITKDropdown
- Inherited Members
-
MonoBehaviour.IsInvoking()MonoBehaviour.CancelInvoke()MonoBehaviour.StopCoroutine(Coroutine)MonoBehaviour.StopAllCoroutines()MonoBehaviour.destroyCancellationTokenMonoBehaviour.useGUILayoutMonoBehaviour.didStartMonoBehaviour.didAwakeMonoBehaviour.runInEditModeBehaviour.enabledBehaviour.isActiveAndEnabledComponent.GetComponent<T>()Component.TryGetComponent<T>(out T)Component.GetComponentInChildren<T>()Component.GetComponentsInChildren<T>()Component.GetComponentInParent<T>()Component.GetComponentsInParent<T>()Component.GetComponents<T>()Component.GetComponentIndex()Component.CompareTag(TagHandle)Component.transformComponent.transformHandleComponent.gameObjectComponent.tagObject.GetEntityId()Object.GetInstanceID()Object.GetHashCode()Object.InstantiateAsync<T>(T)Object.InstantiateAsync<T>(T, Transform)Object.InstantiateAsync<T>(T, Vector3, Quaternion)Object.InstantiateAsync<T>(T, Transform, Vector3, Quaternion)Object.Instantiate(Object, Vector3, Quaternion)Object.Instantiate(Object, Vector3, Quaternion, Transform)Object.Instantiate(Object)Object.Instantiate(Object, Scene)Object.Instantiate<T>(T, InstantiateParameters)Object.Instantiate<T>(T, Vector3, Quaternion, InstantiateParameters)Object.Instantiate(Object, Transform)Object.Instantiate<T>(T)Object.Instantiate<T>(T, Vector3, Quaternion)Object.Instantiate<T>(T, Vector3, Quaternion, Transform)Object.Instantiate<T>(T, Transform)Object.Destroy(Object)Object.DestroyImmediate(Object)Object.DontDestroyOnLoad(Object)Object.DestroyObject(Object)Object.FindObjectsOfType<T>()Object.FindObjectsByType<T>(FindObjectsSortMode)Object.FindObjectsByType<T>(FindObjectsInactive, FindObjectsSortMode)Object.FindObjectOfType<T>()Object.FindFirstObjectByType<T>()Object.FindAnyObjectByType<T>()Object.FindFirstObjectByType<T>(FindObjectsInactive)Object.FindAnyObjectByType<T>(FindObjectsInactive)Object.FindObjectsByType<T>()Object.FindObjectsByType<T>(FindObjectsInactive)Object.ToString()Object.nameObject.hideFlags
Remarks
Callers build the menu and then show it:
dropdown.AddButton("Kick", OnKick);
dropdown.AddToggle("Show offline", OnToggleOffline);
dropdown.Show();
Those AddButton calls cannot create elements straight away. The menu starts hidden,
so before the first Show() there is no visual tree to add them to at all, and after
a hide there is one that is about to be replaced — enabling the document re-clones the
UXML. Either way the entries went into a tree nobody sees and the menu opened empty.
So the entries are recorded as data here and built into the live tree from OnAfterShow() and after any tree rebuild. That also makes the ordering of the calls stop mattering: build-then-show and show-then-build both work.
Fields
Buttons
Dictionary mapping button names to their Button instances.
public Dictionary<string, Button> Buttons
Field Value
- Dictionary<string, Button>
Remarks
Rebuilt whenever the entries are built into a tree; a reference kept across a hide/show would point at an element that has already been discarded.
Toggles
Dictionary mapping toggle names to their Toggle instances.
public Dictionary<string, Toggle> Toggles
Field Value
- Dictionary<string, Toggle>
Properties
Layer
Draw order tier for this panel. See UITKPanelLayer.
protected override UITKPanelLayer Layer { get; }
Property Value
Methods
AddButton(string, UnityAction)
Adds a new button to the dropdown with the specified name and click callback.
public void AddButton(string buttonName, UnityAction onClick)
Parameters
buttonNamestringName of the button.
onClickUnityActionCallback for button click.
AddToggle(string, UnityAction<bool>, bool)
Adds a new toggle to the dropdown with the specified name and state change callback.
public void AddToggle(string toggleName, UnityAction<bool> onToggleStateChanged, bool value = false)
Parameters
toggleNamestringName of the toggle.
onToggleStateChangedUnityAction<bool>Callback for toggle state change.
valueboolInitial state of the toggle.
Hide(bool)
Hides the dropdown and removes all buttons and toggles.
public override void Hide(bool overrideIsAlwaysOpen)
Parameters
overrideIsAlwaysOpenboolWhen true, the call is a no-op.
Remarks
The override is on Hide(bool), not on Hide(). Hide() simply
forwards to this one, but quit-to-login calls Hide(false) directly — so an
override that only covered the parameterless form left a menu's entries, and their
captured callbacks, alive across a return to the login screen.
OnAfterShow()
Builds the recorded entries and moves the menu to the current mouse position.
protected override void OnAfterShow()
OnAfterStarting()
Rebuilds the recorded entries into a tree that has just been replaced.
protected override void OnAfterStarting()
OnDestroying()
Unregisters the outside-click handler.
public override void OnDestroying()
OnStarting()
Resolves cached elements and configures the dropdown for outside-click dismissal.
public override void OnStarting()
OnTick()
Closes the menu on a click anywhere outside it.
protected override void OnTick()
Remarks
Polled rather than handled as an event on the root, because the root no longer takes part in picking — see OnStarting(). This is also the safety net for a menu that opens away from the cursor and so never gets a pointer-leave to close on.
The frame the menu opened on is skipped: the click that opened it is still down when this first runs, and would be read as a click elsewhere.