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. Dismiss Notice

Bug ToolbarToggle click event and other events from ui elements

Discussion in 'UI Toolkit' started by Loden_Heathen, Jan 21, 2021.

  1. Loden_Heathen

    Loden_Heathen

    Joined:
    Sep 1, 2012
    Posts:
    456
    I have searched through various bitts of documention ... which I have to say is either very well hidden, incomplete or just out right missing.

    Anyway ... we have a toggle, we want to register on the change event of its value in C#
    This is for an custom inspector just for note not an editor window.

    Similarly we want to register on the change event of TextFields, etc. I assume the process will be the same.

    I noted that this document
    https://docs.unity3d.com/ScriptReference/UIElements.ToolbarToggle-ctor.html
    says there is a clickEvent ... there doesn't seem to be

    In this document
    https://docs.unity3d.com/2019.4/Documentation/Manual/UIE-Events.html
    We saw the myElement.RegisterCallback<MouseDownEvent>(MyCallback); and gave it a go... no luck MyCallback never invokes

    We did of course thanks to intellisense find RegisterCallback and RegisterValueChangedCallback but either we are using them wrong or they are never called.

    If someone can point us to how we would hook up a C# event handler to the change event of a toggle or toolbar toggle to be more specific that would be a great help

    I do think ToolbarToggle might actually be bugged we also noted that toolbar toogles dont switch on and off to click events like the regular Toggle does ... so are we just beating our head against a broken field or are we doing something wrong?
     
  2. JuliaP_Unity

    JuliaP_Unity

    Unity Technologies

    Joined:
    Mar 26, 2020
    Posts:
    666
    The UI Toolkit Debugger uses ToolbarToggles and registers for value changed callbacks (RegisterValueChangedCallback) to react to it being selected/unselected. Can you share more details as to what's not working for you?
     
  3. Loden_Heathen

    Loden_Heathen

    Joined:
    Sep 1, 2012
    Posts:
    456
    We simply want to respond to the change of a toggle to run some logic. Ideally that would be as simple as registering on a C# event such as valueChanged or at least a Unity style UnityEvent. We understood from what we have read so far that your callback approch should do the trick e.g. we should be able to register a funciton to be invoked when the event is raised.

    Also:
    I have just completed a set of tests and think I can confirm a bug with ToolbarToggle ... and saddly still cant get a callback to work.

    So what I did
    1) Make a new Editor Window to insure all is clean
    2) Via the UI Builder add a Toolbar
    3) To the Toolbar add a ToolbarToggle and a Toggle
    4) Save and open the window
    5) click the 2 toggles and observe

    In this test the ToolbarToggle does not toggle between on and off. I did confirm what On and Off should look liek by setting its value in the markup and then reopening.
    The regular toggle does toggle on and off e.g. the check appears and disapears.

    So that is defently not the expected behaviour I should hope

    Next
    1) Open the class file and add the following code
    Code (CSharp):
    1. Toggle toggle = root.Query<Toggle>("tglTest");
    2. toggle.RegisterValueChangedCallback(HandleCallback);
    and
    Code (CSharp):
    1.     private void HandleCallback(ChangeEvent<bool> evt)
    2.     {
    3.         Debug.Log("HandleCallback invoked with value " + evt.newValue);
    4.     }
    2) Save, open the window and observer the HandleCallback method is never invoked
    3) Tried the same on both toggles never can manage to get the change event to call the method I feed it.
     
  4. Loden_Heathen

    Loden_Heathen

    Joined:
    Sep 1, 2012
    Posts:
    456
    We have found what we think are a lot of bugs
    Of course its hard to know what is a bug vers not understanding what was intended.

    For example the ToolbarToggle ... prety sure thats a bug but maybe not
    Registering events ... are we doing it right and its a bug or no

    We know there are a lot of bugs with the system so just arn't sure where the error is in the chair, the code or the tool ... for example
    The UI Builder will occasionaly decide to just add nodes you had removed back in or give names to nodes from old nodes you removed.

    We also noted that causing an exception in the create step of an editor window can end up with Unity thinking there is a window when there is not. That window then can never be opened even after you ressolve the exception.

    An example of this ...
    Create a new Editor window, go into the code and add a VisualTreeAsset reference to the class remove the hard coded load asset by path and just use the reference.
    If you make the mistake of opening the window before you set the reference on the class then it will throw an exception of course but even after you fix the exception (set the reference on the script file) it still will not open the window. Even reverting the code to its orignal state will not let the window open ... we assume its something odd like the window having a handle but not actual visual.
     
  5. Loden_Heathen

    Loden_Heathen

    Joined:
    Sep 1, 2012
    Posts:
    456
    Any news on this
    We would love to migrate our inspector scripts to use the new system but unless we are missing something it doesn't appear to be ready for prime time.

    We have quite a few complex editor scripts and really are not fans of IMGUI, the UIElements approch seems much better but a few things just seem like they dont work quite yet.
     
  6. JuliaP_Unity

    JuliaP_Unity

    Unity Technologies

    Joined:
    Mar 26, 2020
    Posts:
    666
    Sorry for the delay but I think I see what's wrong here! You're using
    Query
    when you should be using simply
    Q
    - like this:

    Code (CSharp):
    1. Toggle toggle = root.Q<Toggle>("tglTest");
    2. toggle.RegisterValueChangedCallback(HandleCallback);
    Can you try that out and let us know?

    I used code like this and got my toggles to print log messages just fine:
     
    Last edited: Jan 28, 2021
  7. Loden_Heathen

    Loden_Heathen

    Joined:
    Sep 1, 2012
    Posts:
    456
    I'll check soon as I can ... for note the samples I have see for inspectors all use Query not Q ... I assume under the hood they are probably doing the same thing? or is there something different about each?
     
  8. JuliaP_Unity

    JuliaP_Unity

    Unity Technologies

    Joined:
    Mar 26, 2020
    Posts:
    666
    Calling
    Query
    on a VisualElement returns a
    UQueryBuilder
    with which you can find a particular VisualElement and register callbacks to them, so I'm not sure how the code you posted before was compiling as is. This allows for more complex queries, like when you want to iterate on a list of VisualElements to assign values, callbacks, etc.

    Calling simply
    Q
    returns the first matching VisualElement for the given query, and then you can work with said VisualElement directly.

    Hope it helps!
     
  9. Loden_Heathen

    Loden_Heathen

    Joined:
    Sep 1, 2012
    Posts:
    456
    I have re-tested and confirmed that no toolbar toggle doesn't work
    and Q vs Query doesn't seem to have any effecct.

    Actually if you note my test case above no code is required at all to see the UI Toolkit is broken on Unity 2019.4

    If you simply create a new editor window, add a toolbar and a tollbar toggle to it you will see that you cannot toggle it on and off.

    Do the same thing on 2020.2 and it works.

    The result is that Uinty's UI Toolkit is not functional on 2019.4


    Note to confirm its not a project related issue I created a new project and created a new test Editor Window with nothing but a toolbar and a toolbar toggle on it ... and the issue persists ... the toggle can not be turned on and off by clicking it
     
  10. Loden_Heathen

    Loden_Heathen

    Joined:
    Sep 1, 2012
    Posts:
    456

    1) Q vs Query ... that is some bad naming :)
    There are various bits of information out there that show a UIElements based example or 2 that use Query ... some use Q frankly the naming, documentaiton and examples are all very confusing and that is coming from a software engineer with nearly 20 years experance accustom to working in enterprise software from Microsoft ... world famious for its cryptic code structures glued together from multiple aquisitions ... so that needs work to say the least

    2) Query<T> returns a UQueryBuilder<T> whcih has an implicite operator converting the UQueryBuilder to the T you found if any ... thats why it worked ... so I would expect that yields much the same resutly as Q though perhaps not as efficently thanks I will use Q from here on out ... if UI Toolkit / Elements proves to be fit for use just yet ... whcih I think maybe its not

    3) Please see the post just before this ... I have testing on 2019.4 and 2020.2 ... put simply UI Elements / UI Toolkit is broken on 2019.4 though it shows as verified. Toogle events dont work, toolbar toggles dont switch on and off when clicked .. 2020.2 using the same approch seems to work

    The issue here is that it seems like its time to switch away from complex difficult to maintain IMGUI based editor windows into UI Elements / Toolkit ... but it would also seem like UI Toolkit isn't yet fit for purpose ... or maybe this is just a bad patch on 2019.4 not sure
     
  11. JuliaP_Unity

    JuliaP_Unity

    Unity Technologies

    Joined:
    Mar 26, 2020
    Posts:
    666
    Sorry if you mentioned before that you were on 2019, I had missed that, but now I've tried it on 2019 and see that there's a bug when the ToolbarToggle is declared on the UXML and indeed it's not working, but if you declare it by code it does. I'll be checking our backlog and seeing what can be done as this is quite important indeed.

    In any case, using UI Toolkit for making Editor windows/views is highly encouraged and working well for most cases. It has several advantages over IMGUI and internally at Unity lots of teams have moved over for UI Toolkit for making Editor tools. There's also a lot more maintenance on UI Toolkit and new features coming in the foreseeable future, so it would be a shame if you moved away from it now.
     
  12. Loden_Heathen

    Loden_Heathen

    Joined:
    Sep 1, 2012
    Posts:
    456
    We would prefer to use UI Toolkit as we have a lot of experance with WPF, Silverlight, etc. and find it familure and comforttable and are not fans of IMGUI in the first place :)

    That said we have a number of assets that have fairly complicated Editor scripts ... that would benifit form UI Toolkit if only for the savings in maintenance time ... but really needs it to be stable cross all the supported engine verisons ... at the moment since we support from 2018 LTS up we are holding on UI Toolkit. We assume with the release of 2020 LTS that 2019 LTS will become the new base line and are hoping its UI Toolkit implamentaiton is stable enough to use so we can move away from IMGUI.

    That is actually what started the effort to port a couple of our editor extensions over ... however between the event issues, UI Builder having some quarks, and issues with controls be they documentaiton/learning issues or bugs its so far not looking good.

    Here's hoping it gets polished up soon ... we would LOVE to move away from IMGUI
     
  13. Loden_Heathen

    Loden_Heathen

    Joined:
    Sep 1, 2012
    Posts:
    456
    Update for anyone reading this

    I just got a message from Unity support saying they know about this issue ... its tracked here

    https://issuetracker.unity3d.com/product/unity/issues/guid/1270242

    and that they will not backport the fix applied to 2020 onto 2019.

    This is a notable issue if Unity expects to see adoption of the UI Toolkit in Editor scripts ... here is why

    At the moment Asset Developers on building for Unity 2018 LTS ... we live on the oldest supported LTS
    Soon 2019 LTS will take over and we have been encuraged to start using UI Toolkit and that makes a lot of since ... however UI Toolkit is not stable and as a result not fit for purpose on 2019 LTS and apparently wont be ...

    So if your not going to backport the fix to 2019 then no asset developer should support UI Toolkit until 2020 LTS becomes the oldest supported version in or around 2022-2023?
     
  14. JuliaP_Unity

    JuliaP_Unity

    Unity Technologies

    Joined:
    Mar 26, 2020
    Posts:
    666
    Hello, just wanted to let you know that the fix is being ported to 2019.4 and should be published soon.
     
  15. Loden_Heathen

    Loden_Heathen

    Joined:
    Sep 1, 2012
    Posts:
    456
    Look forward to it, will be very glad to move away from IMGUI for our editors but we do use toolbar a lot and toolbar toggle a lot
     
  16. VirZOOM

    VirZOOM

    Joined:
    Mar 16, 2017
    Posts:
    4
    Thank you for this thread, it's the first place I could find after a long web search that actually describes how to register a change callback for toggles.

    I also have a lot of experience with engine and UI APIs, and find the documentation of UIToolkit to be frustrating. The package itself seems well designed.