x


How to prevend GUI.Textfield allow Multiline Strings?

Im using a Gui.Textfield as an input Textfield.

In the reference they write it is a single Line Textfield. But it still allows a "\n" as input.

Do i miss something, or do i have to use a Regular Expression or String.replace("\n", "") to prevent the String to be Multilined?

Or is there even a better way?

var stringToEdit = "Hello World";

function OnGUI () {
// Make a text field that modifies stringToEdit.
stringToEdit = GUI.TextField (Rect (10, 10, 200, 20), stringToEdit, 25);
}

http://unity3d.com/support/documentation/ScriptReference/GUI.TextField.html

more ▼

asked Jun 22 '10 at 07:23 PM

Case23 gravatar image

Case23
441 17 21 30

(comments are locked)
10|3000 characters needed characters left

4 answers: sort voted first

I got this problem too. But in my case,I set a custom style to TextField. I don't know why it allows multiple lines, my custom style only changed Textures.

more ▼

answered Jul 02 '10 at 04:23 AM

forest_lin gravatar image

forest_lin
1

(comments are locked)
10|3000 characters needed characters left

In my little test, the only way I could get the textfield to have multiple lines is if I set the start string to have a newline in it and don't escape it (either through \\n or using the @ syntax).

I tested it with typing in \n and it didn't create a newline or anything. I'm assuming that it escapes special characters like \.

more ▼

answered Jun 22 '10 at 07:30 PM

Tetrad gravatar image

Tetrad
7.2k 27 37 89

In my case, my Textfield starts with an empty string. And it still allows multilines. Right now i solve the issue with [value = value.Replace("n", "").Replace("r", "");].

Jun 22 '10 at 07:43 PM Case23
(comments are locked)
10|3000 characters needed characters left

The only thing I have found is to use replace string as Case23 said

string compareText = inputString;

inputString = GUILayout.TextField(inputString, 20, randomGuiStyle);

if (compareText != inputString)
{
    // This is the only way I have found to remove new line & carriage return....
    inputString = inputString.Replace("\n", "").Replace("\r", "");
}

There is an old post talking about it here http://forum.unity3d.com/viewtopic.php?p=257819 AngryAnt has a good idea(Event.current), but I have not had any luck with it.

more ▼

answered Aug 02 '10 at 10:00 AM

Usul gravatar image

Usul
487 3 5 12

That'd be because the logic is wrong in it. It'd need to be something more like (in pseudocode).:. if((type == keyup || type == keydown) && (keycode == KeyCode.Return || keycode == KeyCode.KeypadEnter))

Aug 02 '10 at 10:10 AM Mike 3

I've tried quite a few combo's and could not get it to work at all :(

Aug 02 '10 at 11:16 AM Usul

Sorry to dig up this old question but I'm having the same problem and it's ruining a lot in my game :( Anyone have any ideas?

Aug 12 '12 at 02:26 AM SomeGuy22

@SomeGuy22 try using TextArea with the same arguments (maxLength + style)? I've found it to be single-lined for no reason and wondering if other people's having the problem too.

Nov 23 '12 at 03:50 AM caroparo

@caroparo I got this to work a while ago by using String.Replace("n", "");

That replaces enters with nothing.

Nov 23 '12 at 04:34 PM SomeGuy22
(comments are locked)
10|3000 characters needed characters left

If you want more control over the touch keyboard, don't use a TextField at all. On mobile devices you always have to use the native keyboard which displays it'a own input field above your Unity application. So it's way easier to replace the TextField with a Button and simply open up the keyboard with the desired settings:

TouchScreenKeyboard

more ▼

answered Apr 25 at 06:37 AM

Bunny83 gravatar image

Bunny83
45.5k 11 49 207

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3695
x151

asked: Jun 22 '10 at 07:23 PM

Seen: 3497 times

Last Updated: Apr 25 at 06:37 AM