设为首页 加入收藏

TOP

Unity UnityWebRequest实现与后端的交互(一)
2019-09-17 18:23:58 】 浏览:36
Tags:Unity UnityWebRequest 实现 后端的 交互

一般我们与后端对接的时候会用到UnityWebRequest
这里简单使用这个与后端进行交互
这个是总类

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
using UnityEngine.Networking;

public enum EOPERATION
{
LOGIN = 0,//登录
REGISTER,//注册
COLLEGELIST, //学院
MAJORLIST, //专业
CLASSLIST,//班级
EXISTMAIL,//邮箱重复验证
EXISTNUMBER, //学号重复验证
GETPASSWORD,//忘记密码
ADDSCORE,//添加成绩
DownLoad,
}
public class CallBackUser
{
public bool success;
public string msg;
public User obj;
}
public class WebWork : MonoBehaviour
{
Dictionary> _handers = new Dictionary>();
private string filepath;
bool isStartDownload;
UnityWebRequest request;
//根据协议号获取地址后缀
Dictionary mURLs = new Dictionary{
{ EOPERATION.LOGIN,"webapi/login" },
{ EOPERATION.REGISTER,"webapi/register"},
{ EOPERATION.COLLEGELIST,"user/college/list_combo"},
{ EOPERATION.MAJORLIST,"user/major/list_combo"},
{ EOPERATION.CLASSLIST,"user/class/list_combo"},
{ EOPERATION.EXISTMAIL,"webapi/existemail"},
{ EOPERATION.EXISTNUMBER,"webapi/existnumber"},
{EOPERATION.GETPASSWORD, "webapi/forget_pass"},
{EOPERATION.ADDSCORE,"webapi/add_score"},
{EOPERATION.DownLoad,"" }
};

private string ipAddress = "http://192.168.40.153:8000/";

AccountHander accountHander = new AccountHander();

public object JsonConvert { get; private set; }

//在这里注册消息返回后分发处理
public void Init()
{
accountHander.RegisterMsg(_handers);
DontDestroyOnLoad(this);
}
/// 
/// 传输数据
/// 
/// 
/// 
public void SendPost(EOPERATION op, Dictionary dic)
{
//根据协议号获取完整路径
string url = ipAddress + mURLs[op];
StartCoroutine(Post(url, dic, op));
}

/// 
/// 获取数据
/// 
/// 
public void SendGet(EOPERATION op, string name = "")
{
string url = ipAddress + mURLs[op] + "/" + name;
StartCoroutine(Get(url, op, name));
}
/// 
/// 获取下载进度
/// 
/// 
public float GetProgress()
{
if (request == null || !isStartDownload)
return 0;
return request.downloadProgress;
}

private IEnumerator Get(string url, EOPERATION op, string name)
{

if (!string.IsNullOrEmpty(url))
{
using (request = UnityWebRequest.Get(url))
{
isStartDownload = true;
//设置超时 链接超时返回 且isNetworkError为true
request.timeout = 30;
yield return request.SendWebRequest();
isStartDownload = false;
//结果回传给具体实现
if (request.isHttpError || request.isNetworkError)
{
Debug.Log(request.error);
}
else
{
_handersop;
}
};
}
}
//private WWW http;
private IEnumerator Post(string url, Dictionary dic, EOPERATION op)
{
if (!string.IsNullOrEmpty(url))
{
WWWForm form = new WWWForm();

foreach (var item in dic)
{
form.AddField(item.Key, item.Value);
}
using (request = UnityWebRequest.Post(url, form))
{
yield return request.SendWebRequest();
//结果回传给具体实现
if (request.isHttpError || request.isNetworkError)
{
Debug.Log(request.error);
}
else
{
_handersop;
}
}
}
}
}
工具类

using System.IO;

public class FileTool
{
/// 
/// 创建文件
/// 
public static void CreateFile(string filePath,byte[]bytes)
{
using (FileStream fs = new FileStream(filePath,FileMode.Create,FileAccess.Write))
{
fs.Write(bytes, 0, bytes.Length);

}
}

消息返回处理类 这只是一个分类

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;

public class AccountHander
{

public void RegisterMsg(Di

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇记netmvc中Html.BeginForm的一个.. 下一篇C# 快速高效率复制对象另一种方式..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目