查看: 1212|回复: 1
打印 上一主题 下一主题

Unity 3D同PHP数据交互案例_代码

[复制链接]

2508

主题

2

听众

3万

积分

资深设计师

Rank: 7Rank: 7Rank: 7

纳金币
32806
精华
12

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

跳转到指定楼层
楼主
发表于 2012-6-29 14:23:59 |只看该作者 |倒序浏览
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 = ***e)

     
   

     
    {

     
   

     
    $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 = ***e)

     
   

     
    {

     
   

     
    $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"]);

     
   

     
    /*

     
   

     
    you can also use this

     
   

     
    $nick = $_POST["myform:_nick"];

     
   

     
    $pass = $_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("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. 此代码放在主相机内的游戏对象上。

原文地址:http://unity3d8.com/content/unit ... 2%E6%A1%88%E4%BE%8B
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

2317

主题

54

听众

2万

积分

资深设计师

Rank: 7Rank: 7Rank: 7

纳金币
20645
精华
62

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

沙发
发表于 2012-12-30 04:19:00 |只看该作者
学习了,虽然还是有难度,谢谢楼主的用心
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

手机版|纳金网 ( 闽ICP备2021016425号-2/3

GMT+8, 2024-9-20 14:32 , Processed in 0.128908 second(s), 29 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部