Search Unity

Help with GuiLayout.Textfields and select all

Discussion in 'Scripting' started by Hanging Bunny, Nov 8, 2010.

  1. Hanging Bunny

    Hanging Bunny

    Joined:
    Nov 1, 2009
    Posts:
    5
    Hi guys,
    Right now I'm working on a store inventory UI and have a section where I list the quantity as a GUILayout.TextField so the player can type the amount of Item X they wish to purchase.

    I am wondering how to force a select all command on the textfield when the player clicks on it once. I know there is GUISettings.doubleClickSelectsWord but that doesn't quite work for what I need. Users should only have to click once (the more a user has to do the greater chance of error or frustration)

    GUISettings.tripleClickSelectsLine would be awesome to use if it could be changed to single click.

    Could anyone help point me in the right direction? I'm primarily an artist but am trying to learn Unity Javascript (slowly but I'm trying).

    Thanks in advance!
     
    Last edited: Nov 10, 2010
  2. Hanging Bunny

    Hanging Bunny

    Joined:
    Nov 1, 2009
    Posts:
    5
    Well I knew this was an unusual request and since I haven't seen a clear solution on the forums I decided to share mine with everyone.

    First start by declaring a control name for the TextField. My text fields are inside a for loop so I give each field a unique name by adding an unique int after it.
    Code (csharp):
    1. GUI.SetNextControlName ("quant"+i);
    2. priceString = GUILayout.TextField(priceString,3);
    3. i++;
    This was documented in the script reference so was not an issue to do.

    The second step is not mentioned on the forum posts I could find.
    Code (csharp):
    1. if(Input.GetMouseButtonUp(0) || Input.GetKeyUp (KeyCode.Return) || Input.GetKeyUp (KeyCode.KeypadEnter)){
    2.                 var lastKeyboardControl : int = -1;
    3.                 var kbdCtrlId : int = GUIUtility.keyboardControl;
    4.                 if(kbdCtrlId != lastKeyboardControl) {
    5.                     // check to see if the focused control is this text area...
    6.                     focusedControl = GUI.GetNameOfFocusedControl();
    7.                     if (itemList != null){
    8.                         firstLetter = focusedControl[0];
    9.                         if(firstLetter == "q"[0]) {
    10.                             // It is!  Now, get the editor state and tweak it.
    11.                             var t : TextEditor = GUIUtility.GetStateObject(typeof(TextEditor), kbdCtrlId) as TextEditor;
    12.                             t.SelectAll();
    13.                             lastKeyboardControl = kbdCtrlId;
    14.                             }
    15.                         }
    16.                     }
    17.                 }
    Hidden away in the Wikki HERE
    I didn't want to add more to the build (it is already getting bigger than I would like to see for a Web game), so I took a look into the C# and saw we can call/ create a TextEditor type. This is not in the script reference, and not being a trained programmer I was unaware it existed.
    I wrapped some of the code from the GUIExtension in a check for a mouse click or return key ect. You don't necessarily need to do this. I also have a check to my HashTable (itemList) these lines also you won't need.

    Hopefully this will help everyone who is stuck in the same situation I was!!!!
     
  3. laurie

    laurie

    Joined:
    Aug 31, 2009
    Posts:
    638
    Nifty, but be aware that since the TextEditor class is undocumented, it isn't guaranteed to be available (or to work the same way) in future versions of Unity. Relying on undocumented features is always risky...