一般我們與後端對接的時候會用到UnityWebRequest這裡簡單使用這個與後端進行交互這個是總類 using UnityEngine;using System.Collections;using System.Collections.Generic;using System;using Unit ...
一般我們與後端對接的時候會用到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(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爪爪學院查找。