using System.Collections;
using UnityEngine;
using UnityEngine.Networking;
///
/// 网络请求测试
///
public class ChinarWebRequest : MonoBehaviour
{
void Start()
{
StartCoroutine(SendRequest());
}
///
/// 开启一个协程,发送请求
///
///
IEnumerator SendRequest()
{
Uri uri = new Uri("http://www.baidu.com"); //Uri 是 System 命名空间下的一个类,注意引用该命名空间
UnityWebRequest uwr = new UnityWebRequest(uri); //创建UnityWebRequest对象
uwr.timeout = 5;
yield return uwr.SendWebRequest(); //等待返回请求的信息
if (uwr.isHttpError || uwr.isNetworkError) //如果其 请求失败,或是 网络错误
{
Debug.LogError(uwr.error); //打印错误原因
}
else //请求成功
{
Debug.Log("请求成功");
}
}
}
Get 方法为创建一个http为传入url的 UnityWebReqest 对象
using System;
using System.Collections;
using UnityEngine;
using UnityEngine.Networking;
///
/// 网络请求测试
///
public class ChinarWebRequest : MonoBehaviour
{
void Start()
{
StartCoroutine(SendRequest());
}
///
/// 开启一个协程,发送请求
///
///
IEnumerator SendRequest()
{
UnityWebRequest uwr = UnityWebRequest.Get("http://www.baidu.com"); //创建UnityWebRequest对象
yield return uwr.SendWebRequest(); //等待返回请求的信息
if (uwr.isHttpError || uwr.isNetworkError) //如果其 请求失败,或是 网络错误
{
Debug.Log(uwr.error); //打印错误原因
}
else //请求成功
{
Debug.Log("Get:请求成功");
Debug.Log(uwr.downloadedBytes);
}
}
}
Post方法将一个表上传到远程的服务器,一般来说我们登陆某个网站的时候会用到这个方法,我们的账号密码会以一个表单的形式传过去
using System.Collections;
using UnityEngine;
using UnityEngine.Networking;
///
/// 网络请求测试
///
public class ChinarWebRequest : MonoBehaviour
{
void Start()
{
StartCoroutine(Post());
}
///
/// 开启一个协程,发送请求
///
///
IEnumerator Post()
{
WWWForm form = new WWWForm();
//键值对
form.AddField("key", "value");
form.AddField("name", "Chinar");
//请求链接,并将form对象发送到远程服务器
UnityWebRequest webRequest = UnityWebRequest.Post("http://www.baidu.com", form);
yield return webRequest.SendWebRequest();
if (webRequest.isHttpError || webRequest.isNetworkError)
{
Debug.Log(webRequest.error);
}
else
{
Debug.Log("发送成功");
}
}
}
Put方法将数据发送到远程的服务器
using System.Collections;
using UnityEngine;
using UnityEngine.Networking;
///
/// 网络请求测试
///
public class ChinarWebRequest : MonoBehaviour
{
void Start()
{
StartCoroutine(Upload());
}
///
/// 开启协程
///
///
IEnumerator Upload()
{
byte[] myData = System.Text.Encoding.UTF8.GetBytes("Chinar的测试数据");
using (UnityWebRequest uwr = UnityWebRequest.Put("http://www.baidu.com", myData))
{
yield return uwr.SendWebRequest();
if (uwr.isNetworkError || uwr.isHttpError)
{
Debug.Log(uwr.error);
}
else
{
Debug.Log("上传成功!");
}
}
}
}
Head方法与Get方法用法一致,都是传入一个Url
///
/// 开启一个协程,发送请求
///
///
IEnumerator SendRequest1()
{
UnityWebRequest uwr = UnityWebRequest.Head("http://www.chinar.xin/chinarweb/WebRequest/Get/00-效果.mp4"); //创建UnityWebRequest对象
yield return uwr.SendWebRequest(); //等待返回请求的信息
if (uwr.isHttpError || uwr.isNetworkError) //如果其 请求失败,或是 网络错误
{
Debug.Log(uwr.error); //打印错误原因
}
else //请求成功
{
Debug.Log("Head:请求成功");
}
}
GetResponseHeader方法可以用来获取请求文件的长度 传入参数 “Content-Length” 字符串,表示获取文件内容长度。
///
/// 开启一个协程,发送请求
///
///
IEnumerator SendRequest1()
{
UnityWebRequest uwr = UnityWebRequest.Head("http://www.chinar.xin/chinarweb/WebRequest/Get/00-效果.mp4"); //创建UnityWebRequest对象
yield return uwr.SendWebRequest(); //等待返回请求的信息
if (uwr.isHttpError || uwr.isNetworkError) //如果其 请求失败,或是 网络错误
{
Debug.Log(uwr.error); //打印错误原因
}
else //请求成功
{
long totalLength = long.Parse(huwr.GetResponseHeader("Content-Length")); //首先拿到文件的全部长度
Debug.Log("totalLength");//打印文件长度
}
}
using System;
using System.Collections;
using System.IO;
using UnityEditor;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
public class ChinarDownLoadFile : MonoBehaviour
{
public Slider ProgressBar; //进度条
public Text SliderValue; //滑动条值
private Button startBtn; //开始按钮
void Start()
{
//初始化进度条和文本框
ProgressBar.value = 0;
SliderValue.text = "0.0%";
startBtn = GameObject.Find("Start Button").GetComponent<Button>();
startBtn.onClick.AddListener(OnClickStartDownload);
}
///
/// 回调函数:开始下载
///
public void OnClickStartDownload()
{
StartCoroutine(DownloadFile());
}
///
/// 协程:下载文件
///
IEnumerator DownloadFile()
{
UnityWebRequest uwr = UnityWebRequest.Get("http://www.linxinfa.test.mp4.mp4"); //创建UnityWebRequest对象,将Url传入
uwr.SendWebRequest(); //开始请求
if (uwr.isNetworkError || uwr.isHttpError) //如果出错
{
Debug.Log(uwr.error); //输出 错误信息
}
else
{
while (!uwr.isDone) //只要下载没有完成,一直执行此循环
{
ProgressBar.value = uwr.downloadProgress; //展示下载进度
SliderValue.text = Math.Floor(uwr.downloadProgress * 100) + "%";
yield return 0;
}
if (uwr.isDone) //如果下载完成了
{
print("完成");
ProgressBar.value = 1; //改变Slider的值
SliderValue.text = 100 + "%";
}
byte[] results = uwr.downloadHandler.data;
// 注意真机上要用Application.persistentDataPath
CreateFile(Application.streamingAssetsPath + "/MP4/test.mp4", results, uwr.downloadHandler.data.Length);
AssetDatabase.Refresh(); //刷新一下
}
}
///
/// 这是一个创建文件的方法
///
/// 保存文件的路径
/// 文件的字节数组
/// 数据长度
void CreateFile(string path, byte[] bytes, int length)
{
Stream sw;
FileInfo file = new FileInfo(path);
if (!file.Exists)
{
sw = file.Create();
}
else
{
return;
}
sw.Write(bytes, 0, length);
sw.Close();
sw.Dispose();
}
}
using System;
using System.Collections;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
public class ChinarBreakpointRenewal : MonoBehaviour
{
private bool _isStop; //是否暂停
public Slider ProgressBar; //进度条
public Text SliderValue; //滑动条值
private Button startBtn; //开始按钮
private Button pauseBtn; //暂停按钮
public string Url = "http://www.linxinfa.test.mp4";
///
/// 初始化UI界面及给按钮绑定方法
///
void Start()
{
//初始化进度条和文本框
ProgressBar.value = 0;
SliderValue.text = "0.0%";
startBtn = GameObject.Find("Start Button").GetComponent<Button>();
startBtn.onClick.AddListener(OnClickStartDownload);
pauseBtn = GameObject.Find("Pause Button").GetComponent<Button>();
pauseBtn.onClick.AddListener(OnClickStop);
}
///
/// 回调函数:开始下载
///
public void OnClickStartDownload()
{
// 注意真机上要用Application.persistentDataPath
StartCoroutine(DownloadFile(Url, Application.streamingAssetsPath + "/MP4/test.mp4", CallBack));
}
///
/// 协程:下载文件
///
/// 请求的Web地址
/// 文件保存路径
/// 下载完成的回调函数
///
IEnumerator DownloadFile(string url, string filePath, Action callBack)
{
UnityWebRequest huwr = UnityWebRequest.Head(url); //Head方法可以获取到文件的全部长度
yield return huwr.SendWebRequest();
if (huwr.isNetworkError || huwr.isHttpError) //如果出错
{
Debug.Log(huwr.error); //输出 错误信息
}
else
{
long totalLength = long.Parse(huwr.GetResponseHeader("Content-Length")); //首先拿到文件的全部长度
string dirPath = Path.GetDirectoryName(filePath);
if (!Directory.Exists(dirPath)) //判断路径是否存在
{
Directory.CreateDirectory(dirPath);
}
//创建一个文件流,指定路径为filePath,模式为打开或创建,访问为写入
using (FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write))
{
long nowFileLength = fs.Length; //当前文件长度
Debug.Log(fs.Length);
if (nowFileLength < totalLength)
{
Debug.Log("还没下载完成");
fs.Seek(nowFileLength, SeekOrigin.Begin); //从头开始索引,长度为当前文件长度
UnityWebRequest uwr = UnityWebRequest.Get(url); //创建UnityWebRequest对象,将Url传入
uwr.SetRequestHeader("Range", "bytes=" + nowFileLength + "-" + totalLength);
uwr.SendWebRequest(); //开始请求
if (uwr.isNetworkError || uwr.isHttpError) //如果出错
{
Debug.Log(uwr.error); //输出 错误信息
}
else
{
long index = 0; //从该索引处继续下载
while (!uwr.isDone) //只要下载没有完成,一直执行此循环
{
if (_isStop) break;
yield return null;
byte[] data = uwr.downloadHandler.data;
if (data != null)
{
long length = data.Length - index;
fs.Write(data, (int) index, (int) length); //写入文件
index += length;
nowFileLength += length;
ProgressBar.value = (float) nowFileLength / totalLength;
SliderValue.text = Math.Floor((float) nowFileLength / totalLength * 100) + "%";
if (nowFileLength >= totalLength) //如果下载完成了
{
ProgressBar.value = 1; //改变Slider的值
SliderValue.text = 100 + "%";
callBack?.Invoke();
}
}
}
}
}
}
}
}
///
/// 下载完成后的回调函数
///
void CallBack()
{
Debug.Log("下载完成");
}
///
/// 暂停下载
///
public void OnClickStop()
{
if (_isStop)
{
pauseBtn.GetComponentInChildren<Text>().text = "暂停下载";
Debug.Log("继续下载");
_isStop = !_isStop;
OnClickStartDownload();
}
else
{
pauseBtn.GetComponentInChildren<Text>().text = "继续下载";
Debug.Log("暂停下载");
_isStop = !_isStop;
}
}
}