Hello and welcome to the first installment
We are going to add a credential system to an existing game, and make it work seamlessly with Unity.
There are 3 things you will have to do:
- Create a c9.io account ( for PHP server )
- Alternativly, you can host your PHP file here
- Create a freesqldatabase.com account ( for database )
- Create a new scene in Unity
With the credentials given to you via email from freesqldatabase.com, sign in to phpmyadmin.co. Create a table name Users with these columns
- Username
- Password
You can create a demo user for testing in this database
Now, navigate to c9.io. Create a workspace called credmanager ( for the purpose of this tut) , and in this workspace, create an crossdomail (crossdomain.xml ). This is the only way Unity can use the PHP file. Place the crossdomain file at the root of your project.
<?xml version="1.0"?>
<cross-domain-policy>
<allow-access-from domain="*"/>
</cross-domain-policy>
Now create a php file called Login ( Login.php ). You can copy this code into it.
<?php
// Database Things =========================================================
//These values can be found in the email.
$host = "####.freesqldatabase.com"; //<--put your host here
$user = "###"; //<-- put username here
$password = "####"; //<--put your password here
$dbname = "###"; //<-- put your database name here
mysql_connect($host, $user, $password) or die("Cant connect into database");
mysql_select_db($dbname)or die("Cant connect into database");
// =============================================================================
$Act = $_GET["Act"];// what is action, Login or Register?
$nick = $_GET["User"];
$pass = $_GET["Pass"];
$Email = $_GET["Email"];
if($Act == "Login"){
if(!$nick || !$pass) {
echo "Login or password cant be empty.";
} else {
$SQL = "SELECT * FROM Users WHERE Username = '" . $nick . "'";
$result_id = @mysql_query($SQL) or die("DB ERROR");
$total = mysql_num_rows($result_id);
if($total) {
$datas = @mysql_fetch_array($result_id);
if(!strcmp($pass, $datas["Password"])) {
echo "Correct";
} else {
echo "Wrong";
}
} else {
echo "No User";
}
}
}
if($Act == "Register"){
$checkuser = mysql_query("SELECT Username FROM Users WHERE Username='$nick'");
$username_exist = mysql_num_rows($checkuser);
if($username_exist > 0)
{
echo "TAKEN";// Username is taken
unset($nick);
exit();
}else{
$query = "INSERT INTO Users (Username, Password,Email) VALUES('$nick', '$pass', '$Email')";
mysql_query($query) or die("ERROR");
mysql_close();
echo "Registered";
}
}
// Close mySQL Connection
mysql_close();
?>
Now click run at the top of the window. it should open up a new tab with a url, that looks something similar to this(username will be your username however…):
http://credmanager.username.c9.io/Login.php
Now the fun part. We will manually modify the URL to register a user like as follows:
The webpage should now say Registered. But refresh the page again and it will say TAKEN. This is because your user is now entered the database! You can check this by going back to your phpmyadmin.co table. Now you have registered a user manually, but how do I login?
Simply change the url to this:
http://credmanager.username.c9.io/Login.php?User=Peter&Pass=Stewie123&Act=Login
The webpage should now say correct! IT works!
Now, most of the work is done. Now we can open up unity.
Create a scene, and make it the first scene indexed in Build Settings.
Create a JScript name Login( Login.js )
Here is my script. You may need to edit this to suit your needs.
private var formNick = ""; //this is the field where the player will put the name to login
private var formPassword = ""; //this is his password
private var RformNick = ""; //this is the field where the player will put the name to login
private var RformPassword = "";
private var Remail = "";
private var TRformPassword = "";
public var guiSkin : GUISkin;
var LoadOut : System.Boolean;
var LoadOutText = "";
var formText = ""; //this field is where the messages sent by PHP script will be in
var URL = "http://credmanager.username.c9.io/Login.php"; //change for your URL
// var hash = "812ca28212be5705ad52010bccc9ea3f"; //change your secret code, and remember to change into the PHP file too
var DoLogin : System.Boolean;
private var textrect = Rect (10, 150, 500, 500); //just make a GUI object rectangle
function Start (){
PlayerPrefs.DeleteAll();
}
function OnGUI() {
if(LoadOut){
GUI.skin = guiSkin;
GUI.Box (new Rect(Screen.width/2 - 150, Screen.height/2 - 30, 300, 60), LoadOutText);
}else{
GUI.Window (0, new Rect (Screen.width/2 - 250, Screen.height/2 - 150, 500, 400),Login, "");
}
}
function Login (id : int){
if(DoLogin){
GUILayout.BeginVertical();
GUI.skin = guiSkin;
GUILayout.Box (/* new Rect (Screen.width/2 - 250, Screen.height/2 - 150, 500, 400),*/"Login");
GUILayout.BeginHorizontal();
GUILayout.FlexibleSpace();
GUILayout.Label( "Username:" ); //text with your nick
formNick = GUILayout.TextField ( formNick , 15, GUILayout.Width(345), GUILayout.Height(35));
GUILayout.EndHorizontal();
GUILayout.Space(10);
GUILayout.BeginHorizontal();
GUILayout.FlexibleSpace();
GUILayout.Label( "Password:" );
formPassword = GUILayout.TextField (formPassword , 15, GUILayout.Width(345), GUILayout.Height(35) ); //same as above, but for password
GUILayout.EndHorizontal();
GUILayout.Space(10);
GUILayout.BeginHorizontal();
GUILayout.Space(5);
if ( GUILayout.Button ("Login" ) ){ //just a button
Action("Login");
}
if ( GUILayout.Button ( "Sign Up" ) ){ //just a button
DoLogin = false;
}
GUILayout.EndHorizontal();
GUILayout.EndVertical();
}else{
GUI.skin = guiSkin;
GUILayout.Box ("Register");
GUILayout.BeginHorizontal();
GUILayout.FlexibleSpace();
GUILayout.Label("Username:" );
RformNick = GUILayout.TextField (RformNick, 15,GUILayout.Width(300) ,GUILayout.Height(35) );
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.FlexibleSpace();
GUILayout.Label("Password:" );
RformPassword = GUILayout.TextField (RformPassword , 15,GUILayout.Width(300) , GUILayout.Height(35) );
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.FlexibleSpace();
GUILayout.Label("Password:" );
TRformPassword = GUILayout.TextField (TRformPassword , 15,GUILayout.Width(300) , GUILayout.Height(35) );
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.FlexibleSpace();
GUILayout.Label("Email:" );
Remail = GUILayout.TextField (Remail, 75,GUILayout.Width(300) , GUILayout.Height(35) );
GUILayout.EndHorizontal();
if ( GUILayout.Button ("Finish" ) ){ //just a button
Action("Register");
}
if ( GUILayout.Button ("Back" ) ){ //just a button
DoLogin = true;
}
}
//GUI.TextArea( textrect, formText );
}
function Action(Act : String) {
var form = new WWWForm(); //here you create a new form connection
//add your hash code to the field myform_hash, check that this variable name is the same as in PHP file
var tempURL : String;
if(Act =="Login"){
tempURL = URL + "?User="+formNick+"&Pass="+formPassword+"&Act="+Act;
}else{
tempURL = URL + "?User="+RformNick+"&Pass="+RformPassword+"&Act="+Act+"&Email="+Remail;
}
var w = WWW(tempURL); //here we create a var called 'w' and we sync with our URL and the form
yield w; //we wait for the form to check the PHP file, so our game dont just hang
if (w.error != null) {
//print(w.error); //if there is an error, tell us
} else {
if(w.data == " Correct"){
print("Logging In...");
LoadOut = true;
LoadOutText = "Signing In...";
yield WaitForSeconds(5);
PlayerPrefs.SetString("RegUser",formNick);
Application.LoadLevel(1);
}
if(w.data == " Wrong"){
LoadOut = true;
LoadOutText = "Wrong Password";
yield WaitForSeconds(3);
LoadOut = false;
}
if(w.data == " No User"){
LoadOut = true;
LoadOutText = "No Registered User Found";
yield WaitForSeconds(3);
LoadOut = false;
}
if(w.data == " ILLEGAL REQUEST"){
LoadOut = true;
LoadOutText = "Server Error";
yield WaitForSeconds(3);
LoadOut = false;
}
if(w.data == " Registered"){
print("Account Created. Logging In.");
LoadOut = true;
LoadOutText = "Creating Account Logging In...";
PlayerPrefs.SetString("RegUser",RformNick);
yield WaitForSeconds(5);
Application.LoadLevel(1);
}
if(w.data == " ERROR"){
LoadOut = true;
LoadOutText = "Login Error. Restarting.";
yield WaitForSeconds(3);
Application.LoadLevel(0);
}
print(w.data);
// formText = w.data; //here we return the data our PHP told us
w.Dispose(); //clear our form in game
}
formNick = ""; //just clean our variables
formPassword = "";
}
You will need to edit out your URL at the top. Set the script to the camera, and click in Unity. Type Peter for Username and Stewie123 for password. Hopefully it loads. If not, check your URL and c9.io to make sure it is still running. You might need a dedicated computer running c9.io if you want to test over a couple of days. I do not suggest c9.io as your full game handler. I just use it for testing since I have a budget of 0.
Now that you have finished the tutorial, you can edit the scripts to suit your needs.
If you want to , you can donate to me at bit.ly/NLGDonate:sunglasses:
Thanks
- NitroLabGames ( Peter A. )