1,创建一个数据库表,使用MySql数据库.创建代码如下:
CREATE TABLE `scores` ( `id` INT( 10 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`name` VARCHAR( 30 ) NOT NULL ,
`password` VARCHAR( 50 ) NOT NULL
) ENGINE = innodb;
2,向数据库表中插入内容:
INSERT INTO `scores` ( `id` , `name` , `password` )
VALUES (
NULL , 'haiweisky', MD5( 'aaaaaa' )
);
3,创建一个验证用户基本信息的PHP页面,页面名字是:check_scores.php
//
$host = "localhost"; //主机名
$user = "root"; //用户名
$password = ""; //密码
$dbname = "unity3dTest"; //数据库名称
mysql_connect($host, $user, $password) or die("Cant connect into database");
mysql_select_db($dbname)or die("Cant connect into database");
// =============================================================================
// PROTECT AGAINST SQL INJECTION and CONVERT PASSWORD INTO MD5 formats
function anti_injection_login_senha($sql, $formUse = true)
{
$sql = preg_replace("/(from|select|insert|delete|where|drop table|show tables|,|'|#|*|--|\\)/i","",$sql);
$sql = trim($sql);
$sql = strip_tags($sql);
if(!$formUse || !get_magic_quotes_gpc())
$sql = addslashes($sql);
$sql = md5(trim($sql));
return $sql;
}
// THIS ONE IS JUST FOR THE NICKNAME PROTECTION AGAINST SQL INJECTION
function anti_injection_login($sql, $formUse = true)
{
$sql = preg_replace("/(from|select|insert|delete|where|drop table|show tables|,|'|#|*|--|\\)/i","",$sql);
$sql = trim($sql);
$sql = strip_tags($sql);
if(!$formUse || !get_magic_quotes_gpc())
$sql = addslashes($sql);
return $sql;
}
// =============================================================================
$unityHash = anti_injection_login($_POST["myform_hash"]);
$phpHash = "hashcode"; // same code in here as in your Unity game
$nick = anti_injection_login($_POST["myform_nick"]); //I use that function to protect against SQL injection
$pass = anti_injection_login_senha($_POST["myform_pass"]);
if(!$nick || !$pass) {
} else {
}
// Close mySQL Connection
mysql_close();
?>
4,新建一个Unity 3D项目,命名为myGame;新建一个Javascript文件,命名为phpUnity(点击Project面板上的Create按钮,点击Javascript,产生一个新的javascript,按F2为其命名为phpUnity).
private var formNick = ""; //登录名
private var formPassword = ""; //密码
var formText = ""; //根据判断显示信息
var yyyskin:GUISkin;
var URL = "http://localhost/unity_test/check_scores.php"; //提交的URL地址
var hash = "hashcode"; //change your secret code, and remember to change into the PHP file too
private var textrect = Rect (10, 150, 500, 100); //just make a GUI object rectangle
function OnGUI() {
//same as above, but for password
}
function Login() {
5.