群里有人问UnityWebRequest的用法,因为本人之前用的也不多只用过Post请求获取服务端数据,就想着把相关的用法总结一下,懒得描述具体用法了,懂得人自然懂,哈哈
void Start()
{
//访问网站
WebRequestManager.Instance.Get("http://www.huanshj.com/", (a) => { Debug.LogError(a.isDone); });
//下载服务器视频文件
WebRequestManager.Instance.DownloadFile("http://www.huanshj.com/World/Videos/1.mp4", Application.dataPath + "/HSJExample/UnityWebRequest/aaa.mp4", (a) => { Debug.LogError(a.downloadProgress); });
//下载服务器图片文件
WebRequestManager.Instance.GetTexture("http://www.huanshj.com/Private/Wedding/Pictures/Xu001.jpg", (a) => { rawImage.texture = a; });
}
#region 模块信息
// **********************************************************************
// Copyright (C) 2020
// Please contact me if you have any questions
// File Name: WebRequestManager
// Author: 幻世界
// QQ: 853394528
// **********************************************************************
#endregion
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.Experimental.PlayerLoop;
using UnityEngine.Networking;
public class WebRequestManager : BaseManager
{
public void Get(string url, Action actionResult)
{
StartCoroutine(_Get(url, actionResult));
}
public void DownloadFile(string url, string downloadFilePathAndName, Action actionResult)
{
StartCoroutine(_DownloadFile(url, downloadFilePathAndName, actionResult));
}
public void GetTexture(string url, Action actionResult)
{
StartCoroutine(_GetTexture(url, actionResult));
}
public void GetAssetBundle(string url, Action actionResult)
{
StartCoroutine(_GetAssetBundle(url, actionResult));
}
public void GetAudioClip(string url, Action actionResult, AudioType audioType = AudioType.WAV)
{
StartCoroutine(_GetAudioClip(url, actionResult, audioType));
}
public void Post(string serverURL, WWWForm wWWForm, Action actionResult)
{
wWWForm.AddField("test","我是测试内容");
StartCoroutine(_Post(serverURL, wWWForm, actionResult));
}
///
/// 通过PUT方式将字节流传到服务器
///
/// 需要上传的字节流
/// 处理返回结果的委托
///
public void UploadByPut(string url, byte[] contentBytes, Action actionResult)
{
StartCoroutine(_UploadByPut(url, contentBytes, actionResult, ""));
}
///
/// GET请求
///
/// 请求发起后处理回调结果的委托
///
IEnumerator _Get(string url, Action actionResult)
{
using (UnityWebRequest uwr = UnityWebRequest.Get(url))
{
yield return uwr.SendWebRequest();
actionResult?.Invoke(uwr);
}
}
/// 下载文件
UnityWebRequest uwr;
IEnumerator _DownloadFile(string url, string downloadFilePathAndName, Action actionResult)
{
uwr = new UnityWebRequest(url, UnityWebRequest.kHttpVerbGET);
uwr.downloadHandler = new DownloadHandlerFile(downloadFilePathAndName);
yield return uwr.SendWebRequest();
actionResult?.Invoke(uwr);
}
float a;
protected override void Update()
{
if (uwr!=null)
{
Debug.Log(uwr.downloadProgress);
}
}
/// 请求图片
IEnumerator _GetTexture(string url, Action actionResult)
{
UnityWebRequest uwr = new UnityWebRequest(url);
DownloadHandlerTexture downloadTexture = new DownloadHandlerTexture(true);
uwr.downloadHandler = downloadTexture;
yield return uwr.SendWebRequest();
Texture2D t = null;
if (!(uwr.isNetworkError || uwr.isHttpError))
{
t = downloadTexture.texture;
}
actionResult?.Invoke(t);
}
/// 请求AssetBundle
IEnumerator _GetAssetBundle(string url, Action actionResult)
{
UnityWebRequest www = new UnityWebRequest(url);
DownloadHandlerAssetBundle handler = new DownloadHandlerAssetBundle(www.url, uint.MaxValue);
www.downloadHandler = handler;
yield return www.SendWebRequest();
AssetBundle bundle = null;
if (!(www.isNetworkError || www.isHttpError))
{
bundle = handler.assetBundle;
}
actionResult?.Invoke(bundle);
}
//音效
IEnumerator _GetAudioClip(string url, Action actionResult, AudioType audioType = AudioType.WAV)
{
using (var uwr = UnityWebRequestMultimedia.GetAudioClip(url, audioType))
{
yield return uwr.SendWebRequest();
if (!(uwr.isNetworkError || uwr.isHttpError))
{
actionResult?.Invoke(DownloadHandlerAudioClip.GetContent(uwr));
}
}
}
/// 向服务器提交post请求
IEnumerator _Post(string serverURL, WWWForm wWWForm, Action actionResult)
{
UnityWebRequest uwr = UnityWebRequest.Post(serverURL, wWWForm);
yield return uwr.SendWebRequest();
actionResult?.Invoke(uwr);
}
///
/// 通过PUT方式将字节流传到服务器
///
/// 需要上传的字节流
/// 处理返回结果的委托
/// 设置header文件中的Content-Type属性
///
IEnumerator _UploadByPut(string url, byte[] contentBytes, Action actionResult, string contentType = "application/octet-stream")
{
UnityWebRequest uwr = new UnityWebRequest();
UploadHandler uploader = new UploadHandlerRaw(contentBytes);
uploader.contentType = contentType;
uwr.uploadHandler = uploader;
yield return uwr.SendWebRequest();
bool res = true;
if (uwr.isNetworkError || uwr.isHttpError)
{
res = false;
}
actionResult?.Invoke(res);
}
}