Input field and it's 'constant' text or floating input field?

Hey.

My game has a part that is needed to look like a windows command line:
It has input field, where you can type commands like “dir” in this example.
c:\Users> dir

“c:\Users>” ← The directory path is needed to be constant, it cannot be erased, but commands can be changed, or erased with keyboard.

The problem is this, how can the input field have text that you cant erase and another part text that is customizable.

I tried making it so that the directory path is another Text Area, and the commands are added to Input Field where is another Text Area, where you put your commands. But when the directory path gets longer, it looks like this:
c:\Users\directorypath\anot
herdirectorypath>

So it changes lines.
And if i make the Text Area longer, it looks like this:

c:\Users>                                          dir

So the command and directorypath have then too big gap between them.

What could be best way to do this? Is it possible to make a floating input field, so it moves to the right as the directory path gets longer?

Unity can auto size text to fit content. You’re digging into right direction but you need to go deeper. Text in unity redered via mesh. This mesh is just bunch of quads (rectangles with a letter texture inside). Last 6 vertices are the quad of last letter. You want to place your input field so the first quad of input text will be right next to last quad of printed text if printed text contains special character at last position (‘>’ for instance).
Or just add invisible input field, focus it, and add/remove it contents to text label text end.

Thank you mate, but i didn’t totally understand what you meant. How about making two imput fields on top of each other and then use InputField.CaretPosition for getting the caret position of the directory, and then just giving it to command position! I’ll try that soon!

In Game window turn on Shaded Wireframe display mode and look at your text. You want to match that black rectangles positions. CaretPosition is not in pixels, it given in characters, like a cater is at char 0 in string.

1 Like

How can you match those rectangles when you have two different text fields side by side and the first text field changes it’s text length? =D

Easy. Read vertex positions, calculate top left vertex offset from objects positions, matcth positions + offset.

1 Like

I might be slightly retard, but i moved the whole ui-element via transform because in my case, it’s ok to use pixels.

Vector3 inputPos = passwordField.GetComponent<RectTransform>().anchoredPosition;
passwordField.GetComponent<RectTransform>().anchoredPosition =(new Vector3(inputPos.x+30,inputPos.y,0));