Search Unity

GUILayout.Toolbar change handling

Discussion in 'UIElements' started by m3rt32, May 31, 2019.

  1. m3rt32

    m3rt32

    Joined:
    Mar 5, 2013
    Posts:
    60
    Hey there, I wanna use Toolbar, since there is no alternative for it in uielements yet, I used an IMGUIContainer to attach it into my root.
    But the problem is, to be able to make pagination based on that, I need to schedule an event since all setup happens only once at OnEnable.

    But it doesn't feel efficient, since I need to remove an existing VisualElement from root and adding another one(other page) in every x milliseconds.

    So is there any other way of doing it?
     
    Last edited: May 31, 2019
  2. m3rt32

    m3rt32

    Joined:
    Mar 5, 2013
    Posts:
    60
    Okay I solved it. Probably there will be people that wanna do the same thing and google it so I'll keep that thread here.

    Solution:
    You can register a MouseUpEvent callback to imguicontainer.This way it will detect your click and you don't have to check everytime. Example:

    Code (CSharp):
    1.         imguiContainer.RegisterCallback<MouseUpEvent>(e =>
    2.         {
    3.          
    4.            selected = selected == 1 ? 0 : 1; //selected = GUILayout.Toolbar(...
    5.             if (selected == 1)
    6.             {
    7.                 rootVisualElement.Remove(page1);
    8.                 rootVisualElement.Add(page0);
    9.             }
    10.             else
    11.             {
    12.                 rootVisualElement.Remove(page0);
    13.                 rootVisualElement.Add(page1);
    14.             }
    15.         });
     
    Last edited: May 31, 2019