- Home /
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;
}
Answer by Paolofix21 · Mar 11 at 09:07 PM
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() {
base.Awake();
_field = GetComponent<TMP_InputField>();
_field.onFocusSelectAll = true;
}
protected override void OnEnable() {
base.OnEnable();
field.Select();
}
}
I hope this solves your problem.
Aha, I was missing the "inputfield.Select()" part, thanks! There's no need to set the selection positions (not sure if that part even works, it didn't do anything in my app at least) and you can just put it in whatever class has access to the input field, in my case the script's on a completely different GameObject that enables the UI stuff.
"On Focus - Select All" is enabled by default, so is "Reset on DeActivate" but that one doesn't work (after disabling and re-enabling the text is still there, I even used to reset it manually because of that).
Answer by Master109 · Mar 11 at 06:01 PM
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);
What does this code do exactly? I tried to find out what "System.Reflection" is but the only thing I found was a warning not to use it unless you really know what you're doing (as it can be quite slow).
After posting the question I also noticed that the caret shouldn't be visible when text is selected but only when it isn't and the input field is focused.
The first line makes future key-presses change the inputField's text (I believe until you click outside inputField). The second line sets the inputField's carot to be visible. The carot is the position that new text will be inserted to or that text will be erased from with future key-presses. Reflection is quite slow, but I used it in my answer because I remember a while back that inputField.Select() didn't work for me, or at least not reliably. However, now that I write this, I realize that maybe it was Unity's regular InputField that had this lack of functionality.
Your answer
Follow this Question
Related Questions
UI Button Focus 2 Answers
inputField.ActivateInputField() not working 2 Answers
InputField instance isFocused always return false. 0 Answers
Why Android device need to loose Inputfield focus to listen to others events ? 0 Answers
Input fields act abritrarily 0 Answers