php unity 3d,[转载]Unity 3D同PHP数据交互案例

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)

{

echo

"npe";//昵称或密码不能为空。

} else {

if

($unityHash != $phpHash){

echo

"HASH code is diferent from your game, you

infidel.";

}

else {

$SQL

= "SELECT * FROM scores WHERE name = '" . $nick .

"'";

$result_id

= @mysql_query($SQL) or die("DATABASE ERROR!");

$total

= mysql_num_rows($result_id);

if($total)

{

$datas

= @mysql_fetch_array($result_id);

if(!strcmp($pass,

$datas["password"])) {

//echo

"LOGADO - PASSWORD CORRECT";

echo

"success";

}

else {

echo

"npw"; //昵称或密码错误

}

}

else {

echo

"nfn";//没此用户

}

}

}

// Close mySQL

Connection

mysql_close();

?>

4,新建一个Unity

3D项目,命名为myGame;新建一个Javascript文件,命名为phpUnity(点击Project面板上的Create按钮,点击Javascript,产生一个新的javascript,按F2为其命名为phpUnity).

在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()

{

if(yyyskin){

GUI.skin

= yyyskin;

GUI.Label(

Rect (10, 10, 80, 20),

"姓名:"); //text with your nick

GUI.Label

( Rect (10, 30, 80, 20),

"密码:"

);

formNick

= GUI.TextField ( Rect (90, 10, 100, 20), formNick ); //here you

will insert the new value to variable formNick

formPassword

= GUI.PasswordField ( Rect (90, 30, 100, 20), formPassword ,"*"[0],

25);

//same as above, but for

password

if

( GUI.Button ( Rect (10, 60, 100, 20) ,

"登录"

) ){ //just a button

Login();

}

GUI.TextArea(

textrect, formText );

}

}

function Login()

{

var

form = new WWWForm();

//创建一个WWWForm对象。

form.AddField(

"myform_hash", hash ); //add your hash code to the field

myform_hash, check that this variable name is the same as in PHP

file

form.AddField(

"myform_nick", formNick );

form.AddField(

"myform_pass", formPassword );

var

w = WWW(URL, form); //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 {

var

return_data=w.data;

if(return_data=="success"){

Application.Externaleval_r("window.location.href='game.php?username="+formNick+"'");

}else

if(return_data=="npe"){

print("Test

ok");

formText

= "用户名或密码不能为空!"; //here we return the data our PHP told

us

}else

if(return_data=="nfn"){

formText="用户名不存在!";

}else

if(return_data=="npw"){

formText="用户名或密码不正确!";

}

w.Dispose();

//clear our form in game

}

formNick

= ""; //just clean our variables

formPassword

= "";

}

5.此代码放在主相机内的游戏对象上。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值