Search Unity

Official Important Message Related to the TextField Refactor for 2022.1+.

Discussion in 'UI Toolkit' started by HugoBD-Unity, Nov 19, 2021.

  1. HugoBD-Unity

    HugoBD-Unity

    Unity Technologies

    Joined:
    May 14, 2018
    Posts:
    499
    An important refactor has landed in 2022.1.0a15. It provides a TextField that fully relies on TextCore for both the Editor and Runtime, removing UI Toolkit’s dependency on Native Text, the legacy text system, and getting us a step closer to feature parity with TextMeshPro. However, some important changes had to be done to the inside components of TextFields introducing behavior changes that might have a significant impact on existing code.

    New Features:
    • TextFields can now display a vertical scrollbar. Whereas before achieving the same result would have required doing some delicate manipulations of the inside of a TextField. It can now easily be achieved by updating the scrollbar visibility.
      • inputField.SetVerticalScrollerVisibility(ScrollerVisibility.Auto);
        showScrollbars.gif

    • All the logic related to rendering, selecting and editing text is now all gathered in TextElement. This refactor gave the capabilities to every TextElements of being selectable. Whereas before achieving the same result would have required the usage of a custom TextField disguise as a label. It can now easily be achieved by setting isSelectable.
      • textElement.selection.isSelectable = true;
        selectable.gif

    • TextCore text assets (FontAssets, SpriteAssets, PanelTextSettings, ColorGradient and TextStyleSheet ) are now supported inside TextFields. It means TextFields now leverage the benefits of SDF rendering, getting UI Toolkit a step closer to feature parity with TextMesh Pro.
    • Based on your feedback, we have improved the selection API by providing more control to all TextElements and TextFields:
      • cursorColor
      • selectionColor
      • cursorPosition
      • cursorIndex
      • selectIndex
      • doubleClickSelectsWord
      • tripleClickSelectsLine
      • HasSelection
      • SelectAll
      • SelectNone
      • SelectRange
    What changed:

    The TextField internal hierarchy has changed. While ideally, this should be an implementation detail, some new physical VisualElements are now existing under the Field. Now all logic related to rendering, selecting, and editing has been centralized inside TextElement, which is a new child of TextFields. This improves maintainability, reduces duplication, and provides more capabilities to all TextElements such as selecting. Moreover, to have a more intuitive API, both TextFields and TextElements now implement the same selection API. Therefore, the inner hierarchy of TextFields went from this:




    To this for singleline TextField


    To this for multiline TextField


    To this, if the ScrollView is set to visible:


    Behavior change:
    • Due to the inner hierarchy of TextFields changing, broad queries that relied on a specific hierarchy order or select internal components might be broken.

      Examples of such queries that may be affected:
      • Accessing an inner component of the TextField through its hierarchy placement:
        • var innerComponent = field.hierarchy[index];
      • Since TextFields now have a ScrollView and a TextElement as children, queries that fetch the first ScrollView or the first TextElement encountered without specifying a classname or name might be affected:
        • var scrollView = root.Q<ScrollView>();
        • var textElement = root.Q<TextElement>();
    • Since the leaf element of the TextFields has changed from TextInputBase to TextElement, some RegisterCallback on TextInputBase that relied on being invoked before ExecuteDefaultActionAtTarget() might be invalid. In other words, it affects RegisterCallback on inner components of the TextFields that intercepted events before they arrived at target. For more information, please refer to the documentation for the dispatching of events and event handling.
      • Example of a RegisterCallback that intercepted the KeydownEvent before it gets to ExecuteDefaultActionAtTarget():
        • field.Q("unity-text-input").RegisterCallback<KeydownEvent>(OnKeyDownEvent);
      • To achieve the same result as before, you will need to register on the TrickleDown phase of the Event propagation loop like so:
        • field.RegisterCallback<KeydownEvent>(OnKeyDownEvent, TrickleDown.TrickleDown);

    We will continue to improve the TextField throughout the 2022.2 release cycle with the end goal of getting to feature parity with TextMeshPro.
     
    Last edited: Jun 23, 2022
  2. jGate99

    jGate99

    Joined:
    Oct 22, 2013
    Posts:
    1,945
    Thank you for bringing UIToolkit one step closer,
    TMPro Input Field have limitations on mobile, so please along with feature parity also take care of solving those limitations so we can have better player chat in our mobile game due to improved input field that provide text selection, mobile native auto correct, and "ideally" a visual rich text editor
     
    HugoBD-Unity likes this.
  3. HugoBD-Unity

    HugoBD-Unity

    Unity Technologies

    Joined:
    May 14, 2018
    Posts:
    499
    Thanks, @jGate99 for taking the time to provide feedback! Improving the soft keyboard along with rich text are our next areas of focus.
     
    Nexer8, Timboc and jGate99 like this.
  4. Arkenhammer

    Arkenhammer

    Joined:
    Nov 10, 2019
    Posts:
    51
    Is there any chance this will make it back into 2021LTS? If there's one thing I need fixed it is the TextField and I was really hoping there'd be an answer 2021.2.
     
  5. HugoBD-Unity

    HugoBD-Unity

    Unity Technologies

    Joined:
    May 14, 2018
    Posts:
    499
    Hi @Arkenhammer !

    Unfortunately, this will not be backported, mostly due to the size of this refactor and the risk of introducing new regressions.

    Could you be more specific on what you hope would be fixed in the TextField, while we can't backport the whole refactor, we might be able to backport some of the fixes.
     
  6. Arkenhammer

    Arkenhammer

    Joined:
    Nov 10, 2019
    Posts:
    51
    I have a bunch of needs from it but the big one is to be able to edit field focus in code.
    I need a text field which only gets focus when the player clicks on it and always loses focus when the player confirms they are done editing.
    The current text field grabs focus whenever a character is typed. Unfortunately that means it grabs focus when, for instance, the player is navigating the world with the WASD keys and the field gets filled with gibberish. The way I attempt to handle this is to disable the text field when its not in "edit mode" and then enable & focus it on a click. However the focus doesn't take and currently it takes two clicks to actually enter text--the first when enables the text field and the second which focuses it.

    While I am adding wishes--I layout my screen using a larger coordinate system than the screen and set the "Panel Settings" to scale it to match the screen. However the text field uses a single point wide rectangle for the vertical bar and, when scaling, that regularly aliases down to zero pixels and vanishes.

    I was hoping that the TextField was going to be stable in 2021LTS, but now it looks like its 2022 which is too far out for me. Currently I am contemplating writing my own or else grabbing the code out of the package and forking it so I can make something that works for me. Is going to be any way to positively control TextField focus in 2021 or am I on my own for this?
     
    HugoBD-Unity likes this.
  7. HugoBD-Unity

    HugoBD-Unity

    Unity Technologies

    Joined:
    May 14, 2018
    Posts:
    499
    Hi @Arkenhammer !

    This should be supported in 2021.2. You could do it like so:

    textField.Focus();
    textField.Blur();


    This issue hasn't been fixed by the refactore. In fact, the issue you are observing is due to the fact that the InputSystem sends navigation events when "WASD" is typed. There are multiple ways of fixing this. If you have the UGUI EventSystem, simply disabling "Send Navigation Events" will do the trick. However, NavigationEvents will no longer be sent. An other alternative is to add a RegisterCallback to your fields and stop the NavigationEvents like so:
    textField.RegisterCallback<NavigationMoveEvent>((evt) => evt.StopPropagation(), TrickleDown.TrickleDown);


    Are you referring to the carret width here ? If so, I beleive the issue is still present in the latest version. Could you log a bug though the unity bug reporter (Help/Report a bug...) ? This will help us with visibility and to make sure it is backported.
     
    mariandev likes this.
  8. leutnant_kane

    leutnant_kane

    Joined:
    Apr 29, 2015
    Posts:
    40
    While I love that the text development is continuing, I can't say I'm glad about the inclusion of the scrollbar/scrollview in the textfield.

    This very thing causes me lots of issues with the original tmpro, as the text is then also responsible for scrolling and makes a bunch of assumptions about how and when that should work. It makes every use case but the standard one more difficult and also complicates nesting tmpro_inputfields in scrollviews. Want text with horizontal scroll only? Want scrolling that syncs with input? Want selection but no scrolling? Want a different start point for the scroll bar or a different scroll direction? Want to manipulate the scrollpoint in some way? Want to disable scrolling in certain situations? All these things and more become more complicated and you have to learn several ways to solve them. Want to configure a scrollbar for some text element? Your normal scrollbar knowledge is no good, better learn how to access the scrollbar through the text element, investigate the source code to find out how tmpro wants the scrollbar to be used and when it accesses/changes its properties. Also be prepared to fight or fork tmpro elements if you have anything planned that doesn't align with its vision of scrolling.

    => Scrolling content and rendering text are two different concerns, so I'm not convinced it is wise to have the text rendering element have strong opinions/knowledge/dependencies about how it is scrolled. I hope this separation will be cleaner here than it is in the original tmpro.

    Another point, since font assets were mentioned. I would be amazing if tmpro could also give us a cleaner cut in terms of generated and static assets. Having assets be half generated and half static (I'm looking at dynamic font atlas features in particular) frequently causes small, but entirely avoidable mistakes (mostly to do with versioning).
     
  9. HugoBD-Unity

    HugoBD-Unity

    Unity Technologies

    Joined:
    May 14, 2018
    Posts:
    499
    Hi @leutnant_kane !

    Thanks a lot for sharing your thoughts. I'm not too familiar with how texfield + scrollview works in TMP and so I can't comment on its usability, but I can talk about UIToolkit's TextField.

    First, here's an update on the state of things. We are currently working on only adding the inner ScrollView when users change the Scrollbar visibility. In other words, the default TextField (multiline and singleline) will use a custom scrolling mechanism to handle the offset when the text overflows. The main purpose of this change is to improve the performance of instantiation, as the ScrollView is a heavy control. It should also allow users who want to embed a TextField into their own ScrollView and configure it as they see fit to do so. That being said, I believe there are better alternatives to achieve the same results.

    While it's true the currently exposed properties don't offer the flexibility you are looking for, APIs will be added to the TextField to allow it to acheive most of what you mentioned above. In fact, it should be fairly easy to do so as the underlying ScrollView used by the TextField is the same one as the one provided by the framework and everything you mentioned above is already supported by the native ScrollView.

    I can't recommend you to do this, but as a temporary workaround, you could query the inner ScrollView (like so: innerScrollView = textField.Q<ScrollView>()) and configure it as you see fit.
     
  10. leutnant_kane

    leutnant_kane

    Joined:
    Apr 29, 2015
    Posts:
    40
    Hi @hugobd ,
    Thank you for your quick response :)

    A quick and dirty overview of the way tmpro does it:
    The inputfield itself does the scrolling/hides its scrolling parts from the outside. You can set a vertical scrollbar and do some configuration, but the component itself has a strong/hardcoded opinion about how that scrollbar is used/how it wants to be scrolled. So if your use case doesn't exactly align with that, be prepared to dive into that tmpro source code (to either modify it or time your edits right after tmpro does them, which is sketchy at best).

    The scrollview itself being queryable does help I think. Still I am sceptical about a couple of points here:

    - Yes the current (and the original tmpro) textfield properties do not allow for the flexibility I'm looking for, but as you've said the native scrollview supports all that. I'm not advocating putting all scrollview properties into the textfield, I'm advocating putting none of them in there (not even the scrollview itself), because I'm not convinced the textfield should care about how it is scrolled at all.

    - Why put this at the textfield level? Why would a textfield have the ability to be scrolled, but a textlabel does not? Why wire the ability to get scrolled into one kind of text object. Why wire it into any specific object at all? Why should a textfield be scrolled differently than a textlabel or any other object?

    - As far as I'm concerned a Textlabel just renders text in a container (and allows read interactions: select text, copy, etc.). A textfield extends this to allow write interactions (and placeholders). A Masking component limits rendering of a container to its region and a Scrolling component decides where on the container an object (the masking region in this case) resides. I don't see much reason for hard coding/intimately wiring all of these parts together (except maybe performance considerations, but even then I'm not convinced the text component should have opinions/set things like scroll direction etc.).

    - In your post you point out that you believe there are better ways to achieve modularity/flexibility than embedding a textfield into a scrollview. I am curious what you had in mind here? Writing custom text components to change things like scroll direction does not seem very modular/flexible to me.

    Thanks for reading through my questions/concerns :)
     
    Bedtime and Nexer8 like this.
  11. HugoBD-Unity

    HugoBD-Unity

    Unity Technologies

    Joined:
    May 14, 2018
    Posts:
    499
    Adding the scrolling capabilities requires advanced programming skills as there are many edge cases to consider. Forcing all users to implement their own scrolling doesn't seem like a reasonable option to me. I would expect TextFields to scroll automatically to follow the caret position.

    To implement all the scrolling capabilities, the ScrollView needs to have intimate knowledge of the TextField (each line-height, each line-width, caret position, multiline...).

    On the other hand, you are right, while we offer default scrolling capabilities for TextFields, we should also allow users who want to embed their TextField into their own ScrollView to be able to do so. We'll add a way to disable scrolling in the TextField.
     
    Nexer8 likes this.
  12. jGate99

    jGate99

    Joined:
    Oct 22, 2013
    Posts:
    1,945
    Hi @hugobd
    We just lost Advanced TextInput Field 2 for uGUI, so my question is, is UI Toolkit based Text Input works on mobile with caret selection and features such as auto suggest / auto fill?
    If not, any plans for that?
     
  13. HugoBD-Unity

    HugoBD-Unity

    Unity Technologies

    Joined:
    May 14, 2018
    Posts:
    499
    Hi @jGate99, we plan to improve our mobile soft keyboard for 2022.2. We aim at feature parity with the TextMesh Pro mobile InputField.
     
    Nexer8 and jGate99 like this.
  14. jGate99

    jGate99

    Joined:
    Oct 22, 2013
    Posts:
    1,945
    I dont think feature parity alone will solve it, we atleast need 2 things in mobile input field
    1- caret selection or ability to tap and any where in text and change it rather than deleting whole text
    2- auto suggest/remember feature

    So please please please, consider this
     
    Nexer8 likes this.
  15. HugoBD-Unity

    HugoBD-Unity

    Unity Technologies

    Joined:
    May 14, 2018
    Posts:
    499
    Hi @jGate99! We took note of your request. Thanks a lot for your feedback and don't hesitate if you see other missing features. We see the importance of the features you highlighted above, but I can't say when we'll tackle those yet!
     
    jGate99 likes this.
  16. seyfe

    seyfe

    Joined:
    May 10, 2019
    Posts:
    74
    If I want to focus to the TextField in code, I’d expect that I could call `textField.Focus()`. It seems to get the focus (the border color of the input field changes, in the editor a cursor appears) but when I build this to my mobile device no keyboard is shown!

    This is a real problem and a regression from the old TextField which worked as expected and which we are wrapping in a custom component and call focus from there.

    To add a question:
    1. Is this a bug or the new intended behavior?
    2. Is there a way to manually force showing and/or hiding the soft keyboard on Android/iOS?
     
    Last edited: Mar 17, 2022
    Nexer8 and jGate99 like this.
  17. FreddyC-Unity

    FreddyC-Unity

    Unity Technologies

    Joined:
    Jun 9, 2021
    Posts:
    36
    Hi @seyfe, thank you for bringing this issue up with us! After taking a quick peek at this, I can confirm it is a regression - we will look into fixing this as soon as possible. To answer your questions:
    1. It's a bug
    2. Unfortunately, there is no way to manually force open the soft keyboard on Android/iOS at the moment
     
  18. seyfe

    seyfe

    Joined:
    May 10, 2019
    Posts:
    74
    Thanks, @freddycheungh, for the answer! What about closing the soft keyboard? ;)
     
  19. FreddyC-Unity

    FreddyC-Unity

    Unity Technologies

    Joined:
    Jun 9, 2021
    Posts:
    36
    @seyfe, we are working on a series of soft keyboard improvements for the text field and this too will be included with it :).
     
    seyfe and jGate99 like this.
  20. Kichang-Kim

    Kichang-Kim

    Joined:
    Oct 19, 2010
    Posts:
    1,012
    @hugobd Can I scroll to last position by using script? I'm trying to implement console-log like UI which shows latest message automatically, but I can't find the way.
     
  21. ChGuidi

    ChGuidi

    Joined:
    Dec 28, 2021
    Posts:
    111
    We are noticing some issues since we switched from 2021.2.19 to 2022.1.0:

    For example, I had to manually add:

    Code (CSharp):
    1. TextInput .unity-scroll-view--inner-input-field-component {
    2.     flex-grow: 1;
    3. }
    To have the TextInput fill the TextField. I also noticed that the TextElement inside the TextField doesn't fill the width of the TextField.

    What is also really weird, is that we're using the Unity default runtime theme (and the same version of Unity), but on my machine, this seems to contain the flex-grow: 1; that I mentioned above, while for my colleagues, this is missing. How is that possible?
     
  22. HugoBD-Unity

    HugoBD-Unity

    Unity Technologies

    Joined:
    May 14, 2018
    Posts:
    499
    Would setting the cursor index to the end be enough for your use case? The innerScrollView is set up to follow the cursor automatically.
     
  23. HugoBD-Unity

    HugoBD-Unity

    Unity Technologies

    Joined:
    May 14, 2018
    Posts:
    499
    Hi @ChGuidi ! I have heard issues with the "Unity default runtime theme" where users had to reImport the asset to have the latest version apply. Could you try that to see if it fixes your issue?
     
  24. ChGuidi

    ChGuidi

    Joined:
    Dec 28, 2021
    Posts:
    111
    Hi, thanks for the reply! I asked my colleague that had a different version than me to do this Reimport and it was fixed. However I would like to commit this to our repository such that it is fixed for everyone but I don't see any code changes after the Reimport. What files does this reimport affect? How can I solve this generally for the repository?
     
    Last edited: Jun 8, 2022
  25. HugoBD-Unity

    HugoBD-Unity

    Unity Technologies

    Joined:
    May 14, 2018
    Posts:
    499
    Hi @ChGuidi ! You may need to enter PlayMode or close your Unity Editor to make sure the new asset is serialized.
     
  26. ChGuidi

    ChGuidi

    Joined:
    Dec 28, 2021
    Posts:
    111
    That doesn't really answer my question. Now, every colleague working on our Unity application has to manually Reimport this asset, my question is whether it's possible to do this Reimport automatically?
     
  27. HugoBD-Unity

    HugoBD-Unity

    Unity Technologies

    Joined:
    May 14, 2018
    Posts:
    499
    Hi @ChGuidi ! I asked internally and it seems to be an issue that we already fixed. Which version are you on ?

    Sorry if I wasn't clear, once the new "Unity default runtime theme" has been reimported and re-serialized (could be triggered by closing the editor), you should be able to commit the new asset to your version control system to make sure your colleges have the latest version.
     
  28. ChGuidi

    ChGuidi

    Joined:
    Dec 28, 2021
    Posts:
    111
    Hi @hugobd , thanks for the reply. We are on version 2022.1.0 at the moment.

    I was indeed checking the asset but didn't see any code changes, that is the strange part :) but if it's fixed, I'll perhaps wait and see whether it occurs again.
     
  29. manuelgoellnitz

    manuelgoellnitz

    Joined:
    Feb 15, 2017
    Posts:
    397
    I tried using the new TextField today and ran into some issues:

    The scrollbar only seems to work from the inputfield to the scrollbar, not the other way round.
    What I mean is: When I add or remove text, the scrollbar gets updated. But when I click on the buttons of the scrollbar or drag the scrollbars-dragger, the textfield is not updated.
     
  30. HugoBD-Unity

    HugoBD-Unity

    Unity Technologies

    Joined:
    May 14, 2018
    Posts:
    499
    Hi @manuelgoellnitz ! Thanks a lot for bringing this up, it's indeed a regression. We have a fix and it's in the process of being backported up to 2022.1.

    Really sorry for the inconvenience!
     
  31. Thimo_

    Thimo_

    Joined:
    Aug 26, 2019
    Posts:
    59
    I'm currently using the Textfields and where the vertical scrollbar does its thing. What about a horizontal scrollbar or an option to detect the text is reaching the width of the textfield? Right now the user can keep adding text quite infinitely horizontally. I would like to build some logic to automatically insert a new line when the end of the TextField width is reached. Is something like this already possible or is this something that can be added?
     
  32. HugoBD-Unity

    HugoBD-Unity

    Unity Technologies

    Joined:
    May 14, 2018
    Posts:
    499
    Hi @Thimo_

    Could you elaborate on the use case for having a visible horizontal scrollbar in the textfield?

    As for enabling word wrapping, I think you are looking for the WhiteSpace.Normal property.
     
    Thimo_ likes this.
  33. Thimo_

    Thimo_

    Joined:
    Aug 26, 2019
    Posts:
    59
    Hi @hugobd,

    Whitespace.normal was exactly what I was looking for! Thank you for mentioning it. The reason for a horizontal scrollbar would be for UI toolkit at runtime. At this moment I am making a form where users input their name, email and some additional information. When I don't use Whitespace.normal a textfield that contains a lot of text on the same line doesn't show that to the user. Also the user can't easily scroll back to the beginning to edit something in the textfield. For my current use case the textfield user uses a custom on screen keyboard to input their data. This means they can't use a mouse and then a scrollbar would improve the UX for touchscreen users a lot.

    For now I can use Whitespace.Normal, but having the option later for enabling a dynamic horizontal scrollbar for runtime would be highly appreciated.
     
    HugoBD-Unity likes this.
  34. manuelgoellnitz

    manuelgoellnitz

    Joined:
    Feb 15, 2017
    Posts:
    397
    I ran into more issue which happen when I use the TextFields ScrollView or enbed the TextField on a screen with its own ScrollView:

    When the ScrollView is all the way up you can select text without problems. But when you scroll down a bit and the try to mark text, the ScrollView scrolls all the way up to the top.
    And marking is all jerky from here (sometimes it marks text without me keeping the mouse pressed, or marks the whole text).

    This all happens in my project and also in clean projects wiith only standard style and no code.

    I use Unity 2022.1.7
     
  35. HugoBD-Unity

    HugoBD-Unity

    Unity Technologies

    Joined:
    May 14, 2018
    Posts:
    499
    Hi @manuelgoellnitz!

    The default behavior of the TextField is the same one as IMGUI which is that it selects all on mouse up and it selects all on focus (could gain focus through tab for instance). This is indeed a bug, we shouldn't trigger a "selectAll" after a mouse up on the scrollbar. In the meantime, disabling selectAll on mouse up should give a more reliable result when using the scrollview.

    Code (CSharp):
    1. inputField.selectAllOnFocus = false;
    2. inputField.selectAllOnMouseUp = false;
     
  36. manuelgoellnitz

    manuelgoellnitz

    Joined:
    Feb 15, 2017
    Posts:
    397
    I tried this but unforunatelly it did not fix the bug.
    It seem that when you hold the mousebutton and move it to the right (to mark the text), the scrollView gets somehow the order to scroll to the top.

    I have some more information:
    When I downgrade to 2022.1.6 the bug is gone. So it seams that some changes in 2022.1.7 broke it.
     
    Last edited: Jul 7, 2022
  37. HugoBD-Unity

    HugoBD-Unity

    Unity Technologies

    Joined:
    May 14, 2018
    Posts:
    499
    Thanks a lot for bringing this up @manuelgoellnitz and sorry for the inconvenience, we'll look into it shortly!

    To make sure this is tracked and properly backported, could you submit a bug report through the Unity Bug Reporter (It's located in the Help/Report a bug... menu item)
     
  38. manuelgoellnitz

    manuelgoellnitz

    Joined:
    Feb 15, 2017
    Posts:
    397
    Done
    IN-9636
     
    Last edited: Jul 8, 2022
    HugoBD-Unity and Thimo_ like this.
  39. Thimo_

    Thimo_

    Joined:
    Aug 26, 2019
    Posts:
    59
    @hugobd ,

    What did you think of my use case for a horizontal scroller for a textfield? Is it something that is quite easily implemented or is it maybe put into consideration to add?

    Thanks in advance!
     
  40. HugoBD-Unity

    HugoBD-Unity

    Unity Technologies

    Joined:
    May 14, 2018
    Posts:
    499
    Hi @Thimo_! Yes, we created a task and added it to the roadmap. It should be fairly easy to do, thanks for bringing this up!
     
    Thimo_ likes this.
  41. FlaSh-G

    FlaSh-G

    Joined:
    Apr 21, 2010
    Posts:
    212
    Hello!

    I may have simply missed this, but is there (soon) an equivalent to this option in TMPro's Input Field?
    upload_2022-7-27_0-50-5.png
    I can't seem to disable this behavior for my UITK Text Field - every time I focus my text, it gets selected in its entirety.
     
  42. ChGuidi

    ChGuidi

    Joined:
    Dec 28, 2021
    Posts:
    111
    There is selectAllOnFocus and selectAllOnMouseUp on TextInputBaseField (see https://docs.unity3d.com/2022.2/Documentation/ScriptReference/UIElements.TextInputBaseField_1.html). Is that what you're looking for?
     
  43. FlaSh-G

    FlaSh-G

    Joined:
    Apr 21, 2010
    Posts:
    212
    Ah, thank you! I think I'm still getting used to this not being a package on its own, so I was working with 2021 LTS, where that didn't exist yet.
     
    ChGuidi likes this.
  44. ChGuidi

    ChGuidi

    Joined:
    Dec 28, 2021
    Posts:
    111
    Hi,

    For the multiline textfield, we would like to change the behaviour on enter. Instead of adding a newline to the text, we would like to submit the text. On shift+enter it should add a newline. I tried to achieve this by overriding
    protected override void ExecuteDefaultActionAtTarget
    but this doesn't seem to work.

    How can I do this? Or is this not possible?

    Thanks! Chloë
     
  45. HugoBD-Unity

    HugoBD-Unity

    Unity Technologies

    Joined:
    May 14, 2018
    Posts:
    499
    Hi @ChGuidi !

    Generally in UI Toolkit, events can be intercepted before they are handled by the VisualElement base class by registering to the TrickleDown phase. Here's our current doc on the subject.

    To answer your question more specifically here's how I would do it:
    Code (CSharp):
    1. myField.RegisterCallback<KeyDownEvents>((evt) => {
    2.  
    3.             if (evt.keyCode == KeyCode.Enter || evt.character == '/n')
    4.             {
    5.                 // Submit logic
    6.  
    7.                 evt.StopPropagation();
    8.                 evt.PreventDefault();
    9.             }
    10.  
    11.             if (evt.modifiers == EventModifiers.Shift && (evt.keyCode == KeyCode.Tab || evt.character == '/t'))
    12.             {
    13.                 // Focus logic
    14.  
    15.                 evt.StopPropagation();
    16.                 evt.PreventDefault();
    17.             }
    18.  
    19.  
    20. }, TrickleDown.TrickleDown);
    21.  
    Note that I haven't tried it, so it's possible it doesn't compile.
     
    mariandev likes this.
  46. ChGuidi

    ChGuidi

    Joined:
    Dec 28, 2021
    Posts:
    111
    Nice! Made a few adjustments but this is indeed exactly what I needed, thanks a lot!
     
    Thimo_ likes this.
  47. ErnestSurys

    ErnestSurys

    Joined:
    Jan 18, 2018
    Posts:
    94
    I'm trying to create a code field element that inherits from TextField.
    I faced the following problems:
    - I cannot access ScrollView in a constructor even after setting `multiline` to true and calling `
    SetVerticalScrollerVisibility`. There doesn't seem to be any way to configure the inner scroll view in the element constructor.
    - I also want to configure and use a horizontal scroller as well, there is no API for that
     
  48. HugoBD-Unity

    HugoBD-Unity

    Unity Technologies

    Joined:
    May 14, 2018
    Posts:
    499
    Hi Ernest!

    The ScrollView may be only accessible on the next frame. Try to register to the OnGeometryChangeEvent and fetch the inner ScrollView there. You should be able to select it using a typical selector: myField.Q<ScrollView>().

    Adding support for a Horizontal scroller is in our plans, but I can't give an ETA.

    If you're implementing a Code Editor, know that TextField still doesn't support rich text. However, it's one of our next priorities, and in the meantime, it will make it challenging to implement some syntax highlighting.
     
    ErnestSurys likes this.
  49. ErnestSurys

    ErnestSurys

    Joined:
    Jan 18, 2018
    Posts:
    94
    Thanks, for the tips, I'll be fine without the rich tags for now :)
    Could we get easier access to the inner scroll view? It would be a guessing game for the developer to find out that he can only access it on the next frame.
     
  50. johfirefly

    johfirefly

    Joined:
    Aug 21, 2018
    Posts:
    15
    Hey :) this me again :D

    I need to return focus to the input field after sending the message (after pressing return) - what is the right way to do this?

    If I use the command "textField.Focus();" , then it looks like the Unity does not focus the input field, but its parent component.

    P.s. I'm not sure if it's right to ask about this in this thread or would it be in future better to create a new one?

    P.P.s. Unity 2022.2.1f1