Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable SelectAll() text of UI.InputField when it gets focus?

The UI InputField when it gets focus highlights all text inside. I would like to move the caret to the end of the text so the user can keep writing the text within. Currently I have a hack solution, which works, however there it is still a short moment when the text is highlighted. Here is my hack:

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) 
    {
        isCaretPositionReset = false;
    }

    void Update()
    {
        if(inputField.isFocused == true && isCaretPositionReset == false)
        {
            inputField.caretPosition = inputField.text.Length;
            isCaretPositionReset = true;
        }
    }
}

I was also checking out the source code of InputField. But I have trouble trouble creating a custom one without the SelectAll() function. I am getting a bunch of errors due to the protection level of the UnityEngine.UI.SetPropertyUtility.

like image 833
rjth Avatar asked Oct 21 '25 03:10

rjth


1 Answers

There is a trick to disable the short moment when the text is highlighted. I managed to redo this without the Update() function.

1.Get the Color of the InputField.selectionColor. Set its alpha to 0.

2.Apply the new color from #1 to the InputField.

3.Wait for one frame. You must because Unity caret waits for one frame to appear.

4.Change the InputField caret position.

5.Change the InputField.selectionColor alpha back to 1.

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;
    }
}

NOTE:

The best way to move caret to the end of the text is with the MoveTextEnd function instead of inputField.caretPosition. You will notice a bug with inputField.caretPosition if your text is long.

If you care about this, replace inputField.caretPosition = inputField.text.Length; with inputField.MoveTextEnd(false); in the code above.

like image 52
Programmer Avatar answered Oct 23 '25 17:10

Programmer