Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

GUI.TextField allows enter multi-line?

Discussion in 'Editor & General Support' started by Reizla, May 23, 2014.

  1. Reizla

    Reizla

    Joined:
    Nov 5, 2013
    Posts:
    136
    I'm using the following line in my code to allow the user to enter a profile-name:

    Code (csharp):
    1.  
    2. MainGame.MyProfileName = GUI.TextField (new Rect(Screen.width/2-125*myGUIScale,Screen.height/2, 250*myGUIScale, 20*myGUIScale), MainGame.MyProfileName, 16, myStyle);
    3.  
    When I look at the description of the GUI.Textfield, it tells me "Make a single-line text field where the user can edit a string.", but when I test the code in the Unity editor it allows me to use the ENTER key to make multiple lines, and when I display the MainGame.MyProfileName, it shows the ENTERs as well.

    Is there any way to avoid people from using the ENTER key when using GUI,TextField?
     
  2. Graham-Dunnett

    Graham-Dunnett

    Unity Technologies

    Joined:
    Jun 2, 2009
    Posts:
    4,287
    Search the string, and if it contains a newline character, remove it from the string.
     
  3. Reizla

    Reizla

    Joined:
    Nov 5, 2013
    Posts:
    136
    Yeah, I got that already, but is this function supposed to act like this? Also, what to search for '/n' or '/r'?
     
  4. EatUrDemon

    EatUrDemon

    Joined:
    May 19, 2014
    Posts:
    37
    Not sure. It hasn't been doing that for me.
     
  5. EatUrDemon

    EatUrDemon

    Joined:
    May 19, 2014
    Posts:
    37
    This is the code I'm using for it, and it works fine on my end.

    Code (csharp):
    1.  
    2. private void DisplayName() {
    3.         GUI.Label(new Rect(10, 10, 50, 25), "Name: ");
    4.         _toon.Name = GUI.TextField(new Rect(65, 10, 100, 25), _toon.Name);
    5.  
     
  6. dazoe

    dazoe

    Joined:
    Dec 3, 2012
    Posts:
    1
    Is this a bug? It's been happening to me while trying to write a KSP plugin. I have searched and searched and cannot find a proper solution. By which I mean using Event's Use or something like that. I feel I should not have to add extra variables to handle intercepting the return key, or extra method calls to trim the string, etc.