- Home /
[4.6 UI][JS] Focusing on InputField issues.
So I've been playing around with the new UI work in Unity 4.6, and have come quite stuck with setting focus on a InputField. What I read around about .Select()
seems to be the way to go, but that just doesn't work for me...
I don't know if it's currently a bug with Unity, as the error is MissingMethodException: UnityEngine.UI.Text.Select
, but the bug tracker doesn't point out any problems with it.
I think the problem might be in what I'm targetting. How it's setup is:
Canvas
> Command Line (has component Input Field)
> Line Text (has component Text)
I want, on key press, to lock the focus to the text area so that the user can only type. So in the end, question is what do I need to .Select()
, potentially in conjunction with .ActivateInputField()
.
This is for making an in game console in Unityscript (Javascript).
Answer by Daeval · Jan 14, 2015 at 10:29 PM
From what I've found, you have to use both Select() AND ActivateInputField() on the InputField component itself. So, where UserInputField is a reference to the InputField component on your Command Line GameObject:
UserInputField.Select();
UserInputField.ActivateInputField();
My understanding is that an Active InputField has isFocused set to true, displays a cursor, and accepts typed input. A Deactivated InputField has isFocused set to false, does not show the cursor, and cannot be typed in.
These states are controlled by ActivateInputField() and DeactivateInputField(), and are distinct from the Selectable system - an InputField can be Selected but Deactivated, and vice versa.
You need your InputField to be both Selected by the Selectable system AND Active in order to be "in focus" to the end user.
I also ran into issues where calling these at the wrong time would result in newlines in the input text that was read by the rest of my code. I don't have any details for a repro at the moment though.
Your answer
Follow this Question
Related Questions
How to use onValidateInput in JavaScript? 1 Answer
Hide cursor in InputField (4.6 UI) 2 Answers
How to save InputField value? 1 Answer
Changing UGUI InputField keyboard appearance on iOS 2 Answers