GUILayout.PasswordField() shows whole password when running on iOS before changing characters!?!

When I test my game's twitter login view in the editor, the GUILayout.PasswordField() works as expected hiding every character immediately and revealing nothing. However when I run it on my iPhone 4 and it has to have the characters input from the iOS keyboard it behaves much differently. It actually shows the ENTIRE PASSWORD until the user hits the Done button confirming they are finished entering their password where it finally turns them all into the asterisk character I gave it. There's no way I can use it like this as it would potentially reveal many of my user's twitter passwords to onlookers. Any ideas on how to fix this would be INDESCRIBABLY HELPFUL! I'll add my OnGUI() implementation here.

static var twitterUser : String = "";
static var twitterPassword : String = "";

function OnGUI()
{
    if (rootView.highRes)
    {
        GUI.skin = rootView.highResGUISkin;
    }
    else
    {
        GUI.skin = rootView.lowResGUISkin;
    }
    if (selected) // this turns true when the root view calls one of this view's EnterFrom functions
    {
        startPos = Mathf.Lerp(startPos, endPos, Time.deltaTime * rootView.guiDamp);
        GUILayout.BeginArea(Rect(startPos, 0, Screen.width, Screen.height));
        GUILayout.BeginVertical();// begin open centering junk
        GUILayout.FlexibleSpace();
        GUILayout.BeginHorizontal();
        GUILayout.FlexibleSpace();// end open centering junk
        GUILayout.BeginVertical("box");
        GUILayout.Box("TWITTER ACCOUNT");
        GUILayout.BeginHorizontal();
        GUILayout.BeginVertical();
        GUILayout.Label("USERNAME", "twitterLabel");
        GUILayout.Label("PASSWORD", "twitterLabel");
        GUILayout.EndVertical();
        GUILayout.BeginVertical();
        twitterUser = GUILayout.TextField(twitterUser, 15);
        twitterPassword = GUILayout.PasswordField(twitterPassword, "*"[0]);
        GUILayout.EndVertical();
        GUILayout.EndHorizontal();
        GUILayout.EndVertical();
        GUILayout.FlexibleSpace();// begin close centering junk
        GUILayout.EndHorizontal();
        GUILayout.FlexibleSpace();
        GUILayout.EndVertical();// end close centering junk
        GUILayout.EndArea();
    }
}

Filed a bug report. Hopefully they can find a fix for me soon so I don't end up using a hack work around.

Bug is confirmed and with a developer for fixing. (June 29th 2011)

3 Answers

3

Until the bug is fixed, you may be able to get around it by making the text invisible by changing the transparency, and then drawing a series of “*” over top in the regular color of it equal to the length of the text. e.g. (untested, but should give the gist)

public static string MyPasswordField( string password )
{
  Color oldColor = GUI.contentColor;                  // save it
  GUI.contentColor = new Color( 0,0,0,0 );            // transparent (0 alpha)
  password = GUILayout.TextField( password );         // regular field, invis text
  GUI.contentColor = oldColor;                        // restore color
  Rect r = GUILayoutUtility.GetLastRect();            // wherever that was, find out
  GUI.Label( r, new string( '*', password.Length ) ); // draw * over top
  return password;
}

If the GUI.contentColor doesn’t work as expected you could just make a special “invisible text textfield” GUIStyle and pass that instead (same basic idea, just a different way).

Just tested, it works flawlessy. Thanks a lot, I really needed this! But unfortunately, the original bug is still here...

Almost a year has passed and this bug is still there…

The problem with the previous hack from Molix is that the virtual keyboard is still showing the password on my iPad…

To get around that, you may use the hack from Molix and hide the keyboard text field:

TouchScreenKeyboard.hideInput=true;

Or you may hide the GUI text when typing the password on the virtual keyboard:

var oldColor : Color = GUI.contentColor;
if (TouchScreenKeyboard.visible) {
	GUI.contentColor = new Color(0,0,0,0);
}
password = GUILayout.PasswordField(password, "*"[0]);
if (TouchScreenKeyboard.visible) {
	GUI.contentColor = oldColor;
}

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

This last solution is not perfect since the password is still briefly displayed when the keyboard is sliding…

I’ve found a workaround for this bug, using an invisible button and the TouchScreenKeyboard.Open() function.

See my post (the 4th one) for an example here: http://forum.unity3d.com/threads/65337-GUI.PasswordField-issue