概述
主要用到了unity内置的WWW类和WWWForm类,运用WWWForm.AddField(String fieldName, String value)方法通过post的表单提交方式把表单参数传递给服务器端的逻辑业务层。
客户端的demo效果图:
imei是手机的唯一识别id,用imei表示可能不恰当.
客户端代码:
using UnityEngine;
using System.Collections;
using System.Text.RegularExpressions;
public class Client : MonoBehaviour
{
WWW www;
WWWForm form;
string url;
string username_label = “username:”; string username_input = “”;
string password_label = “password:”; string password_input = “”;
string password_label = “password:”; string password_input = “”;
string email_label = “email:”; string email_input = “”;
string callback_label = “result:”; string callback_label = “”;
void OnStart()
{
}
void OnGUI()
{
GUI.Label(new Rect(,,,), username_label);
username_input = GUI.TextField(new Rect(,,,), username_input);
GUI.Label(new Rect(,,,), password_label);
password_input = GUI.TextField(new Rect(,,,), password_input);
GUI.Label(new Rect(,,,), password_label);
password_input = GUI.TextField(new Rect(,,,), password_input);
GUI.Label(new Rect(,,,), email_label);
email_input = GUI.TextField(new Rect(,,,), email_input);
GUI.Label(new Rect(,,,), callback_label);
callback_label = GUI.TextField(new Rect(,,,), callback_label);
if (GUI.Button(new Rect(,,,), “Login”)) {
form = new WWWForm();
form.AddField(“name”, username_input);
form.AddField(“password”, password_input);
string url = “http: //...:/ddt/UserLogin.jsp”;
www = new WWW(url, form);
StartCoroutine(WaitForRequestUserNameLogin(www));
}
if (GUI.Button(new Rect(,,,), “Register”)) {
form = new WWWForm();
form.AddField(“id”, “phone_id_str”);
form.AddField(“id”, SystemInfo.deviceUniqueIdentifier);
form.AddField(“name”, username_input);
form.AddField(“password”, password_input);
form.AddField(“retry_password”, password_input);
form.AddField(“email”, email_input);
url = “http: //...:/ddt/registerUser.jsp”;
www = new WWW(url, form);
StartCoroutine(WaitForRequestRegister(www));
}
if (GUI.Button(new Rect(,,,), “non - reg to play”)) {
form = new WWWForm();
form.AddField(“id”, SystemInfo
.deviceUniqueIdentifier);
form.AddField(“name”, username_input);
form.AddField(“password”, password_input);
orm.AddField(“retry_password”, password_input);
form.AddField(“email”, email_input);
url = “http: //...:/ddt/NonRegPlay.jsp”;
www = new WWW(url, form);
StartCoroutine(WaitForRequestPhoneIdLogin(www));
}
if (GUI.Button(new Rect(,,,), “Check UserName”)
) {
form = new WWWForm();
form.AddField(“name”, username_input);
Debug.Log(“username_input....” +username_input);
url = “http: //...:/ddt/CheckUserIsExist.jsp”;
www = new WWW(url, form);
StartCoroutine(WaitForRequestCheck(www));
}
if (GUI.Button(new Rect(,,,), “IMEI”)) {
callback_label = SystemInfo.deviceUniqueIdentifier;
}
}
IEnumerator WaitForRequestUserNameLogin(WWW www)
{
yield return www;
if (www.error != null) Debug.Log(“
fail to request...” +www.error); else {
if (www.isDone)
{
string ex = @“([
Sst]*?)”;
Match m = Regex.Match(www.data, ex);
if (m.Success)
{
string result = m.Value;
result = result.Substring(result.IndexOf(“ >”) + , result.LastIndexOf(“”) - ).Trim();
if (result == “success”) {
callback_label = “登录成功”;
} else if (
result == “empty”) {
callback_label = “用户名或密码为空”;
} else if (result == “fail”) {
callback_label = “找不到指定用户”;
} else {
callback_label = “未知错误”;
}
}
}
}
}
IEnumerator WaitForRequestRegister(WWW www)
{
yield return www;
if (www.error != null) Debug.Log(“
fail to request...” +www.error); else {
if (www.isDone)
{
string ex = @“([
Sst]*?)”;
Match m = Regex.Match(www.data, ex);
if (m.Success)
{
string result = m.Value;
result = result.Substring(result.IndexOf(“ >”) + , result.LastIndexOf(“”) - ).Trim();
if (result == “success”) {
callback_label = “注册成功”;
} else if (result == “empty”) {
callback_label = “用户名或密码为空”;
} else if (result == “equals”) {
callback_label = “两次输入密码不一致”;
} else if (result == “fail”) {
callback_label = “更新数据库失败”;
} else {
callback_label = “未知错误”;
}
}
}
}
}
IEnumerator WaitForRequestCheck(WWW www)
{
yield return www;
if (www.error != null) Debug.Log(“
fail to request...” +www.error); else {
if (www.isDone)
{
Debug.Log(“data-- >” +www.data);
string ex = @“([
Sst]*?)”;
Match m = Regex.Match(www.data, ex);
if (m.Success)
{
string result = m.Value;
result = result.Substring(result.IndexOf(“ >”) + , result.LastIndexOf(“”) - ).Trim();
if (result == “empty”) {
callback_label = “用户名为空”;
} else if (result == “nothing”) {
callback_label = “用户名不存在,可以注册”;
} else if (result == “exist”) {
callback_label = “用户名已存在”;
} else {
callback_label = “未知错误”;
}
}
}
}
}
IEnumerator WaitForRequestPhoneIdLogin(WWW www)
{
yield return www;
if (www.error != null) Debug.Log(“
fail to request...” +www.error); else {
if (www.isDone)
{
string ex = @“([
Sst]*?)”;
Match m = Regex.Match(www.data, ex);
if (m.Success)
{
string result = m.Value;
result = result.Substring(result.IndexOf(“ >”) + , result.LastIndexOf(“”) - ).Trim();
if (result == “ok”) {
callback_label = “手机ID登录成功”;
} else if (result == “error”) {
callback_label = “手机ID登录成功”;
} else {
callback_label = “未知错误”;
}
}
}
}
}
}
服务器端注册逻辑:
<% String id = request.getParameter("id"); String username = request.getParameter("name"); String password = request.getParameter("password"); String retry_password = request.getParameter("retry_password"); String email = request.getParameter("email"); user.processRegisterUserRequest(id, username, password, retry_password, email, request, response);
最后
以上就是迷你金针菇为你收集整理的Unity与注册登录服务器交互原理及code的全部内容,希望文章能够帮你解决Unity与注册登录服务器交互原理及code所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复