Search Unity

Bug Problem with focus button Unity UI toolkit

Discussion in 'UI Toolkit' started by RaulPluto, May 28, 2023.

  1. RaulPluto

    RaulPluto

    Joined:
    Jan 21, 2023
    Posts:
    16
    Hi.
    I have this code but the focus is not working. Please help.

    Code (CSharp):
    1. private void SetStars()
    2.         {
    3.             AddToClassList("thirdPart");
    4.             _backGroundStarsContainer.AddToClassList("backGroundStarsMenuContainer");
    5.             _backGroundStars.AddToClassList("backGroundStarsMenu");
    6.             _lightStar.AddToClassList("lightStar");
    7.  
    8.             Add(_backGroundStarsContainer);
    9.             _backGroundStarsContainer.Add(_backGroundStars);
    10.             Add(_lightStar);
    11.  
    12.             for (int i = 0; i < _listObjects.Count(); i++)
    13.             {
    14.                 ObjectSo objectSo = _listObjects[i];              
    15.                 ItemMenu item = new ItemMenu(
    16.                     objectSo.nameObject.GetLocalizedString(),
    17.                     objectSo.descriptionObject.GetLocalizedString(),
    18.                     objectSo.guid
    19.                 );
    20.                 Button btnItem = (Button)item.Query(className: "itemTitle").First();
    21.                 item.tabIndex = i;
    22.                 btnItem.tabIndex = i;
    23.                 item.CurrentPosition = i;
    24.  
    25.                 item.style.left = Length.Percent(_itemsPositions[i].x);
    26.                 item.style.top = Length.Percent(_itemsPositions[i].y);
    27.  
    28.                 Add(item);
    29.                 _objectsUI.Add(item);
    30.                 btnItem.RegisterCallback<FocusInEvent>(evt => OnFocusEvent(evt, item));
    31.                 btnItem.clicked += () => OnElementClick(item);
    32.  
    33.                 if (i == _listObjects.Count() - 1)
    34.                 {                  
    35.                     btnItem.Focus();
    36.                     ForceFocusEvent(item);                  
    37.                 }
    38.  
    39.             }
    40.            
    41.         }
    42.  
    43.  
    Code (CSharp):
    1.  public class ItemMenu : VisualElement
    2.     {
    3.         // ATTR
    4.         public string ItemName;
    5.         public string ItemDescription;
    6.         public int CurrentPosition = 1;
    7.         public Guid ItemGuid;
    8.  
    9.         public ItemMenu(string itemName, string itemDescription, Guid itemGuid)
    10.         {
    11.             this.ItemName= itemName;
    12.             this.ItemDescription= itemDescription;
    13.             this.ItemGuid= itemGuid;
    14.             //Add USS style properties to the elements
    15.             AddToClassList("itemMenu");
    16.  
    17.             Button label = new Button();
    18.             label.text = itemName;
    19.             Add(label);
    20.             label.AddToClassList("itemTitle");
    21.  
    22.         }
    23.         // Constructor predeterminado
    24.         public ItemMenu() : this("Empty", "", Guid.Empty) {}
    25.  
    26.         public void OnElementClick() {}
    27.     }
     
  2. RaulPluto

    RaulPluto

    Joined:
    Jan 21, 2023
    Posts:
    16
    I have found the problem.
    You have to add the focus here:
    Code (CSharp):
    1. RegisterCallback<GeometryChangedEvent>(OnWindowReady);          
    2.         }
    3.  
    4.         private void OnWindowReady(GeometryChangedEvent evt)
    5.         {
    6.             _objectDescription = root.Q<Label>("ObjectLabelDescription");
    7.             _topFrontArrow = root.Q<Button>("topFrontArrow");
    8.             _bottomFrontArrow = root.Q<Button>("bottomFrontArrow");
    9.             _topFrontArrow.clicked += () => OnArrowClick(-1);
    10.             _bottomFrontArrow.clicked += () => OnArrowClick(1);
    11.  
    12.             _backGroundStarsContainer = new VisualElement();
    13.             _backGroundStars = new VisualElement();
    14.             _lightStar = new VisualElement();
    15.  
    16.             SetVector2Positions();
    17.             SetRotateStarsPositions();
    18.             SetStarLightPositions();
    19.             SetStars();
    20.             ItemMenu item = _objectsUI.Last();
    21.             if (item != null)
    22.             {
    23.                 Button btnItem = item.Q<Button>(className: "itemTitle");
    24.                 btnItem.Focus();
    25.             }
    26.         }
    I hope this helps someone.