Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Text field with popup text field

Discussion in 'UI Toolkit' started by maxxa05, Feb 18, 2020.

  1. maxxa05

    maxxa05

    Joined:
    Nov 17, 2012
    Posts:
    186
    Anybody knows if there's a simple way to have a text field with a search popup in UIElements? I'm looking for something like UnityEditorJunkie SearchableEnum, but for text fields in UI Elements. I checked at ToolbarPopupSearchField, but it's not the same behaviour I'm looking for.

    I may end up trying to build one, but it seems like something that could already exist somewhere, so I thought I'd ask here first.
     
  2. V0odo0

    V0odo0

    Joined:
    Jan 8, 2012
    Posts:
    327
    There is no such built-in control as I know. Here is my solution with button but you can add the text field in front of the button to allow user either input the text or select the value from list.
    Code (UXML):
    1. <engine:VisualElement class="unity-base-field">
    2.   <engine:Label text="Culture" class="unity-base-field__label"/>
    3.   <engine:Button name="LanguageCultureButton" text="Pick" class="unity-enum-field__input flexGrow1 margin0">
    4.     <engine:VisualElement class="unity-enum-field__arrow enumArrow"/>
    5.   </engine:Button>
    6. </engine:VisualElement>
    Code (USS):
    1. .margin0{
    2.     margin: 0;
    3. }
    4.  
    5. .flexGrow1{
    6.     flex-grow: 1;
    7. }
    8.  
    9. .enumArrow{
    10.     position: absolute;
    11.     right: 1px;
    12. }
    Make your own PopupWindowContent and Show it with provided Visual Element's worldBound rect. Inside OnGUI I've used IMGUI SearchField and custom TreeView controls. You can end up with UIElements SearchField and ListView instead.
     
    Threeyes and Xarbrough like this.
  3. unity_zIYWJyNr2AiCPA

    unity_zIYWJyNr2AiCPA

    Joined:
    Jan 5, 2020
    Posts:
    9
    An impl for this might look like:

    Code (CSharp):
    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using System.Text;
    5. using System.Threading.Tasks;
    6. using UnityEditor;
    7. using UnityEngine;
    8. using UnityEngine.UIElements;
    9. using XRTK.UIElements.Attributes;
    10. using XRTK.UIElements.Interfaces;
    11. using PopupWindow = UnityEditor.PopupWindow;
    12.  
    13. namespace XRTK.UIElements.Components
    14. {
    15.     public abstract class PopupElement : VisualElement, IPopup
    16.     {
    17.         /// <summary>
    18.         /// Initial Rect bounds.
    19.         /// </summary>
    20.         /// <remarks>(0, 0, 0, 0) for (x, y, width, height)</remarks>
    21.         /// <remarks>(250, 250) for (250, 250, 200 (default), 200 (default))</remarks>
    22.         public Rect initialRect { get; protected set; }
    23.  
    24.         public new class UxmlTraits : VisualElement.UxmlTraits
    25.         {
    26.             public UxmlRectAttributeDescription m_initialRect = new UxmlRectAttributeDescription();
    27.  
    28.             /// <summary>
    29.             /// Support adding any kind of element for an inline popup option.
    30.             /// </summary>
    31.             public override IEnumerable<UxmlChildElementDescription> uxmlChildElementsDescription
    32.             {
    33.                 get { yield return new UxmlChildElementDescription(typeof(VisualElement)); }
    34.             }
    35.  
    36.             public override void Init(VisualElement ve, IUxmlAttributes bag, CreationContext cc)
    37.             {
    38.                 base.Init(ve, bag, cc);
    39.  
    40.                 var popup = (PopupElement) ve;
    41.                 popup.initialRect = m_initialRect.GetValueFromBag(bag, cc);
    42.             }
    43.         }
    44.  
    45.         public virtual void OnOpen(EditorWindow window)
    46.         {
    47.             window.rootVisualElement.Add(this);
    48.         }
    49.  
    50.         public virtual void OnClose() { }
    51.  
    52.         public virtual void Show(Rect rect, Vector2 windowSize)
    53.         {
    54.             PopupWindow.Show(rect, new PopupWindowWrapper(this, windowSize));
    55.         }
    56.     }
    57.  
    58.     internal sealed class PopupWindowWrapper : PopupWindowContent
    59.     {
    60.         private PopupElement container;
    61.         private Vector2 windowSize;
    62.         public override void OnGUI(Rect rect) { }
    63.         internal PopupWindowWrapper(PopupElement ve, Vector2 ws)
    64.         { container = ve; windowSize = ws; }
    65.         public override void OnOpen()
    66.         { container.OnOpen(editorWindow); }
    67.         public override void OnClose()
    68.         { container.OnClose(); }
    69.         public override Vector2 GetWindowSize() => windowSize;
    70.     }
    71. }