当前位置:   article > 正文

Unity 中的UnityWebRequest使用方法参考_unitywebrequest.result.success

unitywebrequest.result.success

获取AssetBundle:

  1. IEnumerator GetAssetBundle(string path, UnityAction<AssetBundle> onGetAssetBundle)
  2. {
  3. using (UnityWebRequest webRequest = UnityWebRequestAssetBundle.GetAssetBundle(path))
  4. {
  5. yield return webRequest.SendWebRequest();
  6. if (webRequest.result == UnityWebRequest.Result.Success)
  7. {
  8. AssetBundle ab = (webRequest.downloadHandler as DownloadHandlerAssetBundle).assetBundle;
  9. onGetAssetBundle?.Invoke(ab);
  10. }
  11. else
  12. {
  13. Debug.Log("error = " + webRequest.error + "\n Load Path = " + path);
  14. onGetAssetBundle?.Invoke(null);
  15. }
  16. }
  17. }

获取文本:

  1. IEnumerator GetText(string path, UnityAction<string> onGetJson)
  2. {
  3. using (UnityWebRequest webRequest = UnityWebRequest.Get(path))
  4. {
  5. yield return webRequest.SendWebRequest();
  6. if (webRequest.result == UnityWebRequest.Result.Success)
  7. {
  8. string json = webRequest.downloadHandler.text;
  9. onGetJson?.Invoke(json);
  10. }
  11. else
  12. {
  13. Debug.Log("error = " + webRequest.error + "\n Load Path = " + path);
  14. onGetJson?.Invoke(null);
  15. }
  16. }
  17. }

获取图片:

  1. IEnumerator GetTexture2D(string path, UnityAction<Texture2D> onGetTexture2D)
  2. {
  3. using (UnityWebRequest webRequest = UnityWebRequestTexture.GetTexture(path))
  4. {
  5. yield return webRequest.SendWebRequest();
  6. if (webRequest.result == UnityWebRequest.Result.Success)
  7. {
  8. Texture2D tex2d = DownloadHandlerTexture.GetContent(webRequest);
  9. onGetTexture2D?.Invoke(tex2d);
  10. }
  11. else
  12. {
  13. Debug.Log("error = " + webRequest.error + "\n Load Path = " + path);
  14. onGetTexture2D?.Invoke(null);
  15. }
  16. }
  17. }

获取音频:

  1. IEnumerator GetAudio(string path, AudioType type, UnityAction<AudioClip> onGetAudio)
  2. {
  3. using (UnityWebRequest webRequest = UnityWebRequestMultimedia.GetAudioClip(path, type))
  4. {
  5. yield return webRequest.SendWebRequest();
  6. if (webRequest.result == UnityWebRequest.Result.Success)
  7. {
  8. AudioClip clip = DownloadHandlerAudioClip.GetContent(webRequest);
  9. onGetAudio?.Invoke(clip);
  10. }
  11. else
  12. {
  13. Debug.Log("error = " + webRequest.error + "\n Load Path = " + path);
  14. onGetAudio?.Invoke(null);
  15. }
  16. }
  17. }

获取buffer

  1. IEnumerator GetBuffer(string url, UnityAction<byte[]> act)
  2. {
  3. using (UnityWebRequest webRequest = UnityWebRequest.Get(url))
  4. {
  5. yield return webRequest.SendWebRequest();
  6. if (webRequest.result == UnityWebRequest.Result.Success)
  7. {
  8. byte[] buffer = webRequest.downloadHandler.data;
  9. act?.Invoke(buffer);
  10. }
  11. else
  12. {
  13. Debug.Log("error = " + webRequest.error + "\n Load Path = " + url);
  14. act?.Invoke(null);
  15. }
  16. }
  17. }

加载视频:

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

附一:

        Result枚举的介绍:

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

附二:

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

  1. using System.IO;
  2. using UnityEditor;
  3. using UnityEngine;
  4. public class BuildAssetBundle : MonoBehaviour
  5. {
  6. static void Package(BuildTarget buildTarget)
  7. {
  8. string packagePath = EditorUtility.OpenFolderPanel("Set " + buildTarget + " Save Path", Application.dataPath, "");
  9. if (packagePath.Length <= 0 || !Directory.Exists(packagePath))
  10. {
  11. Debug.Log("Directory Error!");
  12. return;
  13. }
  14. BuildPipeline.BuildAssetBundles(packagePath, BuildAssetBundleOptions.None, buildTarget);
  15. AssetDatabase.Refresh();
  16. }
  17. [MenuItem("BuildAssetBundle / WebGL")]
  18. static void PackageWebGL()
  19. {
  20. Package(BuildTarget.WebGL);
  21. }
  22. [MenuItem("BuildAssetBundle / Window")]
  23. static void PackageWindow()
  24. {
  25. Package(BuildTarget.StandaloneWindows);
  26. }
  27. [MenuItem("BuildAssetBundle / Android")]
  28. static void PackageAndroid()
  29. {
  30. Package(BuildTarget.Android);
  31. }
  32. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/95779
推荐阅读
相关标签
  

闽ICP备14008679号