栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 游戏开发 > 其他

Unity2021版本UnityWebRequest使用方法参考

其他 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Unity2021版本UnityWebRequest使用方法参考

获取AssetBundle:

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

获取文本:

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

		if (webRequest.result == UnityWebRequest.Result.ConnectionError)
			Debug.Log("error = " + webRequest.error);
		else
		{
			string json = webRequest.downloadHandler.text;
			onGetJson?.Invoke(json);
		}
	}
}

获取图片:

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

获取音频:

IEnumerator GetAudio(string path, AudioType type, UnityAction onGetAudio)
{
	using (UnityWebRequest webRequest = UnityWebRequestMultimedia.GetAudioClip(path, type))
	{
		yield return webRequest.SendWebRequest();
		if (webRequest.result == UnityWebRequest.Result.ConnectionError)
			Debug.Log("error = " + webRequest.error);
		else
		{
			AudioClip clip = DownloadHandlerAudioClip.GetContent(webRequest);
			onGetAudio?.Invoke(clip);
		}
	}
}

加载视频:

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

另附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);
	}
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/905050.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号