一般我们与后端对接的时候会用到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
{
_handers;
}
};
}
}
//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
{
_handers;
}
}
}
}
}
工具类

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(dictionary> handers)
{
handers.add(eoperation.login, onrsplogin);
handers.add(eoperation.register, onrspregister);
handers.add(eoperation.download, onrspdownload);
}

private void onrsplogin(string name,downloadhandler data)
{
//用json转化为类内部数据
jsonconvert.deserializeobject(data.text);

}
private void onrspregister(string name,downloadhandler data)
{

}

private void onrspdownload(string name,downloadhandler data)
{
//data.data二进制的文件 视频 图片的信息
filetool.createfile(name, data.data);
}

}

public void registermsg(dictionary> handers)
{

}
如果想添加一个新的就在主类init里注册 然后新类写一个注册方法就行了 这样会自动根据枚举转到相应的处理函数
然后name的话只是为了区分下载文件起码要改名吧 不然不知道名字 如果只是传数据可以无视name不调用
来用一个demo使用下

更多unity2018的功能介绍请到paws3d爪爪学院查找。