Unity 中的UnityWebRequest使用方法参考

阅读: 评论:0

Unity 中的UnityWebRequest使用方法参考

Unity 中的UnityWebRequest使用方法参考

获取AssetBundle:

IEnumerator GetAssetBundle(string path, UnityAction<AssetBundle> onGetAssetBundle)
{using (UnityWebRequest webRequest = UnityWebRequestAssetBundle.GetAssetBundle(path)){yield return webRequest.SendWebRequest();if (sult == UnityWebRequest.Result.Success){AssetBundle ab = (webRequest.downloadHandler as DownloadHandlerAssetBundle).assetBundle;onGetAssetBundle?.Invoke(ab);}else{Debug.Log("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 (sult == UnityWebRequest.Result.Success){string json = ;onGetJson?.Invoke(json);}else{Debug.Log("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 (sult == UnityWebRequest.Result.Success){Texture2D tex2d = DownloadHandlerTexture.GetContent(webRequest);onGetTexture2D?.Invoke(tex2d);}else{Debug.Log("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 (sult == UnityWebRequest.Result.Success){AudioClip clip = DownloadHandlerAudioClip.GetContent(webRequest);onGetAudio?.Invoke(clip);}else{Debug.Log("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 (sult == UnityWebRequest.Result.Success){byte[] buffer = webRequest.downloadHandler.data;act?.Invoke(buffer);}else{Debug.Log("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);}
}

本文发布于:2024-01-31 01:50:24,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170663702524469.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23