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=http://www.mamicode.com/w.error;
- } else {
- return_data=http://www.mamicode.com/w.text;
- w.Dispose(); //clear our form in game
- }
- }
复制代码
4. 此代码放在主相机上。本文出自【狗刨学习网】
5.最后在网上搜一个crossdomain.xml文件,自己写也行,把它放在挂有Asp.net网站的根目录下,这样就大功告成了.
6,导入到IPhone手机里也成功访问到数据库. |