Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

InputField.caretPosition not working on iOS with mobile input hidden

Discussion in 'UGUI & TextMesh Pro' started by Xyra, Sep 11, 2015.

  1. Xyra

    Xyra

    Joined:
    Jul 18, 2012
    Posts:
    17
    I've created a script to allow you to tap on the screen to position the caret within an InputField on iOS by tapping on letters in the field.

    The caret positions perfectly before or after the letter by setting InputField.caretPosition, however as soon as you start typing again the caret jumps to the end of the line and the text appends to the line rather than adding in where the caret is.

    Any one else found this, or have a work around?
     
  2. Xyra

    Xyra

    Joined:
    Jul 18, 2012
    Posts:
    17
    Submitted a bug:
    726802
     
  3. phil-Unity

    phil-Unity

    Unity UI Lead Developer

    Joined:
    Nov 23, 2012
    Posts:
    1,226
    This has been a known issue for a while so i'm closing your bug as a duplicate.

    This issue is that the touch screen keyboard has no way of positioning the caret on the device and the device always sets the caret to the end. Its something i'm working on but is something i need to implement on multiple devices which takes time.
     
  4. Xyra

    Xyra

    Joined:
    Jul 18, 2012
    Posts:
    17
    Fair enough, thanks for taking the time to look.

    I might try and implement a workaround - perhaps store the last caret position at the end of late update and check for changes to the .text value in the field. If it finds any then it resets the caret and puts the text at the last known position? Might be flickery though if the text extends past the end of the box I suppose
     
  5. phil-Unity

    phil-Unity

    Unity UI Lead Developer

    Joined:
    Nov 23, 2012
    Posts:
    1,226
    If you can find a work around by all means use it as i don't know when i'll be able to get this work released.
     
  6. roclau0102

    roclau0102

    Joined:
    Oct 11, 2014
    Posts:
    6
    Is that has been solved?
     
  7. twiggs1

    twiggs1

    Joined:
    Jul 6, 2018
    Posts:
    7
    Yeah not sure if this has been solved yet
     
  8. jpair

    jpair

    Joined:
    Jan 3, 2019
    Posts:
    3
    Does anyone know if the caret positioning has been fixed or have a working solution? I have an InputField which works fine when playing inside the Editor. However, when I build and deploy to my iOS device, the behavior is not like any other form field I've seen in other languages. If you start typing in the field and mobile click somewhere in the typed text area to make a correction, the cursor appears to be located where you clicked until you start typing again, at which point all edits are applied starting at the end of the text line. I've tried lots of options like keyboardType, setting caretPosition, etc but nothing seems to work. Does anyone have a workaround or ideas on what I'm doing wrong? I'm using Unity 2018.3 Pro version if that matters. Starting an upgrade to 2019.3.6 to see if problem exists in this version. Thanks for any help or advice.
     
  9. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    What version of the TMP package are you using?

    Please test with the latest version which is 2.1.0-preview.8 for Unity 2019.x.
     
  10. jpair

    jpair

    Joined:
    Jan 3, 2019
    Posts:
    3
    Stephan_B, I'm using TMP 1.3.0 - will upgrade and try newer version. Thank you for recommendation. Will reply if this fixes the problem.
     
  11. jpair

    jpair

    Joined:
    Jan 3, 2019
    Posts:
    3
    Hi Stephan_B, I upgraded from 2018.3 to 2019.3 which include the newer TMP version. Works like a charm on iOS mobile now. Thank you. As is often the case with upgrades though, it didn't come without a few hurdles to solve. Glad to have it working with 2019. Thanks again!
     
  12. ina

    ina

    Joined:
    Nov 15, 2010
    Posts:
    1,080
    Has this been fixed for regular InputField?
     
  13. dezzell

    dezzell

    Joined:
    Feb 7, 2015
    Posts:
    32
    Same issue with regular Input Fields. Seems TMP is no longer supported in the latest version of Unity 3D 2020.2.

    When I have Hide Mobile Input enabled, and press my finger in the middle of text, it jumps caret to the end and opens keyboard.

    I press again, the keyboard goes away, press again and keyboard opens and jumps caret to end of input.

    Strange thing it works fine on IOS 14.4 and on PC and Mac, however does not work on Android version 11, Ultra S20.

    It use to work in older versions of Android and Unity. Now when I press any Input Field, Single Line or Multi Line with Mobile Input disabled, the keyboard opens and jumps Caret to the end instead of where I press within the text.

    I am upgrading from Unity 2020.2.1f1 to 20.20.2.7f1 to see if this is resolved and will let you know.

    Not sure if this is an Android issue or Unity3d issue.
     
  14. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    TMP is still very much supported and on active development. The latest release of the TMP package for Unity 2020.x is version 3.0.4 available via the package manager.

    In terms of the keyboard behavior on Android, it is driven by Android native code where we don't have control over the opening and closing of the input fields. This is something I have asked the Android team at Unity to look into.

    You should be able to find several posts on the forum about this. One such post includes reference to a bug report for tracking this issue.
     
    Last edited: Mar 5, 2021
  15. dezzell

    dezzell

    Joined:
    Feb 7, 2015
    Posts:
    32
    Nope, after updating Android Studio to 4.1.2 and Unity 2020.2.2.7f1, now the app will not open at all on Android version 11. I created a Google Play build, uploaded for internal testing, and not working now. I tried to revert back to Unity 2020.2.1f1 and still not working. Must be something with Android Studio 4.1.2.

    I made a backup first, however feel it has something to do with latest version of Android Studio. All the Android packages for Android 10 and above were already updated so they did not require any updates, only Android Studio was updated and Unity 3D.

    So now I need to figure out why my app is broken and how to fix. Always updates and things breaking, makes me want to stop developing apps and games, lol :)
     
  16. dezzell

    dezzell

    Joined:
    Feb 7, 2015
    Posts:
    32
    When I go to Package Manager and TextMesh Pro then view in the Asset Store, it says it is no longer supported.

    Unfortunately, TextMesh Pro is no longer available.
    This package has been deprecated from the Asset Store. This means that new purchases of the package are not allowed and that only users who already purchased or downloaded the package before it was deprecated, are allowed to download it.
    In most cases, package deprecation happens because the publisher is unable or unwilling to support the package anymore. We suggest looking for alternative packages or contacting the publisher directly.
    If you've already purchased it and need to download a copy, you can do so here.

    So I figure it was removed. I did a search for TMP or TextMesh Pro and just see addons and not the actual version.

    Well I know this is an Android thing as I've seen other articles about this. After updating Android Studio to 4.1.2, nothing is working now, just a quick black screen, green dot in top left corner, and it immediately closes the app.

    I am looking into why this is not working.
     
  17. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    The version of TextMesh Pro available on the Asset Store is the legacy version which was for Unity 2017 or older. Since Unity 2018.x , TextMesh Pro has been available via the Package Manager as seen below.

    upload_2021-3-6_13-34-5.png

    As you can see the latest release is version 3.0.4 which was released on Feb 19.

    Since you appear to be using the legacy Asset Store version of TextMesh Pro, you will need to migrate from this Asset Store (dll) based version of TMP to the Package Manager version as a lot of changes have occurred over the past 4 years. See the following post / thread about how to migrate to the Package Manager release of TMP.

    In case you did not know, TextMesh Pro was acquired by Unity 4 years ago and it still very much on active development. Since it is now part of Unity, it is available via the Package Manager and no longer via the Asset Store.
     
  18. dezzell

    dezzell

    Joined:
    Feb 7, 2015
    Posts:
    32
    It seems the update from 2020.2.1f1 to 2020.2.2.7f1 caused the issue of the app on Android crashing. All it said it the Android Device Monitor Log Cat was this.

    The file com.appname/base.apk/assets/bin/Data/level0 is corrupted! Remove it and launch unity again! [Position out of bounds!] Fatal signal 5 (SIGTRAP), code -6 (SI_TKILL) in tid 10004 (Thread -5)

    So I attempted to revert back to 2020.2.1f1 and still had the problem. I then restored from a backup I did before upgrading to 2020.2.2.7f1 and this fixed the problem with the app crashing on Android 11.

    With Android studio 4.1.2 and all updates, and the app is now back at 2020.2.2.1f1, I am able to build for Google Play, however the input fields still do not work correctly.

    Unable to press in the middle of text like you can on IOS. Also I noticed if I type fast, the text gets hosed up, it jumps back a couple spaces. Like typing my password or any text, it starts typing and jumps a character or two behind and caused the password to fail, or messes up typing and you can't easily go to that spot and fix. Have to delete all text.

    I understand this can be an issue with the latest version of Android 11 and Unity running on this version.

    Was looking for an easy fix. Works fine in Editor on Windows, Mac, and IOS, just not on Android. I was thinking a function that prevents Keyboard from opening, capture caret position, then enable keyboard, and jump to that position. I tried some code here and there, and haven't fixed it yet.

    The TextMesh Pro says it is no longer available. I can download previous versions however if this is no longer supported, I would hate to run into an issue with having to go back to standard fields and wasting time in future releases.

    Will wait for Android 12 to hopefully fix this issue. If anyone has a piece of code that can allow for caret position within text selected, that will be helpful for many.

    I will keep messing around with the code. If I figure something out, will post here.
     
  19. dezzell

    dezzell

    Joined:
    Feb 7, 2015
    Posts:
    32
    OK, I understand now about TMP, however, does it do the same thing as standard fields on Android? I have the older asset store for TMP.

    If it works better on Android with the ability to press in the text, I will consider updating all the fields to TMP, however if it behaves the same way on Android as a standard field, and does not move caret to position selected, then it is not worth the time to switch to TMP.

    Have you tested TMP 3 field on Android and does it work as expected, or does it just open keyboard and jump to end of text.

    Also the fast typing, this seems to be a known issue as well. While typing fast, it jumps a character or two behind.

    I will look at updating TMP and run a test, I just didn't want to have to do all these code changes for TMP if it does the same thing on Android 11 as a standard field does.
     
  20. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    There has been a massive amount of changes in TMP over the past 4 years with the addition of many new features. Besides having to migrate from the legacy version to the newer package manager versions which will require using the Project GUID Remapping Tool, you will most likely run into stuff that will require changes, tweaks, etc.

    In terms of the Input Field, I would suggest creating a new project just for testing the TMP Input Field on Android in 2020.x using this latest release of TMP. If it behaves more as you would expect, then consider migrating your main project over.
     
  21. dezzell

    dezzell

    Joined:
    Feb 7, 2015
    Posts:
    32
    Just wanted to provide an update and Good News. I updated to the latest version of TMP 3, following the instructions to remove old legacy version, and import TMP from Window -> TextMesh Pro -> Import TMP Essential Resources menu using Unity 3D 2020.2.1f1 and latest version of Android Studio 4.1.2 with all updates installed.

    I replaced all text fields on every form and ran several test on Samsung Galaxy S20 Ultra with Android 11 and all updates as of March 18th, 2021.

    At first it did the same thing, selected all text and did not jump caret to position I pressed on. Then I noticed an option called OnFocus - Select All. After unchecking this, I was able to press on a spot within the text and it jumps right to that position with the caret.

    Awesome. This option was not available on the standard UI input fields.

    So this solves the issue of the missing caret on Android devices. Will test on Apple next.

    Just had to add a few lines of code to capture the new Input:

    using TMPro;

    public GameObject RecordAffirmationTextBox;


    string getafftext = RecordAffirmationTextBox.GetComponent<TMP_InputField>().text;

    And it also solved the issue with typing fast and it auto jumping to a a previous character. This made typing in the password field difficult which is now resolved with TMP.

    So now I can type quickly and it stays at the end of the text with TMP.

    I greatly appreciate your help Stephan_B and your advice to Update to TextMesh Pro 3. Even thou it took me days to replace all the fields and update code, it was well worth it to be able to type quickly, and press anywhere in the text to update.

    Everyone who is experiencing challenges with the standard UI Input Fields on Android, I highly recommend going to TextMesh Pro 3 or above. It is already built in with the latest version of Unity 3D, and all you have to do is go to the Window Menu and select TextMesh Pro -> Import TMP Essential Resources. Make sure you remove the old version of TMP before importing the new version.

    Then uncheck the onFocus - Select All check box under Control Settings on the TextMesh Pro Input Field.

    Great Job!!!

    You all Enjoy your time and have fun programming!!

    TextMeshPro.jpg

    TextMeshPro2.jpg

    TextMeshPro3.jpg
     
    ltomov and Stephan_B like this.
  22. waldgeist

    waldgeist

    Joined:
    May 6, 2017
    Posts:
    386
    These input fields are driving me mad on Android. I tried out the latest TMP version available for Unity 2019.4, which is 2.1.6. At least you can position the cursor now, which was not possible with UI Inputs. However, once you tap again (to position the cursor somewhere else), the focus gets lost and the keyboard disappears. Why is this so? It is quite natural that someone wants to tap a couple of times to get the caret to the right position.

    Also, I am missing a way to reposition the caret by tapping, holding and moving. Our users are also complaining that they can't paste in passwords if UI Inputs are used as password fields. Is this possible with TMP now? I couldn't find a way to paste something so far.

    I personally think, the only acceptable "benchmark" for these input fields should be: behave like a native input on the devices does. Nothing short than that. We're using these fields for login purposes, and if there are quirks, we might lose users right away.
     
    hoangyenpham and ina like this.
  23. meghnazala746

    meghnazala746

    Joined:
    Dec 28, 2020
    Posts:
    8

    ok now does it fixed or not?... bescause i m facing issue in inpuitfeld caret position ...i can not reset caret position at where i clicked on inputfield...whenever cllicked on inputfield it sets the caret at the last...end of the all text.. so can not edit the text on the middle of the inputfield
     
    Roman_34 likes this.
  24. TheVirtualMunk

    TheVirtualMunk

    Joined:
    Sep 6, 2019
    Posts:
    150
  25. Yiming075

    Yiming075

    Joined:
    Mar 24, 2017
    Posts:
    32
    Lol 2022 still hasn't been fixed.
     
  26. Roman_34

    Roman_34

    Joined:
    Nov 1, 2018
    Posts:
    19
    I am using Unity 2019.4.31 and TextMeshPro version 2.1.6 installed by Package Manager.
    Settings of TextMeshPro - InputField:
    2022-10-17_11-15-12.png
    The Unity editor has a cursor. But there is no cursor on the iOS device (iPhone)! It not visible! Can someone explain what's wrong?
     
    Muckel likes this.
  27. Muckel

    Muckel

    Joined:
    Mar 26, 2009
    Posts:
    471
    it's a long time know'n secret feature of TMP...
    M.
     
  28. Yandalf

    Yandalf

    Joined:
    Feb 11, 2014
    Posts:
    491
    It looks like the onscreen keyboard's caret position still doesn't properly update with changes to the caret position in the TMP_InputField, I've been banging my head against this problem since yesterday. Made a new forum thread here in the hopes of maybe getting an answer I couldn't find after hours of googling.
    Very disappointing to see Unity continue to leave bugs like these present for years on end.
     
  29. AndreiMarian

    AndreiMarian

    Joined:
    Jun 9, 2015
    Posts:
    77
    Although "colonies of bugs" is Unity's unique feature, like a trademark, I think this one is out of Unity team's reach. They've pulled out all the stops to make the feature of hiding the native input field. But there's only so much you can do when you have no access to the (Android) system.
    I salute the idea and its implementation of rendering the native input outside of the visible screen.
    As a compromise I suggest that tapping in the Unity input field closes the keyboard (which currently doesn't happen) and (virtually unnoticeably) quickly reopens it as usual, i.e. with the carret at the new position. Still, the issue with selecting text remains.