1

I know this question has been asked before but none of the solutions seem to work on mobile devices. The solution I found here worked perfectly on my PC but not on my tablet unfortunately. I've commented on the solution to see if they can help but it seems they haven't been on SO for over a year. I'm therefore trying my luck here to see if anyone know how to get their solution working on mobile devices?

This is my code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class TextFieldBehaviour : MonoBehaviour, ISelectHandler
{
    private InputField inputField;
    private bool isCaretPositionReset = false;

    void Start()
    { 
        inputField = gameObject.GetComponent<InputField>();

    }

    public void OnSelect(BaseEventData eventData)
    {
         StartCoroutine(disableHighlight());
    }


    IEnumerator disableHighlight()
    {
        Debug.Log("Selected!");

        //Get original selection color
        Color originalTextColor = inputField.selectionColor;
        //Remove alpha
        originalTextColor.a = 0f;

        //Apply new selection color without alpha
        inputField.selectionColor = originalTextColor;

        //Wait one Frame(MUST DO THIS!)
        yield return null;

        //Change the caret pos to the end of the text
        inputField.caretPosition = inputField.text.Length;

        //Return alpha
        originalTextColor.a = 1f;

        //Apply new selection color with alpha
        inputField.selectionColor = originalTextColor;

        if (inputField.touchScreenKeyboard.canSetSelection)
            inputField.touchScreenKeyboard.selection = new RangeInt(0, 0);
    }
}
Lloyd
  • 435
  • 3
  • 12
  • 29
  • It worked Dave!!!! Thank you so so much!!! I'll mark your post as the answer and award you with 50+ reputation points!!! Thank you! – Lloyd Sep 15 '19 at 18:19

1 Answers1

1

If you are writing about native on-screen keyboards you can try this:

if (inputField.touchScreenKeyboard != null && inputField.touchScreenKeyboard.active && inputField.touchScreenKeyboard.canSetSelection)
    inputField.touchScreenKeyboard.selection = new RangeInt(0, 0);

The parameters of the RangeInt are the start position and length of the selection;

Keep in mind this is not supported on all platforms. For all of the TouchScreenKeyboard functionality see the docs here and here.

Dave
  • 2,684
  • 2
  • 21
  • 38
  • Thank you so much for your comment. Do you know how your solution would slot into the code I linked to in my post? Thank you :) – Lloyd Sep 14 '19 at 22:51
  • Put this code instead of the start StartCoroutine in the OnSelect method. If this won't work for you let me know and I will try myself :) – Dave Sep 15 '19 at 11:12
  • Thanks @Dave. I've put the code in where you said but I get an errors saying "TouchScreenKeyboard does not contain a definition for 'canSetSelection'? – Lloyd Sep 15 '19 at 15:37
  • I can see this option is in Unity 2018 and higher. Do you have an older version? – Dave Sep 15 '19 at 16:01
  • Ok so I've opened it in Unity 2019. I get the following error now: An object reference is required for the non-static field, method, or property 'TouchScreenKeyboard.canSetSelection' – Lloyd Sep 15 '19 at 16:09
  • I've just added this: `private TouchScreenKeyboard keyboard;` which removed the error but now I get "Cannot implicitly convert type 'int' to 'UnityEngine.RangeInt'...... – Lloyd Sep 15 '19 at 16:11
  • Thanks so much @Dave. It all runs now but when I select the input field I get the error on your if statement saying "Object reference not set to an instance of an object"? – Lloyd Sep 15 '19 at 17:03
  • Ok no problem, thank you. I've just updated my original post with my code and deleted my unnecessary comments :) – Lloyd Sep 15 '19 at 17:10