I currently use this:
using UnityEngine;
using System.Collections;
public class LoginRegister: MonoBehaviour
{
public string registerURL = "http://localhost/unity/register.php?"; //be sure to add a ? to your url
public string loginURL = "http://localhost/unity/login.php?";
public string Username = "";
public string Password = "";
void OnGUI()
{
Username = GUI.TextArea(new Rect(Screen.width / 2, 50, 100, 30), Username);
Password = GUI.TextArea(new Rect(Screen.width / 2, 150, 100, 30), Password);
if(GUI.Button(new Rect(Screen.width / 2, 250, 100, 30), "Sign Up/Log in"))
StartCoroutine(LoginPlayer(Username, Password));
}
IEnumerator RegisterPlayer(string name, string pass)
{
string post_url = registerURL + "name=" + WWW.EscapeURL(name) + "&pass=" + pass;
WWW hs_post = new WWW(post_url);
yield return hs_post;
if (hs_post.error != null)
{
print("There was an error posting the high score: " + hs_post.error);
}
}
IEnumerator LoginPlayer(string name, string pass)
{
string post_url = loginURL + "name=" + WWW.EscapeURL(name) + "&pass=" + pass;
WWW hs_post = new WWW(post_url);
yield return hs_post;
if (hs_post.error != null)
{
print("There was an error logging in: " + hs_post.error);
}
else
{
gameObject.guiText.text = hs_post.text;
}
}
}
That is the C# code for what I use to register/login to my MySQL.
Here is the login.php script:
<?php
$db = mysql_connect('localhost', 'root', '') or die('Could not connect: ' . mysql_error());
mysql_select_db('unity') or die('Could not select database');
$name = mysql_real_escape_string($_GET['name'], $db);
$pass = mysql_real_escape_string($_GET['pass'], $db);
$query = "SELECT * FROM `scores` WHERE `name`='$name' AND `pass`=sha1('$pass');";
$result = mysql_query($query);
if(mysql_num_rows($result) == 1)
while($row = mysql_fetch_assoc($result))
{
echo $row['name'] . " - " . $row['pass'] . " - " . $row['level'];
}
else
echo "not logged in";
?>
When I run the “LoginPlayer” coroutine, it displays: “username - password - score”, so EG: “callum - hash - 10”
Instead of doing that, how could I get it to set a variable inside of the script (C#), for instance:
public int playerScore;
//when SQL loads
playerScore = (what would I put here that goes inside of the "LoginPlayer" function?)
All help is greatly appreciated.