Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

GUI.PasswordField issue

Discussion in 'iOS and tvOS' started by tony92875, Oct 29, 2010.

  1. tony92875

    tony92875

    Joined:
    Apr 16, 2009
    Posts:
    7
    Maybe I'm missing something here. Is the GUI.PasswordField supposed to ignore the password character you supply while the iOS keyboard is open? I'm not using calls to iPhoneKeyboard. This is when the user touches a passwordfield and the keyboard pops up for input. While the user is typing the text entered appears just above the keyboard in the keyboard supplied text box and it appears in the password field as clear text. Once the user is done typing and clicks "done" or the keyboards "enter" key, the keyboard slides away and the password field replaces the text with the proper password character.

    So am I missing a setting?
     
  2. franktinsley

    franktinsley

    Joined:
    Jul 1, 2010
    Posts:
    130
  3. evax

    evax

    Joined:
    Jul 6, 2010
    Posts:
    23
    I'm having the exact same issue. too
     
  4. bournifle

    bournifle

    Joined:
    Jan 13, 2010
    Posts:
    31
    Hi, here is a workaround for the GUILayout.PasswordField bug.

    It solves the following problems:
    1) Password appears as clear text on the screen on iOS (bug 441113 reported).
    2) iOS shows some suggestions and autocorrects while typing the password (!)

    I've tried some workarounds (see http://answers.unity3d.com/questions/55448/guilayoutpasswordfield-shows-whole-password-when-r.html), but without any success:
    1) Workaround solutions that put a Label on top of a PasswordField still autocorrect...
    2) Workaround solutions that test TouchScreenKeyboard.visible to hide the password briefly display it when the virtual keyboard is sliding...

    So my solution (inspired from the first workaround) is to place an invisible button on top of a GUI.Label. The button will manually open a virtual keyboard with the right parameters. Then we fill the GUI.Label with *s.

    (This UnityScript code is for Unity 3.5, replace TouchScreenKeyboard by iPhoneKeyboard if you are using Unity 3.4)

    Code (csharp):
    1.  
    2. private var password  : String = "";
    3. private var keyboard : TouchScreenKeyboard = null;
    4. private var oldColor : Color;
    5. private var oldBackgroundColor : Color;
    6. private var r : Rect;
    7.  
    8. function OnGUI() {
    9.  
    10.     ...
    11.  
    12.     if (Application.platform == RuntimePlatform.IPhonePlayer) {
    13.         // make the button invisible by setting contentColor and backgroundColor to (0,0,0,0)
    14.         oldColor = GUI.contentColor;
    15.         GUI.contentColor = new Color(0,0,0,0);
    16.         oldBackgroundColor = GUI.backgroundColor;
    17.         GUI.backgroundColor = new Color(0,0,0,0);
    18.         // here is the invisible button that opens a keyboard on ios
    19.         if (GUILayout.Button("", GUILayout.Width(250))) {
    20.             keyboard = TouchScreenKeyboard.Open(password, TouchScreenKeyboardType.Default, false, false, true);
    21.         }
    22.         // update password content
    23.         if (keyboard) {
    24.             password = keyboard.text;
    25.         }
    26.         // restore the GUI colors
    27.         GUI.contentColor = oldColor;
    28.         GUI.backgroundColor = oldBackgroundColor;
    29.         // get Rect of last GUI (the button)
    30.         r = GUILayoutUtility.GetLastRect();
    31.         // draw a Label with * instead of password
    32.         GUI.Label(r, (new String("*"[0], password.Length)));
    33.     } else {
    34.         // use the classic password field when running in the editor or on another platform
    35.         password = GUILayout.PasswordField(password, "*"[0], GUILayout.Width(250));
    36.     }
    37. }
    38.  
    If you have more than one text or password field in your GUI, you will have to add a check in the code, else the keyboard.text content will be duplicated between the fields... Here is an example for a login/password GUI:

    Code (csharp):
    1.  
    2. private var login : String = "";
    3. private var password  : String = "";
    4. private var keyboard : TouchScreenKeyboard = null;
    5. private var oldColor : Color;
    6. private var oldBackgroundColor : Color;
    7. private var r : Rect;
    8. private var fieldSelected : int = -1;
    9.  
    10. function OnGUI() {
    11.     ...
    12.  
    13.     if (Application.platform == RuntimePlatform.IPhonePlayer) {
    14.         // make the button invisible by setting contentColor and backgroundColor to (0,0,0,0)
    15.         oldColor = GUI.contentColor;
    16.         GUI.contentColor = new Color(0,0,0,0);
    17.         oldBackgroundColor = GUI.backgroundColor;
    18.         GUI.backgroundColor = new Color(0,0,0,0);
    19.         // here is the invisible button that opens a keyboard on ios
    20.         if (GUILayout.Button("", GUILayout.Width(250))) {
    21.             keyboard = TouchScreenKeyboard.Open(login, TouchScreenKeyboardType.Default, false, false, false);
    22.             fieldSelected = 0;
    23.         }
    24.         // update login content
    25.         if (keyboard  (fieldSelected == 0)) {
    26.             login = keyboard.text;
    27.         }
    28.         // restore the GUI colors
    29.         GUI.contentColor = oldColor;
    30.         GUI.backgroundColor = oldBackgroundColor;
    31.         // get Rect of last GUI (the button)
    32.         r = GUILayoutUtility.GetLastRect();
    33.         // draw a Label with the login
    34.         GUI.Label(r, login);
    35.     } else {
    36.         // use the classic text field when running in the editor or on another platform
    37.         login = GUILayout.TextField(login, GUILayout.Width(250));
    38.     }
    39.  
    40.     ...
    41.  
    42.     if (Application.platform == RuntimePlatform.IPhonePlayer) {
    43.         // make the button invisible by setting contentColor and backgroundColor to (0,0,0,0)
    44.         oldColor = GUI.contentColor;
    45.         GUI.contentColor = new Color(0,0,0,0);
    46.         oldBackgroundColor = GUI.backgroundColor;
    47.         GUI.backgroundColor = new Color(0,0,0,0);
    48.         // here is the invisible button that opens a keyboard on ios
    49.         if (GUILayout.Button("", GUILayout.Width(250))) {
    50.             keyboard = TouchScreenKeyboard.Open(password, TouchScreenKeyboardType.Default, false, false, true);
    51.             fieldSelected = 1;
    52.         }
    53.         // update password content
    54.         if (keyboard  (fieldSelected == 1)) {
    55.             password = keyboard.text;
    56.         }
    57.         // restore the GUI colors
    58.         GUI.contentColor = oldColor;
    59.         GUI.backgroundColor = oldBackgroundColor;
    60.         // get Rect of last GUI (the button)
    61.         r = GUILayoutUtility.GetLastRect();
    62.         // draw a Label with * instead of password
    63.         GUI.Label(r, (new String("*"[0], password.Length)));
    64.     } else {
    65.         // use the classic password field when running in the editor or on another platform
    66.         password = GUILayout.PasswordField(password, "*"[0], GUILayout.Width(250));
    67.     }
    68. }
    69.  
     
  5. Fattie

    Fattie

    Joined:
    Jul 5, 2012
    Posts:
    476