Focus on/select (TMP) input field?

I want to focus on a (TMP) input field and select its text once its parent panel is enabled. How do I accomplish this in Unity 2021.2?

This doesn’t do anything (if there’s already text in the input field):

if(inputField.text != "") {
    inputField.selectionAnchorPosition = 0;
    inputField.selectionFocusPosition = 5;
    inputField.caretPosition = 5;

It’s the easiest thing in the world since TextMeshPro added the On Focus - Select All toggle on its InputField class.

You just need to have a component like this on your InputField GameObject:

using TMPro;
using UnityEngine.EventSystems;

public class AutoSelectFieldTextOnEnable : UIBehaviour {
    private TMP_InputField _field;
    private void Awake() {
        _field = GetComponent<TMP_InputField>();
        _field.onFocusSelectAll = true;
    protected override void OnEnable() {

I hope this solves your problem.

Make sure using System.Reflection; is at the top of your script, and before the code you provided add inputField.GetType().GetField("m_AllowInput", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(inputField, true); inputField.GetType().InvokeMember("SetCaretVisible", BindingFlags.NonPublic | BindingFlags.InvokeMethod | BindingFlags.Instance, null, inputField, null);