Search Unity

TextMesh Pro Issue with custom validator and input field

Discussion in 'UGUI & TextMesh Pro' started by aurelien-morel-ubiant, Jul 24, 2019.

  1. aurelien-morel-ubiant

    aurelien-morel-ubiant

    Joined:
    Sep 27, 2017
    Posts:
    275
    Hello !
    We use TMPro for a while now and we were in 1.3.0 and we updated to 1.4.1 but now some fields doesn't work in our project.
    We have a custom validator to remove emoji from our field with the following Validate method :

    Code (CSharp):
    1. using System;
    2. using TMPro;
    3. using UnityEngine;
    4.  
    5. [Serializable]
    6. public class NoEmojiValidator : TMP_InputValidator
    7. {
    8.     private TMP_InputField InputField { get; set; }
    9.  
    10.     private int m_startSelectIndex = 0;
    11.     private int m_endSelectIndex = 0;
    12.  
    13.     private bool IsSelected { get { return m_startSelectIndex != m_endSelectIndex; } }
    14.  
    15.     public void Init(TMP_InputField inputField)
    16.     {
    17.         InputField = inputField;
    18.         InputField.onTextSelection.AddListener(OnTextSelection);
    19.         InputField.onEndTextSelection.AddListener(OnEndTextSelection);
    20.     }
    21.  
    22.     private void OnDestroy()
    23.     {
    24.         InputField.onTextSelection.RemoveListener(OnTextSelection);
    25.         InputField.onEndTextSelection.RemoveListener(OnEndTextSelection);
    26.     }
    27.  
    28.     private void OnTextSelection(string text, int startSelectionIndex, int endSelectionIndex)
    29.     {
    30.         m_startSelectIndex = startSelectionIndex;
    31.         m_endSelectIndex = endSelectionIndex;
    32.     }
    33.  
    34.     private void OnEndTextSelection(string s, int startSelectionIndex, int endSelectionIndex)
    35.     {
    36.         m_startSelectIndex = startSelectionIndex;
    37.         m_endSelectIndex = endSelectionIndex;
    38.     }
    39.  
    40.     public override char Validate(ref string text, ref int pos, char ch)
    41.     {
    42.         if ((int)ch >= 55357)
    43.         {
    44.             return '\0';
    45.         }
    46. #if UNITY_EDITOR || UNITY_STANDALONE || UNITY_WEBGL
    47.         if (IsSelected)
    48.         {
    49.             int minIndex = Mathf.Min(m_startSelectIndex, m_endSelectIndex);
    50.             int maxIndex = Mathf.Max(m_startSelectIndex, m_endSelectIndex);
    51.             int length = maxIndex - minIndex;
    52.  
    53.             text = text.Remove(minIndex, length);
    54.             text = text.Insert(minIndex, ch.ToString());
    55.         }
    56.         else
    57.         {
    58.             text = text.Insert(pos, ch.ToString());
    59.         }
    60. #endif
    61.         pos += 1;
    62.         return ch;
    63.     }
    64. }
    65.  
    66.  

    I think our issue come from here cause when we use any other "Character Validation" all is fine. But as soon as we use this particular validator it fails whereas it was ok before.

    I continue to search where is the problem but if you have any hint or if it's a bug from your side, thanks in advance to answer :)

    P.S : This is our issue =>

    Code (CSharp):
    1. IndexOutOfRangeException: Index was outside the bounds of the array.
    2. TMPro.TMP_InputField.GenerateCaret (UnityEngine.UI.VertexHelper vbo, UnityEngine.Vector2 roundingOffset) (at Library/PackageCache/com.unity.textmeshpro@1.4.1/Scripts/Runtime/TMP_InputField.cs:3313)
    3. TMPro.TMP_InputField.OnFillVBO (UnityEngine.Mesh vbo) (at Library/PackageCache/com.unity.textmeshpro@1.4.1/Scripts/Runtime/TMP_InputField.cs:3281)
    4. TMPro.TMP_InputField.UpdateGeometry () (at Library/PackageCache/com.unity.textmeshpro@1.4.1/Scripts/Runtime/TMP_InputField.cs:3219)
    5. TMPro.TMP_InputField.Rebuild (UnityEngine.UI.CanvasUpdate update) (at Library/PackageCache/com.unity.textmeshpro@1.4.1/Scripts/Runtime/TMP_InputField.cs:3194)
    6. UnityEngine.UI.CanvasUpdateRegistry.PerformUpdate () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/CanvasUpdateRegistry.cs:198)
    7. UnityEngine.Canvas:SendWillRenderCanvases()

    I can see in that case your "caretPositionInternal" has a bad value : -1
     
    mcallet likes this.
  2. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    Can you provide me with a simple repro project / scene to test this?

    You can always submit a bug report with the included project and then provide me with the Case #.
     
  3. aurelien-morel-ubiant

    aurelien-morel-ubiant

    Joined:
    Sep 27, 2017
    Posts:
    275
    Hello @Stephan_B
    I made a really super simple repro project in that case : #1172102

    I really hope you will find why it's not working since our last update :)

    I update my ticket cause it doesn't work with Unity 2019 and TextMeshPro 2.0.1
     
    Last edited: Jul 25, 2019
  4. aurelien-morel-ubiant

    aurelien-morel-ubiant

    Joined:
    Sep 27, 2017
    Posts:
    275
    @Stephan_B sorry to insiste but I didn't have any return from the QA team (whereas it's quite fast most of time). Any news on this issue ?
     
  5. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    I'll try taking a look at it tomorrow and provide feedback shortly thereafter.
     
    aurelien-morel-ubiant likes this.
  6. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    I am currently looking at the case and it appears there is an extra null character that is causing the issue with your custom validator.

    QA has already replied to the case and suggested a code change which will allow you to get around the reported issue which is to simply check for char(0).

    I'll take a close look on my end to figure out what is going on with this null character.
     
  7. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    I was able to identify the source of the Null character being forwarded to the Validate method.

    This behavior will be as it was in version 1.3.0 of the TMP package. The solution for this issue will be in the next release of the TMP package which is expected to be version 1.4.2 for Unity 2018.3 or newer and version 2.0.2 for Unity 2019.x.

    Until the next package is available, just filter out the Null character as per the QA's suggestion.
     
    aurelien-morel-ubiant likes this.
  8. aurelien-morel-ubiant

    aurelien-morel-ubiant

    Joined:
    Sep 27, 2017
    Posts:
    275
    Yeah ! Thanks again for the really fast answer and fix @Stephan_B and to the QA Team !
     
  9. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    As usual you are most welcome :)
     
  10. aurelien-morel-ubiant

    aurelien-morel-ubiant

    Joined:
    Sep 27, 2017
    Posts:
    275
    @Stephan_B I was maybe too enthusiastic cause it seems it don't work on mobile devices.
    All our custom validator using the one mentionned above doesn't take any input on mobile. So We can see the input action (the field enter in a selected state) but the native mobile keyboard never shows up.

    Everything is fine on Standalone and Editor probably due to the no-keyboard.

    I try to investigate further... but it's quite a major issue for us ^^"
     
    jhina likes this.
  11. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    Can you submit a new bug report with a project / scene that will enable me to re-test one of your custom validator?
     
  12. aurelien-morel-ubiant

    aurelien-morel-ubiant

    Joined:
    Sep 27, 2017
    Posts:
    275
    I think the case 1172102 must (still open) must be enough to repro this issue ;)
    But I can submit again exactly the same with default target platform to android or iOS :)
     
  13. aurelien-morel-ubiant

    aurelien-morel-ubiant

    Joined:
    Sep 27, 2017
    Posts:
    275
    You just need to add the QA fix in it. But I can understand that you need it for the tracking.
     
  14. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    Actually reflecting on this last night as I went to sleep, I realized this was related to the following:

    In the TMP_InputField.cs at line 3717, make the following changes

    Code (csharp):
    1.  
    2. // Replace the following line with the line below
    3. if (shouldHideSoftKeyboard == false && m_ReadOnly == false && contentType != ContentType.Custom)
    4.  
    5. // New line should be
    6. if (shouldHideSoftKeyboard == false && m_ReadOnly == false)
    7.  
    That should solve the issue.
     
    kirbyderby2000 and SkylinR like this.
  15. aurelien-morel-ubiant

    aurelien-morel-ubiant

    Joined:
    Sep 27, 2017
    Posts:
    275
    Ok I will check this tomorrow morning !
    Any ETA for the 1.4.2 release with all those fixes ?
     
  16. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    It will be version 1.5.0 and it is week to week now. Hoping to release a preview next week.
     
    aurelien-morel-ubiant likes this.
  17. aurelien-morel-ubiant

    aurelien-morel-ubiant

    Joined:
    Sep 27, 2017
    Posts:
    275
    I just test it and it works. The keyboard appears again after your fix.
     
    Stephan_B likes this.
  18. leedan_johnson

    leedan_johnson

    Joined:
    Jun 17, 2019
    Posts:
    6
    This workaround was still required for me in Jan 2020
     
  19. SkylinR

    SkylinR

    Joined:
    Sep 4, 2015
    Posts:
    6
    Yup it's still working. Thanks a lot Stephan! :) You save my day
     
  20. kirbyderby2000

    kirbyderby2000

    Joined:
    Apr 28, 2017
    Posts:
    35
    Thank you! Dumb question but where is the TMP_InputField.cs script? I can't find it in the "TextMesh Pro" folder in my project "Assets" folder. Definitely imported TMP into my project with no problem.
     
  21. SkylinR

    SkylinR

    Joined:
    Sep 4, 2015
    Posts:
    6
    It's not in Your asset folder. It should be under path like that
    {your_project_directory}\Library\PackageCache\com.unity.textmeshpro@1.4.1\Scripts\Runtime
     
    kirbyderby2000 likes this.
  22. cobrecht

    cobrecht

    Joined:
    Nov 14, 2018
    Posts:
    2
    Thanks for the work-around.

    The package manager overrides the fix every time the editor restarts though ...
    Last time I forgot to put it back and it affected all my users :(
    Any news of an official fix ?
     
  23. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    Fix was released in one of the preview releases for version 1.5.0. The latest is Preview 4 which became available this week.
     
    mason_c likes this.
  24. cobrecht

    cobrecht

    Joined:
    Nov 14, 2018
    Posts:
    2
    Thanks !
    I upgraded to the "preview 4" package version and the line is fixed
     
  25. bsivko

    bsivko

    Joined:
    Jan 24, 2018
    Posts:
    8
    I checked my Unity 2019.3.13f1, there is com.unity.textmeshpro@2.0.1, and the line above:

    Code (CSharp):
    1. if (shouldHideSoftKeyboard == false && m_ReadOnly == false && contentType != ContentType.Custom)
    2.  
    is still present in the source code.

    On my phone the problem is still occur, it's not fixed.
     
  26. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    Please test with the latest release which is version 2.1.0-preview.13 for Unity 2019.x.
     
  27. bsivko

    bsivko

    Joined:
    Jan 24, 2018
    Posts:
    8
    Just imported 2.1.0-preview.13 version for the same app. And it's fine for it, the bug is fixed.