Unity 中的UnityWebRequest使用方法参考

35 篇文章 1 订阅

获取AssetBundle:

IEnumerator GetAssetBundle(string path, UnityAction<AssetBundle> onGetAssetBundle)
{
	using (UnityWebRequest webRequest = UnityWebRequestAssetBundle.GetAssetBundle(path))
	{
		yield return webRequest.SendWebRequest();
		if (webRequest.result == UnityWebRequest.Result.Success)
		{
			AssetBundle ab = (webRequest.downloadHandler as DownloadHandlerAssetBundle).assetBundle;
			onGetAssetBundle?.Invoke(ab);
		}
		else
		{
			Debug.Log("error = " + webRequest.error + "\n Load Path = " + path);
			onGetAssetBundle?.Invoke(null);
		}
	}
}

获取文本:

IEnumerator GetText(string path, UnityAction<string> onGetJson)
{
	using (UnityWebRequest webRequest = UnityWebRequest.Get(path))
	{
		yield return webRequest.SendWebRequest();

		if (webRequest.result == UnityWebRequest.Result.Success)
		{
			string json = webRequest.downloadHandler.text;
			onGetJson?.Invoke(json);
		}
		else
		{
			Debug.Log("error = " + webRequest.error + "\n Load Path = " + path);
			onGetJson?.Invoke(null);
		}
	}
}

获取图片:

IEnumerator GetTexture2D(string path, UnityAction<Texture2D> onGetTexture2D)
{
	using (UnityWebRequest webRequest = UnityWebRequestTexture.GetTexture(path))
	{
		yield return webRequest.SendWebRequest();
		if (webRequest.result == UnityWebRequest.Result.Success)
		{
			Texture2D tex2d = DownloadHandlerTexture.GetContent(webRequest);
			onGetTexture2D?.Invoke(tex2d);
		}
		else
		{
			Debug.Log("error = " + webRequest.error + "\n Load Path = " + path);
			onGetTexture2D?.Invoke(null);
		}
	}
}

获取音频:

IEnumerator GetAudio(string path, AudioType type, UnityAction<AudioClip> onGetAudio)
{
	using (UnityWebRequest webRequest = UnityWebRequestMultimedia.GetAudioClip(path, type))
	{
		yield return webRequest.SendWebRequest();

		if (webRequest.result == UnityWebRequest.Result.Success)
		{
			AudioClip clip = DownloadHandlerAudioClip.GetContent(webRequest);
			onGetAudio?.Invoke(clip);
		}
		else
		{
			Debug.Log("error = " + webRequest.error + "\n Load Path = " + path);
			onGetAudio?.Invoke(null);
		}
	}
}

获取buffer

IEnumerator GetBuffer(string url, UnityAction<byte[]> act)
	{
		using (UnityWebRequest webRequest = UnityWebRequest.Get(url))
		{
			yield return webRequest.SendWebRequest();
			if (webRequest.result == UnityWebRequest.Result.Success)
			{
				byte[] buffer = webRequest.downloadHandler.data;
				act?.Invoke(buffer);
			}
			else
			{
				Debug.Log("error = " + webRequest.error + "\n Load Path = " + url);
				act?.Invoke(null);
			}
		}
	}

加载视频:

        这个事情早期版本要用到MovieTexure之类的,Unity目前版本加载视频实际上和UnityWebRequest没有毛线关系,请使用VideoPlayer,哈哈。

附一:

        Result枚举的介绍:

Result值内容描述
InProgress请求尚未完成。
Success请求成功。
ConnectionError无法与服务器进行通信。例如,请求无法连接或无法建立安全通道。
ProtocolError服务器返回了一个错误响应。请求成功与服务器通信,但收到连接协议定义的错误。
DataProcessingError处理数据时出错。请求成功与服务器通信,但在处理接收到的数据时遇到错误。例如,数据损坏或格式不正确。

附二:

AssetBundle创建参考脚本(这个脚本注意放到Editor文件夹下):

using System.IO;
using UnityEditor;
using UnityEngine;

public class BuildAssetBundle : MonoBehaviour
{
	static void Package(BuildTarget buildTarget)
	{
		string packagePath = EditorUtility.OpenFolderPanel("Set " + buildTarget + " Save Path", Application.dataPath, "");
		if (packagePath.Length <= 0 || !Directory.Exists(packagePath))
		{
			Debug.Log("Directory Error!");
			return;
		}
		BuildPipeline.BuildAssetBundles(packagePath, BuildAssetBundleOptions.None, buildTarget);
		AssetDatabase.Refresh();
	}

	[MenuItem("BuildAssetBundle / WebGL")]
	static void PackageWebGL()
	{
		Package(BuildTarget.WebGL);
	}

	[MenuItem("BuildAssetBundle / Window")]
	static void PackageWindow()
	{
		Package(BuildTarget.StandaloneWindows);
	}

	[MenuItem("BuildAssetBundle / Android")]
	static void PackageAndroid()
	{
		Package(BuildTarget.Android);
	}
}

  • 7
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值