Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

How do I add a Login Password to my game?

Discussion in 'Multiplayer' started by WarpZone, Apr 15, 2008.

  1. WarpZone

    WarpZone

    Joined:
    Oct 29, 2007
    Posts:
    326
    I assume it can be done with Unity, since MMOs are referenced in the description of this forum. But how?

    I guess for starters, I'd need to set up some kinda database... I've tinkered with PHP and MySQL before, but does Unity have commands to issue these types of querries and accept a response?

    And how would the bigger picture work once validation is possible? Do I read and write persistant player stats to the database arbitrarily during runtime? Or would it be better to just grab the entire table when the player joins and work with it client-side until the player logs off? (Or would doing this much stuff client-side be an open invitation to cheaters?)

    Thanks in advance for any helpful replies.
     
  2. TJB

    TJB

    Joined:
    Mar 20, 2007
    Posts:
    146
    Unity can interface with MySQL no problem. You could probably read through this for starters:
    http://unity3d.com/support/documentation/ScriptReference/WWWForm.html

    I'm going to need to do something like this soon myself, so it's been floating around in the back of my mind. For login I'm planning to use a SQL database so it's easier to create accounts from a web page. While i'll probably use PlayerPrefs to save player stats, position etc since the game server and website server will be on separate machines.

    Info on playerprefs can be found here:
    http://unity3d.com/support/documentation/ScriptReference/PlayerPrefs.html
     
  3. WarpZone

    WarpZone

    Joined:
    Oct 29, 2007
    Posts:
    326
    Thanks for the info, TJB. :D

    Are you free to discuss the gritty implementation details?

    I would like to think that once I get Unity talking to MySQL, I could save some of those PlayerPrefs to the server somehow. Not neccessarily everything, but important gameplay-related stuff like acquired stats and the player's position in the world. That needs to be server-side.

    I may need some kinda middleware app running on the server to keep track of potential database changes throughout the day, then at night it would roll the changes over into the database with one big table call instead of a bunch of little ones. I've heard about that trick being used before somewhere. I think maybe KOL does it? It's supposed to be a lot faster or something.

    Anywho, how is the basic MySQL call done in Unity?

    Edit: Ah, WWWForm. Interesting. I'll have to see if I can get this working tomorrow.

    AddBinaryData looks especially intriguing. Does this mean I could dump some arbitrary block of data such as a mesh or a texture into a bytearray, send it to the server for long-term storage, then retrieve it later for use in the Unity app? That's a freaking powerful technique if it works like that!
     
  4. TJB

    TJB

    Joined:
    Mar 20, 2007
    Posts:
    146
    Made a couple scripts for unity that might help you. They're not complete and haven't been tested yet, but i'm pretty sure everything in them is right. (although there might be a slightly better method, i'm far from a pro at this stuff)

    This is for verifying a user account exists on login:
    Code (csharp):
    1. var phpscript = "http://www.host.com/validatelogin.php";   
    2. private var nametext = "";
    3. private var passtext = "";
    4. function OnGUI () {
    5.     nametext = GUI.TextField (Rect (10, 10, 130, 20), nametext);
    6.     passtext = GUI.TextField (Rect (10, 40, 130, 20), passtext);
    7.     if (GUI.Button (Rect (150,10,90,20), "Login")) {
    8.         senddata ();
    9.     }
    10. }
    11.  
    12. function senddata () {
    13.     var form =  new WWWForm();
    14.     form.AddField("Name", nametext);
    15.     form.AddField("Pass", passtext);
    16.     var w = WWW(phpscript, form);
    17.     yield w;
    18.     if (w.data == "good") {
    19.         //log them on, when that's done load playerpref data instantiate char etc
    20.     }
    21. }
    This would be attached to the object controlled by the player. It would save the position of the player out to player prefs and the health to a SQL database (using a php script that you'd have to write) :
    Code (csharp):
    1.  
    2. var phpscript = "http://www.host.com/outputdata.php";
    3.  
    4. function OnPlayerDisconnected() {
    5.     var info = transform.GetComponent("StatsScript");
    6.     PlayerPrefs.SetFloat(info.playername+"xpos",transform.position.x);
    7.     PlayerPrefs.SetFloat(info.playername+"ypos",transform.position.y);
    8.     PlayerPrefs.SetFloat(info.playername+"zpos",transform.position.z);
    9.     var form =  new WWWForm();
    10.     form.AddField("Name", info.playername);
    11.     form.AddField("Pass", info.password);
    12.     form.AddField("Health", info.health);
    13.     var w = WWW(phpscript, form);
    14.     yield w;
    15.     Network.Destroy(gameObject);
    16. }

    edit: oh, and i think this might be interesting to you concerning the importing meshes thing you mentioned:
    http://www.unifycommunity.com/wiki/index.php?title=MeshSerializer
     
  5. redshift_one

    redshift_one

    Joined:
    Apr 29, 2008
    Posts:
    2
    That info is really useful TJB, I am helping a friend with something similar.

    There is one thing that I am not totally sure about. The URL you refer to as $phpscript - I understand it needs to have a validation script in it, but how would this return a value to work? Does it somehow need to return the value "good" in your code?
     
  6. TJB

    TJB

    Joined:
    Mar 20, 2007
    Posts:
    146
    I don't have a script that actually gets data from a mysql database right now, but this might help you get started:
    Code (csharp):
    1. <?php
    2. $name = $_REQUEST["Name"];
    3. $pass = $_REQUEST["Pass"];
    4. if ($name == "t"  $pass == "pass") {
    5. echo "good";
    6. }
    7. ?>
    It's fairly self explanatory and simple, but it should get you started. The code goes in a text file with the php ending and is put on your server. The webserver processes whatever is sent to it using this code and sends out "good" if the password and username is right.
    Sorry I can't show how to actually hook it up to the mysql database.




    Oh! Also, if you want to hide the password that's typed in unity (so it outputs stars instead of the password) this might be helpful:
    Code (csharp):
    1. var passtext = "";
    2. function OnGUI () {
    3.     var letternum = 0;
    4.     var letternumaft = 0;
    5.     var dispass = "";
    6.     var temppass = "";
    7.     for (var element in passtext) {
    8.         dispass += "*";
    9.         letternum++;
    10.     }
    11.     dispass = GUI.TextField (Rect (25,135,250,30), dispass);
    12.     for (var element in dispass) {
    13.         letternumaft++;
    14.         var strn = element.ToString();
    15.         if (strn != "*") passtext += element;
    16.     }
    17.     if (letternum > letternumaft) {
    18.         letternum = 1;
    19.         for (var element in passtext) {
    20.             if (letternum <= letternumaft) {
    21.                 var chara = element.ToString();
    22.                 temppass+=chara;
    23.                 letternum++;
    24.             }
    25.         }
    26.         passtext = temppass;
    27.     }
    28. }
    It's not perfect... backspace will always remove the last character (if you move cursor halfway through the characters it'll still remove the last)... but i don't think that's a big deal... most people don't go back and change the 3rd of 10 characters in there password after they've typed the whole thing...
     
  7. boomaster_legacy

    boomaster_legacy

    Joined:
    Feb 24, 2008
    Posts:
    262
    how could i just simply add a login screen and everything so i could just allow people to log in and play something, after registering. I also need to know how to make a registering thing. it would be AMAZING if you could just put that all together, even the scene switch thing, and simply give it to me via the forum in a package. but that would be asking for a lot. I'D BE SO HAPPY IF YOU COULD! and if you don't want to, which I would think any person really would, you REALLY don't have to. or you could just add a multiplayer function to the game i make. but again, you really don't have to. thanks... :D
     
  8. jeffcraighead

    jeffcraighead

    Joined:
    Nov 15, 2006
    Posts:
    740
    But half of the fun is figuring it out yourself. :D At least for me.
     
  9. redshift_one

    redshift_one

    Joined:
    Apr 29, 2008
    Posts:
    2
    That's great TJB, thanks for all your help. It looks at first glance like that would work straight away.

    My main problem at the moment is having to try and help a friend out with this, without having a mac or the unity program!
     
  10. boomaster_legacy

    boomaster_legacy

    Joined:
    Feb 24, 2008
    Posts:
    262
    it does sound pretty challenging to do that. and it is also half the fun to figure it out yourself. the thing is i've tried that. i can't understand it. check my signature!
     
  11. jeffcraighead

    jeffcraighead

    Joined:
    Nov 15, 2006
    Posts:
    740
    What part are you having a problem with? Also I assume you want the login to be in the game not on the web page, correct? The first thing you need is a GUI that includes 2 labels, 2 text fields, and a Login button. The text fields and labels are for the user name and password. With these you can set a username and password variable. When you hit login it should send these to a web address using the WWWForm and WWW classes. See http://unity3d.com/support/documentation/ScriptReference/WWWForm.html

    The hard part is setting up the http and sql servers. If you have your own machine to set up things on, I suggest the Apache web server and MySQL sql server.
     
  12. boomaster_legacy

    boomaster_legacy

    Joined:
    Feb 24, 2008
    Posts:
    262
    is there a tutorial i could look at?
    anyway, thanks for all the help.
     
  13. jeffcraighead

    jeffcraighead

    Joined:
    Nov 15, 2006
    Posts:
    740
    I don't think I've seen a tutorial specifically on login pages. I think you'll just have to go through the documentation. I've attached a file that I've created that controls the majority of my GUI system. It has options for various backgrounds, options menus, a multiplayer host/join menu, etc. You should be able to use this as an example to create what you need. I've divided most of the code into independent functions. So the OptionsMenu is a function, MainMenu is a function, etc. Keep asking questions, I'll be happy to give you a hand, but it helps if they're more specific than "How do I do <X>." :D
     

    Attached Files:

  14. TJB

    TJB

    Joined:
    Mar 20, 2007
    Posts:
    146
    There's some php and mysql database video tutorials on 3dbuzz for free. Just have to create a account:
    http://3dbuzz.com/vbforum/sv_dl_tr.php

    I haven't gone through them myself but they should help you get started.
     
  15. boomaster_legacy

    boomaster_legacy

    Joined:
    Feb 24, 2008
    Posts:
    262
    thanks guys, big help. should i be using scripts like Server, Button, Sender, or Password Field? or maybe that's just in the package. your always tons of help, and thanks again. :)

    p.s. what do i attach the .js to?
     
  16. jeffcraighead

    jeffcraighead

    Joined:
    Nov 15, 2006
    Posts:
    740
    Jake, just attach the .js to any game object in the scene. I've got an empty object I call GameManager on which I call DontDestroyOnLoad and that I attach all management scripts to. This includes the GUI script I posted as well as one I'm working on called MultiplayerManager, SimManager, etc. Each of these scripts handles a different component of my application.

    For the login screen, I'd put all the related code in some kind of network manager or multiplayer manager. If you want you could put the GUI code for the login in the same script or as I do, put all the GUI code in one file. That is really just your preference. A single function should do it. Something like this modified version of the highscores example in the WWWForm docs:

    Code (csharp):
    1.  
    2. function DoLogin(name : String, password : String) : boolean {
    3.     // Create a form object for sending high score data to the server
    4.     var form = new WWWForm();
    5.     form.AddField( "game", "MyGameName" );
    6.     form.AddField( "playerName", name );
    7.     form.AddField( "password", password );
    8.  
    9.     // Create a download object
    10.     var download = new WWW( login_url, form );
    11.  
    12.     // Wait until the download is done
    13.     yield download;
    14.    
    15.     if(download.error) {
    16.         print( "Error logging in: " + download.error );
    17.         return false;
    18.     }
    19.     else {
    20.         return true;
    21.     }
    22. }
    23.  
    Then the GUI code would look something like:
    Code (csharp):
    1.  
    2. var name = "";
    3. var pass = "";
    4. var netManager : <NetManagerScriptName>;
    5. var doLogin = true;
    6. var loggingin = false;
    7. function OnGUI(){
    8. if(doLogin){
    9.     GUILayout.BeginArea(Rect(<define your rect here>));
    10.         GUILayout.BeginHorizontal();
    11.             GUILayout.Label("User Name");
    12.             GUILayout.TextField(name);
    13.         GUILayout.EndHorizontal();
    14.         GUILayout.BeginHorizontal();
    15.             GUILayout.Label("Password");
    16.             GUILayout.TextField(pass);
    17.         GUILayout.EndHorizontal();
    18.         if(!loggingin  GUILayout.Button("Submit")){
    19.             loggingin=true;
    20.             doLogin=!netManager.DoLogin(name,pass);
    21.         }
    22.         else if(GUILayout.Button("Cancel Login")){
    23.             loggingin=false;
    24.             doLogin=true;
    25.         }
    26.     GUILayout.EndArea();
    27. }
    28. }
    29.  
    Now that is untested, I just whipped that up while writing this message, but that is the general idea.