1,建立一個資料庫表,我們就以 test資料庫為例,資料庫裡建一個tb_User表, tb_User裡有User_Name , User_Pass 字段.
2,建立一個驗證使用者基本資訊的asp.net頁面,頁面名字是:Default.aspx
背景代碼如下:
普通浏覽複制代碼
using System;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
SqlConnection conn = new SqlConnection("Data Source=.\sqlexpress2008;Initial Catalog=test;Integrated Security=True");
protected void Page_Load(object sender, EventArgs e)
{
string name = Request.Form["name2"];
string pwd=Request.Form["pwd"];
conn.Open();
string sql = "select User_Name from tb_User where User_Name='" + name + "' and User_Pass='"+pwd+"'";
SqlCommand cmd = new SqlCommand(sql, conn);
if (cmd.ExecuteScalar() != null)
Response.Write("success");
else
Response.Write("faield");
conn.Close();
}
}
前台代碼不用管.
3,建立一個Unity 3D項目,建立一個Javascript檔案, 在Javascript檔案上編寫如下代碼:
var URL = "http://devmac.net/domino/rui/Default.aspx"; //送出的URL位址
static var return_data:String;
var strname:String;
var strpwd:String;
var cls:boolean=false;
var xx:Rect = Rect (20, 10, 150, 100);
function OnGUI() {
if(cls)
xx = GUI.Window(1, xx, windowjj, "Window");
GUI.Label(Rect(10,10,80,20),"UserName:");
GUI.Label(Rect(10,30,80,20),"Userpass:");
strname=GUI.TextField(Rect(90,10,100,20),strname);
strpwd=GUI.PasswordField(Rect(90,30,100,20),strpwd,"*"[0],25);
//same as above, but for password
if ( GUI.Button ( Rect (60, 60, 100, 20) , "Login" ) ){ //just a button
Login();
}
function windowjj(windowID : int){
GUI.Label(Rect(55,25,50,20),return_data);
if(GUI.Button(Rect(50,55,50,20),"Close")) cls=false;//
function Login() {
var form = new WWWForm(); //建立一個WWWForm對象。
form.AddField( "name2", strname );
form.AddField("pwd",strpwd);
var w:WWW=new WWW(URL,form);
yield w; //we wait for the form to check the PHP file, so our game dont just hang
cls=true;
if (w.error != null) {
//if there is an error, tell us
return_data=w.error;
} else {
return_data=w.text;
w.Dispose(); //clear our form in game
}
複制代碼
4. 此代碼放在主相機上。
本文轉蓬萊仙羽51CTO部落格,原文連結:http://blog.51cto.com/dingxiaowei/1366181,如需轉載請自行聯系原作者