- Home /
Draw your own InputField, cursor/selection location
I would like to draw my own InputField control, using 3d objects.
I’ve got the mouse-input working, so I can click on parts of my text to highlight it, and move the cursor around.
Rather than re-invent how the Input field handles keystrokes, given the cursor position, and highlighted text, I would like to use the functionality that is already built into the InputField.
I started by simply hiding the input field, and sending it the keystrokes. Then lookup the resultant text, and display that. This works fine, but has limitations I can’t seem to get around.
The problem I’m hitting is drawing the cursor. Where in the InputField is (Waldo.. no..) the cursor? The issue goes in the other direction too, if the user clicks with the mouse, how do I place the cursor at the given character position in the (hidden) InputField, so the next keystoke will go in the right place.
Unable to find the cursor information, I TRIED to create by own input control code. This worked fairly-well, but has a ton of minutiae still left to handle/implement. Then I tried to handle marked-up text (RichText): which quickly became a nightmare of additional minutiae to handle. Surely there is something out there I can use, rather than having to re-invent the wheel.
I’m not MARRIED to using unity input field’s functionality, but whatever code I use should be able to handle the unity Rich text markup.
(p.s. I realize editing markedup-text fields can yield some odd behavior, but I’m ok with that.)
Research: I DID find this answer: http://answers.unity3d.com/questions/275973/find-cursor-position-in-a-textarea.html
But it doesn't seem to work with an inputfield, I guess it’s only for the old GUI? Maybe I’m doing it wrong? I added a script with this function to an inputfield.
void OnGUI () {
TextEditor editor = (TextEditor)GUIUtility.GetStateObject(typeof(TextEditor), GUIUtility.keyboardControl);
Debug.Log(editor.selectPos.ToString()); //output is ALWAYS zero
}
How are you wanting it to work? If all you want is a custom image at the end of the text input that easily "fudgable" but if you want to move the cursor to also appear between text it'd not work with the method I'm thinking of.
PS also not getting mail notifications.
I'd like to be able to draw the cursor over any selected text: be it a single character, or multiple characters. If no characters are selected, it WOULD indeed be drawn between characters (or at the beginning or end of the string), based on the cursor position.
Again, I have the cursor drawing part down, I just need that cursor/selection position/range.
Thanks for heads up about the notifications! :)
Answer by banniandoko · Nov 17, 2016 at 02:23 PM
Maybe you could try this code, I used this code to select/ highlight text in the content/ textField
GUI.FocusControl("MyTextField");
editor = (TextEditor)GUIUtility.GetStateObject(typeof(TextEditor), GUIUtility.keyboardControl);
//editor.MoveUp();
editor.OnFocus();
editor.cursorIndex = 25; // cursor start position
editor.selectIndex = 56; // cursor end select position
Any idea how to make that work on an Scene's GameObject's InputField? I can make that work on fields I create in code like
GUI.SetNextControlName("$$anonymous$$yTextField");
username = GUI.TextField(new Rect(10, 10, 100, 20), username);
But I'm not sure how to use GUI.FocusControl to focus on a GameObject's Component (be it an InputField, or Text).
I'm using this script to focus to "inputfield"
void Update()
{
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Tab))
{
Selectable next = system.currentSelectedGameObject.GetComponent<Selectable>().FindSelectableOnDown();
if (next != null)
{
InputField inputfield = next.GetComponent<InputField>();
if (inputfield != null)
inputfield.OnPointerClick(new PointerEventData(system)); //if it's an input field, also set the text caret
system.SetSelectedGameObject(next.gameObject, new BaseEventData(system));
}
//else Debug.Log("next nagivation element not found");
}
}
I used the UnityEngine.EventSystem.SetSelectedGameObject function as you suggested. Alas, the results of this debug are always "0,0"- did I miss somthing? (where objectToFocusOn is the InputField itself- also tried the child with the text component- same result)
eventSystem.SetSelectedGameObject(objectToFocusOn, new BaseEventData(eventSystem));
TextEditor editor = (TextEditor)GUIUtility.GetStateObject(typeof(TextEditor), GUIUtility.keyboardControl);
editor.OnFocus();
Debug.Log("This cursur pos: " + editor.cursorIndex + "," + editor.selectIndex);
Answer by Glurth · Nov 18, 2016 at 06:05 PM
Been so long since I've looked at this: looks like selectionFocusPosition and selectionAnchorPosition are working ok for this. Guess they are new? https://docs.unity3d.com/ScriptReference/UI.InputField-selectionAnchorPosition.html
I'll do more tests and post back,
Your answer
Follow this Question
Related Questions
uGUI inputfield text truncates? 2 Answers
Saving only Integers in an inputfield via code 1 Answer
Problem with UI input field in android 1 Answer
How to disable TouchScreenKeyboard for InputField? 0 Answers
Get Text from an Input Field on one scene and use it as a text on another scene Unity c# 0 Answers