Thursday, July 26, 2012

Create a login screen in unity

This is nice base using player prefs to create a login screen for your game... useful if you want to create a multiplayer game.

var password : String = "My Password";
var playerName:String = "player name";
function Start () {

}

function Update () {

}

function OnGUI()
{

// these strings set player name to the string you enter.
    playerName =  GUI.TextField (Rect (300, 250, 200, 20), playerName, 25);

// this is the password field that will create the password.
    password = GUI.PasswordField (Rect (300, 300, 200, 20), password, "*"[0], 25);
    

// pressing the create button creates a new user.
    if(GUI.Button(Rect(300, 350, 100,20), "Create"))
    {
    PlayerPrefs.SetString(playerName, password);
    }


// this lets the user log in. if the string == the entered password then you can load the next screen.
    if(GUI.Button(Rect(420, 350,100,20), "login"))
    {
        if (password == PlayerPrefs.GetString(playerName))
        {
            print(playerName);
            print("login successful");
        }
        else
        {
            print(playerName);
            print("login not successful");
        }
    }
}

No comments:

Post a Comment