unity3d登陆代码主要用到了unity内置的WWW类和WWWForm类,运用WWWForm.AddField(String fieldName, String value)方法通过post的表单提交方式把表单参数传递给服务器端的逻辑业务层。(我用的是JSP,在逻辑层上request.getParameter(fiedlName);就能得到AddField中传递的参数,接下来就是服务器的逻辑处理了。PHP貌似是$_POST[fieldName]吧,好久没写过php了,ASP.NET不着咋写...)

客户端的demo效果图:

imei是手机的唯一识别id,用imei表示可能不恰当...

客户端代码:

复制代码
 1 using UnityEngine;  2 using System.Collections;  3 using System.Text.RegularExpressions;  4  5 public class Client : MonoBehaviour  6 {  7  WWW www;  8  WWWForm form;  9 string url;  10  11 string username_label = "username:";  12 string username_input = "";  13  14 string password_label = "password:";  15 string password_input = "";  16  17 string password_label2 = "password2:";  18 string password_input2 = "";  19  20 string email_label = "email:";  21 string email_input = "";  22  23 string callback_label = "result:";  24 string callback_label2 = "";  25  26 void OnStart()  27  {  28  29  }  30  31 void OnGUI()  32  {  33 GUI.Label(new Rect(0, 0, 80, 20), username_label);  34 username_input = GUI.TextField(new Rect(80, 0, 100, 20), username_input);  35  36 GUI.Label(new Rect(0, 30, 80, 20), password_label);  37 password_input = GUI.TextField(new Rect(80, 30, 100, 20), password_input);  38  39 GUI.Label(new Rect(0, 60, 80, 20), password_label2);  40 password_input2 = GUI.TextField(new Rect(80, 60, 100, 20), password_input2);  41  42 GUI.Label(new Rect(0, 90, 80, 20), email_label);  43 email_input = GUI.TextField(new Rect(80, 90, 100, 20), email_input);  44  45 GUI.Label(new Rect(0, 160, 80, 20), callback_label);  46 callback_label2 = GUI.TextField(new Rect(50, 160, 160, 20), callback_label2);  47  48 if (GUI.Button(new Rect(0, 120, 100, 30), "Login"))  49  {  50 form = new WWWForm();  51 form.AddField("name", username_input);  52 form.AddField("password", password_input);  53 string url = "http://192.168.100.98:8084/ddt/UserLogin.jsp";  54 www = new WWW(url, form);  55  StartCoroutine(WaitForRequestUserNameLogin(www));  56  }  57  58 if (GUI.Button(new Rect(120, 120, 100, 30), "Register"))  59  {  60 form = new WWWForm();  61 //form.AddField("id", "phone_id_str");  62 form.AddField("id", SystemInfo.deviceUniqueIdentifier);  63 form.AddField("name", username_input);  64 form.AddField("password", password_input);  65 form.AddField("retry_password", password_input2);  66 form.AddField("email", email_input);  67 url = "http://192.168.100.98:8084/ddt/registerUser.jsp";  68 www = new WWW(url, form);  69  StartCoroutine(WaitForRequestRegister(www));  70  }  71  72 if (GUI.Button(new Rect(240, 120, 100, 30), "non-reg to play"))  73  {  74 form = new WWWForm();  75 form.AddField("id", SystemInfo.deviceUniqueIdentifier);  76 //form.AddField("name", username_input);  77 //form.AddField("password", password_input);  78 //form.AddField("retry_password", password_input2);  79 //form.AddField("email", email_input);  80 url = "http://192.168.100.98:8084/ddt/NonRegPlay.jsp";  81 www = new WWW(url, form);  82  StartCoroutine(WaitForRequestPhoneIdLogin(www));  83  }  84  85 if (GUI.Button(new Rect(200, 0, 130, 20), "Check UserName"))  86  {  87 form = new WWWForm();  88 form.AddField("name", username_input);  89 Debug.Log("username_input...." + username_input);  90 url = "http://192.168.100.98:8084/ddt/CheckUserIsExist.jsp";  91 www = new WWW(url, form);  92  StartCoroutine(WaitForRequestCheck(www));  93  }  94  95 if (GUI.Button(new Rect(0, 200, 50, 30), "IMEI"))  96  {  97 callback_label2 = SystemInfo.deviceUniqueIdentifier;  98  }  99 100  } 101 102  IEnumerator WaitForRequestUserNameLogin(WWW www) 103  { 104 yield return www; 105 if (www.error != null) 106 Debug.Log("fail to request..." + www.error); 107 else 108  { 109 if (www.isDone) 110  { 111 string ex = @"<span>([\S\s\t]*?)</span>"; 112 Match m = Regex.Match(www.data, ex); 113 if (m.Success) 114  { 115 string result = m.Value; 116 result = result.Substring(result.IndexOf(">") + 1, result.LastIndexOf("<") - result.IndexOf(">") - 1).Trim(); 117 if (result == "success") 118  { 119 callback_label2 = "登录成功"; 120  } 121 else if (result == "empty") 122  { 123 callback_label2 = "用户名或密码为空"; 124  } 125 else if (result == "fail") 126  { 127 callback_label2 = "找不到指定用户"; 128  } 129 else 130  { 131 callback_label2 = "未知错误"; 132  } 133  } 134  } 135  } 136  } 137 138  IEnumerator WaitForRequestRegister(WWW www) 139  { 140 yield return www; 141 if (www.error != null) 142 Debug.Log("fail to request..." + www.error); 143 else 144  { 145 if (www.isDone) 146  { 147 string ex = @"<span>([\S\s\t]*?)</span>"; 148 Match m = Regex.Match(www.data, ex); 149 if (m.Success) 150  { 151 string result = m.Value; 152 result = result.Substring(result.IndexOf(">") + 1, result.LastIndexOf("<") - result.IndexOf(">") - 1).Trim(); 153 if (result == "success") 154  { 155 callback_label2 = "注册成功"; 156  } 157 else if (result == "empty") 158  { 159 callback_label2 = "用户名或密码为空"; 160  } 161 else if (result == "equals") 162  { 163 callback_label2 = "两次输入密码不一致"; 164  } 165 else if (result == "fail") 166  { 167 callback_label2 = "更新数据库失败"; 168  } 169 else 170  { 171 callback_label2 = "未知错误"; 172  } 173  } 174  } 175  } 176 177  } 178 179  IEnumerator WaitForRequestCheck(WWW www) 180  { 181 yield return www; 182 if (www.error != null) 183 Debug.Log("fail to request..." + www.error); 184 else 185  { 186 if (www.isDone) 187  { 188 Debug.Log("data-->" + www.data); 189 string ex = @"<span>([\S\s\t]*?)</span>"; 190 Match m = Regex.Match(www.data, ex); 191 if (m.Success) 192  { 193 string result = m.Value; 194 result = result.Substring(result.IndexOf(">") + 1, result.LastIndexOf("<") - result.IndexOf(">") - 1).Trim(); 195 if (result == "empty") 196  { 197 callback_label2 = "用户名为空"; 198  } 199 else if (result == "nothing") 200  { 201 callback_label2 = "用户名不存在,可以注册"; 202  } 203 else if (result == "exist") 204  { 205 callback_label2 = "用户名已存在"; 206  } 207 else 208  { 209 callback_label2 = "未知错误"; 210  } 211  } 212  } 213  } 214  } 215 216  IEnumerator WaitForRequestPhoneIdLogin(WWW www) 217  { 218 yield return www; 219 if (www.error != null) 220 Debug.Log("fail to request..." + www.error); 221 else 222  { 223 if (www.isDone) 224  { 225 string ex = @"<span>([\S\s\t]*?)</span>"; 226 Match m = Regex.Match(www.data, ex); 227 if (m.Success) 228  { 229 string result = m.Value; 230 result = result.Substring(result.IndexOf(">") + 1, result.LastIndexOf("<") - result.IndexOf(">") - 1).Trim(); 231 if (result == "ok") 232  { 233 callback_label2 = "手机ID登录成功"; 234  } 235 else if (result == "error") 236  { 237 callback_label2 = "手机ID登录成功"; 238  } 239 else 240  { 241 callback_label2 = "未知错误"; 242  } 243  } 244  } 245  } 246  } 247 248 249 }
复制代码

服务器端注册逻辑:

复制代码
 1 <%@ page language="java" import="ddt.*" %>  2 <jsp:useBean id="user" scope="session" class="ddt.UserBean" />  3 <%@page contentType="text/html" pageEncoding="UTF-8"%>  4 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  5  "http://www.w3.org/TR/html4/loose.dtd">  6 <html>  7 <head>  8 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  9 <title>JSP Page</title> 10 </head> 11 <body> 12 <% 13 String id = request.getParameter("id"); 14 String username = request.getParameter("name"); 15 String password = request.getParameter("password"); 16 String retry_password = request.getParameter("retry_password"); 17 String email = request.getParameter("email"); 18  user.processRegisterUserRequest(id, username, password, retry_password, email, request, response); 19 %> 20 </body> 21 </html>
复制代码

user.processRegisterUserRequest方法是注册就是处理了。登录等其他功能也是采用同样的业务处理流程,不再详述了。