一、在JavaIDEA配置Javaweb的Tomcat
URL路径是java对接unity的关键
二、创建Servlet类,再配置好Web.Xml
public abstract class BaseServlet extends HttpServlet {
protected final ObjectMapper objectMapper = new ObjectMapper();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("UTF-8");
resp.setCharacterEncoding("UTF-8");
resp.setContentType("text/html;charset=UTF-8");
String methodName = req.getParameter("method");
if(!Tools.isNotNull(methodName)) methodName = "index";
try {
Method method = this.getClass().getDeclaredMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
if ("private|protected".contains(Modifier.toString(method.getModifiers()).trim())) {
method.setAccessible(true);
}
method.invoke(this, req, resp);
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
PrintWriter writer = resp.getWriter();
}
}
protected abstract void index(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException;
protected void returnJson(HttpServletResponse response, Object obj) throws IOException {
response.setContentType("application/json;charset=UTF-8");
PrintWriter writer = response.getWriter();
writer.write(objectMapper.writeValueAsString(obj));
writer.flush();
writer.close();
}
}
JsomMsg类处理传递的数据
public class JsonMsg implements Serializable {
private Boolean state;
private int code;
private String msg;
private Object data;
public Boolean getState() {
return state;
}
public void setState(Boolean state) {
this.state = state;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
}
简单的登陆类
private void doLogin(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
JsonMsg jsonMsg = new JsonMsg();
jsonMsg.setState(false);
//获取参数
String account = req.getParameter("account");
String password = req.getParameter("password");
System.out.println(account +":"+ password);
//===数据验证
//==验证账号和密码
if (!Tools.isNotNull(account)) {
jsonMsg.setMsg("请输入账号");
//把jsonMsg序列化为Json 字符串 通往输出流输出
returnJson(resp, jsonMsg);
return;
}
if (!Tools.isNotNull(password)) {
jsonMsg.setMsg("请输入密码");
//把jsonMsg序列化为Json 字符串 通往输出流输出
returnJson(resp, jsonMsg);
return;
}
//根据用户名 查询用户
EmployeesVo user = this.employeesService.getSelectByEmployees(account);
if (user == null) {
jsonMsg.setMsg("账号不存在");
returnJson(resp, jsonMsg);
return;
}
if (password.equals(user.getPassword())) {
jsonMsg.setMsg("登录成功");
jsonMsg.setState(true);
//把jsonMsg序列化为Json 字符串 通往输出流输出
returnJson(resp, jsonMsg);
} else {
jsonMsg.setMsg("登录失败");
//把jsonMsg序列化为Json 字符串 通往输出流输出
returnJson(resp, jsonMsg);
}
}
三、创建Unity项目简单搭个页面,建个C#脚本
这里验证码下期再写
servletLoginUrl和Tomcat的URL一样再加上配置的Web.Xml路径即可跳转页面
loginUrl是调用Servlet的登陆方法
public string servletLoginUrl = "http://127.0.0.1:8080/PensionSystem/LoginServlet?method=";
//登陆private string loginUrl = servletLoginUrl + "doLogin&";
//登陆按钮public void LoginButtonOnClick()
{
//向服务器传递的参数
string parameter = "";
parameter += "account=" + account.text + "&";
parameter += "password=" + password.text + "&";
parameter += "identityKey=" + identityText.text;
//开始传递
StartCoroutine(ServletJsp(loginUrl + parameter));
}
ServletJsp处理与接收到的数据,字符串处理用的是Json Net for Unity 插件
//访问Servlet服务器IEnumerator ServletJsp(string path)
{
UnityWebRequest request = new UnityWebRequest(path);
DownloadHandlerBuffer dH = new DownloadHandlerBuffer();
request.downloadHandler = dH;
yield return request.SendWebRequest();//读取数据
//如果发生错误,打印这个错误
if (request.error != null)
{
Debug.Log(request.error);
}
else
{
CharacterVo characterVo = JsonConvert.DeserializeObject<CharacterVo>
(request.downloadHandler.text);
if(characterVo.state.Equals("true")){
image.SetActive(true);
text.text = characterVo.msg;
}
else{
image.SetActive(true);
text.text = characterVo.msg;
}
}
}
到这了基本就可以和Unity互传数据了
URL路径是java对接unity的关键
二、创建Servlet类,再配置好Web.Xml
public abstract class BaseServlet extends HttpServlet {
protected final ObjectMapper objectMapper = new ObjectMapper();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("UTF-8");
resp.setCharacterEncoding("UTF-8");
resp.setContentType("text/html;charset=UTF-8");
String methodName = req.getParameter("method");
if(!Tools.isNotNull(methodName)) methodName = "index";
try {
Method method = this.getClass().getDeclaredMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
if ("private|protected".contains(Modifier.toString(method.getModifiers()).trim())) {
method.setAccessible(true);
}
method.invoke(this, req, resp);
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
PrintWriter writer = resp.getWriter();
}
}
protected abstract void index(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException;
protected void returnJson(HttpServletResponse response, Object obj) throws IOException {
response.setContentType("application/json;charset=UTF-8");
PrintWriter writer = response.getWriter();
writer.write(objectMapper.writeValueAsString(obj));
writer.flush();
writer.close();
}
}
JsomMsg类处理传递的数据
public class JsonMsg implements Serializable {
private Boolean state;
private int code;
private String msg;
private Object data;
public Boolean getState() {
return state;
}
public void setState(Boolean state) {
this.state = state;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
}
简单的登陆类
private void doLogin(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
JsonMsg jsonMsg = new JsonMsg();
jsonMsg.setState(false);
//获取参数
String account = req.getParameter("account");
String password = req.getParameter("password");
System.out.println(account +":"+ password);
//===数据验证
//==验证账号和密码
if (!Tools.isNotNull(account)) {
jsonMsg.setMsg("请输入账号");
//把jsonMsg序列化为Json 字符串 通往输出流输出
returnJson(resp, jsonMsg);
return;
}
if (!Tools.isNotNull(password)) {
jsonMsg.setMsg("请输入密码");
//把jsonMsg序列化为Json 字符串 通往输出流输出
returnJson(resp, jsonMsg);
return;
}
//根据用户名 查询用户
EmployeesVo user = this.employeesService.getSelectByEmployees(account);
if (user == null) {
jsonMsg.setMsg("账号不存在");
returnJson(resp, jsonMsg);
return;
}
if (password.equals(user.getPassword())) {
jsonMsg.setMsg("登录成功");
jsonMsg.setState(true);
//把jsonMsg序列化为Json 字符串 通往输出流输出
returnJson(resp, jsonMsg);
} else {
jsonMsg.setMsg("登录失败");
//把jsonMsg序列化为Json 字符串 通往输出流输出
returnJson(resp, jsonMsg);
}
}
三、创建Unity项目简单搭个页面,建个C#脚本
这里验证码下期再写
servletLoginUrl和Tomcat的URL一样再加上配置的Web.Xml路径即可跳转页面
loginUrl是调用Servlet的登陆方法
public string servletLoginUrl = "http://127.0.0.1:8080/PensionSystem/LoginServlet?method=";
//登陆private string loginUrl = servletLoginUrl + "doLogin&";
//登陆按钮public void LoginButtonOnClick()
{
//向服务器传递的参数
string parameter = "";
parameter += "account=" + account.text + "&";
parameter += "password=" + password.text + "&";
parameter += "identityKey=" + identityText.text;
//开始传递
StartCoroutine(ServletJsp(loginUrl + parameter));
}
ServletJsp处理与接收到的数据,字符串处理用的是Json Net for Unity 插件
//访问Servlet服务器IEnumerator ServletJsp(string path)
{
UnityWebRequest request = new UnityWebRequest(path);
DownloadHandlerBuffer dH = new DownloadHandlerBuffer();
request.downloadHandler = dH;
yield return request.SendWebRequest();//读取数据
//如果发生错误,打印这个错误
if (request.error != null)
{
Debug.Log(request.error);
}
else
{
CharacterVo characterVo = JsonConvert.DeserializeObject<CharacterVo>
(request.downloadHandler.text);
if(characterVo.state.Equals("true")){
image.SetActive(true);
text.text = characterVo.msg;
}
else{
image.SetActive(true);
text.text = characterVo.msg;
}
}
}
到这了基本就可以和Unity互传数据了